-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Invariant SAM type that's not a platform SAM: AbstractMethodError (JVM) / Referring to non-existent method (JS) #16388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report. |
I could reproduce the issue without Scala.js nor Java classes. All that's really needed is an invariant SAM type that is not a platform SAM type: abstract class Parent
trait Function[A, B] extends Parent {
def apply(a: A): B
}
object Test {
def test(x: (Int, Int), f: Function[? >: (Int, Int), ? <: Boolean]): Boolean =
f.apply(x)
def isLess(left: Int, right: Int): Boolean = {
// working
val f = new Function[(Int, Int), Boolean] {
override def apply(t: (Int, Int)): Boolean = left.compareTo(right) <= 0
}
test((left, right), f)
// NOT working
test(
(left, right),
_ => left.compareTo(right) <= 0
)
}
def main(args: Array[String]): Unit = {
println(isLess(1, 2))
println(isLess(1, 2))
println(isLess(2, 1))
}
}
So this is not Scala.js-specific. I think Test.test(Tuple2.apply[Int, Int](left, right),
{
def $anonfun(_$1: (Int, Int)): Boolean =
int2Integer(left).compareTo(int2Integer(right)).<=(0)
{
final class $anon() extends Parent(),
Function[? >: (Int, Int), ? <: Boolean] {
final def apply(_$1: (Int, Int)): Boolean = $anonfun(_$1)
}
new Parent with Function[? >: (Int, Int), ? <: Boolean] {...}()
}
}
) Notice the |
In fact, I wonder whether Test.test(Tuple2.apply[Int, Int](left, right),
{
def $anonfun(_$1: (Int, Int)): Boolean =
int2Integer(left).compareTo(int2Integer(right)).<=(0)
closure($anonfun:Function[? >: (Int, Int), ? <: Boolean])
}
) Not downright illegal, but since it obviously can correctly construct |
Also, if
so apparently it is already trying to protect itself from this issue, but it only does that for |
Aaaand ... I knew I had seen this somewhere before. It's a duplicate of #16065. |
Compiler version
scala 3.2.0, scalajs 1.11.0
Minimized code
https://scastie.scala-lang.org/kwQJBQRlSneSBEP93zzz8w
Output
Expectation
code will run without issues
The text was updated successfully, but these errors were encountered: