Skip to content

Commit fff0162

Browse files
committed
Use backend magic to handle super calls
The problem can be seen from the following example: trait A { def foo() = ??? } trait B { def foo() = ??? } object C extends A with B { super[A].foo() super[B].foo() } In the code above, we cannot translate the following calls from <init> to <clinit>: super[A].foo() super[B].foo() super[A].$iinit$() super[B].$init$() More details can be found here: #5928 A principled way would be to generage super accessors as it is done in posttyper. However, the backend has a magic to support prefix to super trees in [1], which is exploited in the Scala 2 fix [2]. [1] scala/scala#5944 [2] scala/scala#7270
1 parent 3e6fb43 commit fff0162

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,16 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
710710
if (t.symbol ne defn.Object_synchronized) genTypeApply(t)
711711
else genSynchronized(app, expectedType)
712712

713-
case Apply(fun @ DesugaredSelect(Super(_, _), _), args) =>
713+
case Apply(fun @ DesugaredSelect(Super(superQual, _), _), args) =>
714714
// 'super' call: Note: since constructors are supposed to
715715
// return an instance of what they construct, we have to take
716716
// special care. On JVM they are 'void', and Scala forbids (syntactically)
717717
// to call super constructors explicitly and/or use their 'returned' value.
718718
// therefore, we can ignore this fact, and generate code that leaves nothing
719719
// on the stack (contrary to what the type in the AST says).
720-
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
720+
721+
// scala/bug#10290: qual can be `this.$outer()` (not just `this`), so we call genLoad (not just ALOAD_0)
722+
genLoad(superQual)
721723
genLoadArguments(args, paramTKs(app))
722724
generatedType = genCallMethod(fun.symbol, InvokeStyle.Super, app.span)
723725

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ trait BCodeSkelBuilder extends BCodeHelpers {
157157
tree.withType(tp) match {
158158
case tree: This if tree.symbol == claszSymbol =>
159159
ref(claszSymbol.sourceModule)
160-
case Apply(fun @ Select(Super(qual, _), _), args) if qual.symbol == claszSymbol =>
161-
ref(claszSymbol.sourceModule).select(fun.symbol).appliedToArgs(args)
162-
// case ident: Ident =>
163-
// super.transform(desugarIdent(ident))
164160
case tree =>
165161
super.transform(tree)
166162
}

0 commit comments

Comments
 (0)