diff options
author | Lukas Rytz <lukas.rytz@gmail.com> | 2016-08-30 16:26:05 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@gmail.com> | 2016-09-01 22:38:05 +0200 |
commit | a980fded6806f83bebe2ced31ab1ed70926254b2 (patch) | |
tree | c8885e4db2ad68476eb45291043d2c306961f7ab /test/junit | |
parent | 505c723b1e0bc282bdfa9cfc41adf24573ed19ad (diff) | |
download | scala-a980fded6806f83bebe2ced31ab1ed70926254b2.tar.gz scala-a980fded6806f83bebe2ced31ab1ed70926254b2.tar.bz2 scala-a980fded6806f83bebe2ced31ab1ed70926254b2.zip |
SD-143 error for super calls that cannot be implemented correctly
If a call super[T].m resolves to a method A.m where A is a class, but
not the direct superclass of the current class, there is no way to
emit an invocation of A.m: `invokespecial A.m` will resolve to B.m
where B is the superclass of the current class.
This commit adds an error message in this case.
Note that this is similar to an existing error message for qualified
super calls to a non-direct superclass:
class A { def m = 1 }
class B extends A { override def m = 2 }
class C extends B { override def m = super[A].m }
Gives "error: A does not name a parent class of class C".
If the user means to call method m in the superclass, he can write an
unqualified `super.m`.
An similar error message is introduced if A is a Java-defined interface
(and m is a default method), and A is not a direct parent of the current
class. In this case `invokespecial A.m` is invalid bytecode. The
solution is to add A as a direct parent of the current class.
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/lang/traits/BytecodeTest.scala | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/test/junit/scala/lang/traits/BytecodeTest.scala b/test/junit/scala/lang/traits/BytecodeTest.scala index a92d2244a2..c20fae2500 100644 --- a/test/junit/scala/lang/traits/BytecodeTest.scala +++ b/test/junit/scala/lang/traits/BytecodeTest.scala @@ -244,7 +244,6 @@ class BytecodeTest extends BytecodeTesting { @Test def sd143(): Unit = { - // this tests the status quo, which is wrong. val code = """class A { def m = 1 } |class B extends A { override def m = 2 } @@ -253,10 +252,28 @@ class BytecodeTest extends BytecodeTesting { | override def m = super[T].m // should invoke A.m |} """.stripMargin - val List(_, _, c, _) = compileClasses(code) - // even though the bytecode refers to A.m, invokespecial will resolve to B.m - assertSameCode(getMethod(c, "m"), - List(VarOp(ALOAD, 0), Invoke(INVOKESPECIAL, "A", "m", "()I", false), Op(IRETURN))) + + val err = + """cannot emit super call: the selected method m is declared in class A, which is not the direct superclass of class C. + |An unqualified super call (super.m) would be allowed.""".stripMargin + val cls = compileClasses(code, allowMessage = _.msg contains err) + assert(cls.isEmpty, cls.map(_.name)) + } + + @Test + def sd143b(): Unit = { + val jCode = List("interface A { default int m() { return 1; } }" -> "A.java") + val code = + """class B extends A { override def m = 2 } + |trait T extends A + |class C extends B with T { + | override def m = super[T].m + |} + """.stripMargin + + val err = "unable to emit super call unless interface A (which declares method m) is directly extended by class C" + val cls = compileClasses(code, jCode, allowMessage = _.msg contains err) + assert(cls.isEmpty, cls.map(_.name)) } @Test |