You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ cat sandbox/A_1.scala
class C {
class Inner
}
object Test {
def main(args: Array[String]): Unit = {
val c = new C
val i = new c.Inner
(i: Any) match {
case _: c.Inner =>
println("matched `case _: c.Inner`")
}
}
}
$ cat sandbox/A_2.scala
class C {
final class Inner
}
$ scalac sandbox/A_1.scala && scala Test && scalac sandbox/A_2.scala && scala Test
matched `case _: c.Inner`
java.lang.NoSuchMethodError: C$Inner.C$Inner$$$outer()LC;
at Test$.main(A_1.scala:10)
at Test.main(A_1.scala)
Inner classes marked final do not store the outer pointer unless it is captured. They still have the outer parameter in their constructor, so new c.Inner is binary compatible in the example above.
// access flags 0x1
public main([Ljava/lang/String;)V
// parameter final args
NEW C
DUP
INVOKESPECIAL C.<init> ()V
ASTORE 3
NEW C$Inner
DUP
ALOAD 3
INVOKESPECIAL C$Inner.<init> (LC;)V
But type tests in pattern matching and isInstanceOf that check prefixes have a binary fragility.
The runtime prefix check is elided if it can the prefix of the scrutinee is statically the same as that of the tested type, hence the upcast to Any in this test case.
We could make this more binary compatible by still generating the $outer accessor in final inner classes (it would return null) and treating a null prefix in the scrutinee as a wildcard for the prefix check.
The text was updated successfully, but these errors were encountered:
Inner classes marked
final
do not store the outer pointer unless it is captured. They still have the outer parameter in their constructor, sonew c.Inner
is binary compatible in the example above.But type tests in pattern matching and
isInstanceOf
that check prefixes have a binary fragility.The runtime prefix check is elided if it can the prefix of the scrutinee is statically the same as that of the tested type, hence the upcast to
Any
in this test case.We could make this more binary compatible by still generating the
$outer
accessor in final inner classes (it would return null) and treating a null prefix in the scrutinee as a wildcard for the prefix check.The text was updated successfully, but these errors were encountered: