com-lihaoyi / unroll

23 stars 0 forks source link

`Failure to disambiguate overloaded reference` error only in ujson Scala 3.3.x/Scala.js #5

Closed lihaoyi closed 6 months ago

lihaoyi commented 6 months ago

Using the latest head version of Unroll in uPickle (https://github.com/com-lihaoyi/upickle/pull/555/files) gives me the error below. This only seems to happen in Scala-3.3.0/Scala.JS (3.3.1 gives same error)

[911/3781] ujson.js[3.3.0].compile 
[info] compiling 21 Scala sources to /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/compile.dest/classes ...
dotty.tools.dotc.core.TypeError$$anon$1: Failure to disambiguate overloaded reference with
  method transform in package ujson: (x$0: ujson.Readable, x$1: upickle.core.Visitor): Object  and
  method transform in package ujson: (t: ujson.Readable, v: upickle.core.Visitor, sortKeys: Boolean): Object while running genSJSIR on /Users/lihaoyi/Github/upickle/ujson/src/ujson/package.scala
[info] exception occurred while compiling /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/BaseByteRenderer.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/BaseCharRenderer.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ByteParser.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/CharParser.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ToDecimalByte.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ToDecimalChar.scala, /Users/lihaoyi/Github/upickle/ujson/src-js/ujson/WebJson.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/AstTransformer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/ByteArrayParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/ByteBufferParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/CharSequenceParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Exceptions.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/IndexedValue.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/InputStreamParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/JsVisitor.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Readable.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Renderer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/StringParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Transformer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Value.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/package.scala
Failure to disambiguate overloaded reference with
  method transform in package ujson: (x$0: ujson.Readable, x$1: upickle.core.Visitor): Object  and
  method transform in package ujson: (t: ujson.Readable, v: upickle.core.Visitor, sortKeys: Boolean): Object while compiling /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/BaseByteRenderer.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/BaseCharRenderer.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ByteParser.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/CharParser.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ToDecimalByte.scala, /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/generatedSources.dest/ToDecimalChar.scala, /Users/lihaoyi/Github/upickle/ujson/src-js/ujson/WebJson.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/AstTransformer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/ByteArrayParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/ByteBufferParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/CharSequenceParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Exceptions.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/IndexedValue.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/InputStreamParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/JsVisitor.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Readable.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Renderer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/StringParser.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Transformer.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/Value.scala, /Users/lihaoyi/Github/upickle/ujson/src/ujson/package.scala
[error] ## Exception when compiling 21 sources to /Users/lihaoyi/Github/upickle/out/ujson/js/3.3.0/compile.dest/classes
[error] dotty.tools.dotc.core.TypeError$$anon$1: Failure to disambiguate overloaded reference with
[error]   method transform in package ujson: (x$0: ujson.Readable, x$1: upickle.core.Visitor): Object  and
[error]   method transform in package ujson: (t: ujson.Readable, v: upickle.core.Visitor, sortKeys: Boolean): Object
[error] 
[error]            
sjrd commented 6 months ago

What's the stack trace of the error?

lihaoyi commented 6 months ago

@sjrd at least as part of the Mill build, no stack trace is shown. Just the message above

lihaoyi commented 6 months ago

I've minimized the code-under-compilation to much smaller repro:

package unroll
object Unrolled{
  def foo(s: String, n: Int = 1, @Unroll b: Boolean = true) = s + n + b
}

This can be reproduced by checking out https://github.com/com-lihaoyi/unroll and running ./mill -i -j 5 -k "unroll[3.3.1].tests[objectMethod].v2.js.compile", printing

[#3] 
[#3]   unhandled exception while running genSJSIR on /Users/lihaoyi/Github/unroll/unroll/tests/objectMethod/v2/src/Unrolled.scala
[#3] 
[#3]   An unhandled exception was thrown in the compiler.
[#3]   Please file a crash report here:
[#3]   https://github.com/lampepfl/dotty/issues/new/choose
[#3] 
[#3]      while compiling: <no file>
[#3]         during phase: <no phase>
[#3]                 mode: Mode(ImplicitsEnabled)
[#3]      library version: version 2.13.10
[#3]     compiler version: version 3.3.1
[#3]             settings: -Vprint List(all) -Xplugin List(/Users/lihaoyi/Github/unroll/out/unroll/3.3.1/plugin/jar.dest/out.jar) -Xplugin-require List(unroll) -bootclasspath /Users/lihaoyi/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.10/scala-library-2.13.10.jar -classpath /Users/lihaoyi/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_sjs1_3/3.3.1/scala3-library_sjs1_3-3.3.1.jar:/Users/lihaoyi/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-js/scalajs-library_2.13/1.13.1/scalajs-library_2.13-1.13.1.jar:/Users/lihaoyi/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.3.1/scala3-library_3-3.3.1.jar:/Users/lihaoyi/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-js/scalajs-javalib/1.13.1/scalajs-javalib-1.13.1.jar:/Users/lihaoyi/Github/unroll/unroll/annotation/compile-resources:/Users/lihaoyi/Github/unroll/out/unroll/3.3.1/annotation/compile.dest/classes:/Users/lihaoyi/Github/unroll/unroll/tests/objectMethod/v2/compile-resources:/Users/lihaoyi/Github/unroll/out/unroll/3.3.1/tests/objectMethod/v2/js/compile.dest/classes -d /Users/lihaoyi/Github/unroll/out/unroll/3.3.1/tests/objectMethod/v2/js/compile.dest/classes -scalajs true
[#3] 
[#3]                 tree: EmptyTree
[#3]        tree position: :<unknown>
[#3]            tree type: <notype>
[#3]               symbol: val <none>
[#3]            call site: package <root> in module class <root>
[#3] 
[#3]   == Source file context for tree position ==
[#3] 
[#3] 
[#3] [error] ## Exception when compiling 1 sources to /Users/lihaoyi/Github/unroll/out/unroll/3.3.1/tests/objectMethod/v2/js/compile.dest/classes
[#3] [error] dotty.tools.dotc.core.TypeError$$anon$1: Failure to disambiguate overloaded reference with
[#3] [error]   method foo in object Unrolled: (x$0: String, x$1: Int): String  and
[#3] [error]   method foo in object Unrolled: (s: String, n: Int, b: Boolean): String
[#3] [error] 
[#3] [error]        

You can also run ./mill -i -j 5 -k "unroll[_].tests[_].__.run" to run all the test cases, and show that only the unroll[3.3.1].tests[objectMethod].v2.js and unroll[3.3.1].tests[objectMethod].v3.js test cases fail (I haven't managed to come up with a repro for the other crasher in Scala-Native https://github.com/com-lihaoyi/unroll/issues/6)

lihaoyi commented 6 months ago

-Xprint:all shows the last phase before the crash looking like this

[#3] [info] [[syntax trees at end of MegaPhase{dropOuterAccessors, checkNoSuperThis, flatten, transformWildcards, moveStatic, expandPrivate, restoreScopes, selectStatic, junitBootstrappers, Collect entry points, collectSuperCalls, repeatableAnnotations}]] // /Users/lihaoyi/Github/unroll/unroll/tests/objectMethod/v2/src/Unrolled.scala
[#3] [info] package unroll {
[#3] [info]   @SourceFile("unroll/tests/objectMethod/v2/src/Unrolled.scala") final module
[#3] [info]     class Unrolled extends Object {
[#3] [info]     def <init>(): Unit =
[#3] [info]       {
[#3] [info]         super()
[#3] [info]         ()
[#3] [info]       }
[#3] [info]     private def writeReplace(): Object =
[#3] [info]       new scala.runtime.ModuleSerializationProxy(classOf[unroll.Unrolled])
[#3] [info]     def foo(s: String, n: Int, @Unroll b: Boolean): String =
[#3] [info]       s.+(scala.Int.box(n)).+(scala.Boolean.box(b))
[#3] [info]     def foo$default$2(): Int = 1
[#3] [info]     def foo$default$3(): Boolean = true
[#3] [info]     def foo(s: String, n: Int): String = this.foo(s, n, this.foo$default$3())
[#3] [info]   }
[#3] [info]   final lazy module val Unrolled: unroll.Unrolled = new unroll.Unrolled()

The error appears to be coming from https://github.com/lampepfl/dotty/blob/067ec20820943d61cf29733ea489e25bb920ea37/compiler/src/dotty/tools/dotc/core/Denotations.scala#L1267-L1277, though without a stack trace it's hard to tell how it got to that point

sjrd commented 6 months ago

I think there's a debug flag to pass to the compiler to enable full stack traces.

lihaoyi commented 6 months ago

-Ydebug-type-error and -Ydebug-error don't seem to give me any stack traces

I haven't managed to build a Dotty locally with additional logging; trying to run sbt publishLocal from 3.3.2-RC3 and use the compiler jar gives me the following error

[error] java.lang.UnsupportedClassVersionError: dotty/tools/io/JDK9Reflectors has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0
lihaoyi commented 6 months ago

Found it, just need to do defdef.symbol.flags &~ HasDefaultParams on the new symbol. Apparently it's set separately and can get out of sync with the method parameter's default parameters

lihaoyi commented 6 months ago

@sjrd for the record, I think the relevant callsite is this one

https://github.com/lampepfl/dotty/blob/067ec20820943d61cf29733ea489e25bb920ea37/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala#L4900

Presumably JSCodeGen.scala is the only place in Dotty that checks whether or not multiple overloads have HasDefaultParams set. I don't know how the other JVM/native backends check for such problematic overloads; maybe they inspect the default parameter trees/symbols instead?

sjrd commented 6 months ago

I expect the JVM and Native backends don't need that information in the first place. We need it for the interpretability semantics with JavaScript.