Skip to content

Commit 49d88a7

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 2c29afe commit 49d88a7

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,16 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
700700
if (t.symbol ne defn.Object_synchronized) genTypeApply(t)
701701
else genSynchronized(app, expectedType)
702702

703-
case Apply(fun @ DesugaredSelect(Super(_, _), _), args) =>
703+
case Apply(fun @ DesugaredSelect(Super(superQual, _), _), args) =>
704704
// 'super' call: Note: since constructors are supposed to
705705
// return an instance of what they construct, we have to take
706706
// special care. On JVM they are 'void', and Scala forbids (syntactically)
707707
// to call super constructors explicitly and/or use their 'returned' value.
708708
// therefore, we can ignore this fact, and generate code that leaves nothing
709709
// on the stack (contrary to what the type in the AST says).
710-
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)
710+
711+
// scala/bug#10290: qual can be `this.$outer()` (not just `this`), so we call genLoad (not just ALOAD_0)
712+
genLoad(superQual)
711713
genLoadArguments(args, paramTKs(app))
712714
generatedType = genCallMethod(fun.symbol, InvokeStyle.Super, app.span)
713715

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

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

0 commit comments

Comments
 (0)