Skip to content

Commit

Permalink
Use backend magic to handle super calls
Browse files Browse the repository at this point in the history
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: scala#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
  • Loading branch information
liufengyun committed Jul 13, 2020
1 parent 499aa70 commit b090711
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,16 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
if (t.symbol ne defn.Object_synchronized) genTypeApply(t)
else genSynchronized(app, expectedType)

case Apply(fun @ DesugaredSelect(Super(_, _), _), args) =>
case Apply(fun @ DesugaredSelect(Super(superQual, _), _), args) =>
// 'super' call: Note: since constructors are supposed to
// return an instance of what they construct, we have to take
// special care. On JVM they are 'void', and Scala forbids (syntactically)
// to call super constructors explicitly and/or use their 'returned' value.
// therefore, we can ignore this fact, and generate code that leaves nothing
// on the stack (contrary to what the type in the AST says).
mnode.visitVarInsn(asm.Opcodes.ALOAD, 0)

// scala/bug#10290: qual can be `this.$outer()` (not just `this`), so we call genLoad (not just ALOAD_0)
genLoad(superQual)
genLoadArguments(args, paramTKs(app))
generatedType = genCallMethod(fun.symbol, InvokeStyle.Super, app.span)

Expand Down
4 changes: 0 additions & 4 deletions compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ trait BCodeSkelBuilder extends BCodeHelpers {
tree.withType(tp) match {
case tree: This if tree.symbol == claszSymbol =>
ref(claszSymbol.sourceModule)
case Apply(fun @ Select(Super(qual, _), _), args) if qual.symbol == claszSymbol =>
ref(claszSymbol.sourceModule).select(fun.symbol).appliedToArgs(args)
// case ident: Ident =>
// super.transform(desugarIdent(ident))
case tree =>
super.transform(tree)
}
Expand Down

0 comments on commit b090711

Please sign in to comment.