Skip to content

Commit c86ca4d

Browse files
committed
Don't move stats in constructor
This is the best we can do with the current encoding of traits. 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
1 parent 2c29afe commit c86ca4d

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

Diff for: compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

+27-26
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ trait BCodeSkelBuilder extends BCodeHelpers {
129129

130130
val (clinits, body) = impl.body.partition(stat => stat.isInstanceOf[DefDef] && stat.symbol.isStaticConstructor)
131131

132-
val (uptoSuperStats, remainingConstrStats) = splitAtSuper(impl.constr.rhs.asInstanceOf[Block].stats)
133132
val clInitSymbol: TermSymbol =
134133
if (clinits.nonEmpty) clinits.head.symbol.asTerm
135134
else ctx.newSymbol(
@@ -150,40 +149,42 @@ trait BCodeSkelBuilder extends BCodeHelpers {
150149
coord = claszSymbol.coord
151150
).entered
152151

153-
val thisMap = new TreeMap {
154-
override def transform(tree: Tree)(using Context) = {
155-
val tp = tree.tpe.substThis(claszSymbol.asClass, claszSymbol.sourceModule.termRef)
156-
tree.withType(tp) match {
157-
case tree: This if tree.symbol == claszSymbol =>
158-
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))
163-
case tree =>
164-
super.transform(tree)
165-
}
166-
}
167-
}
168-
169-
def rewire(stat: Tree) = thisMap.transform(stat).changeOwner(claszSymbol.primaryConstructor, clInitSymbol)
152+
// val thisMap = new TreeMap {
153+
// override def transform(tree: Tree)(using Context) = {
154+
// val tp = tree.tpe.substThis(claszSymbol.asClass, claszSymbol.sourceModule.termRef)
155+
// tree.withType(tp) match {
156+
// case tree: This if tree.symbol == claszSymbol =>
157+
// ref(claszSymbol.sourceModule)
158+
// case Apply(fun @ Select(Super(qual, mix), _), args) if qual.symbol == claszSymbol =>
159+
// ref(claszSymbol.sourceModule).select(fun.symbol).appliedToArgs(args)
160+
// case ident: Ident =>
161+
// super.transform(desugarIdent(ident))
162+
// case tree =>
163+
// super.transform(tree)
164+
// }
165+
// }
166+
// }
167+
168+
// def rewire(stat: Tree) = thisMap.transform(stat).changeOwner(claszSymbol.primaryConstructor, clInitSymbol)
169+
170+
// val (uptoSuperStats, remainingConstrStats) = splitAtSuper(impl.constr.rhs.asInstanceOf[Block].stats)
171+
// val remainingConstrStatsSubst = remainingConstrStats.map(rewire)
170172

171173
val callConstructor = New(claszSymbol.typeRef).select(claszSymbol.primaryConstructor).appliedToArgs(Nil)
172174
val assignModuleField = Assign(ref(moduleField), callConstructor)
173-
val remainingConstrStatsSubst = remainingConstrStats.map(rewire)
174175
val clinit = clinits match {
175176
case (ddef: DefDef) :: _ =>
176-
cpy.DefDef(ddef)(rhs = Block(ddef.rhs :: assignModuleField :: remainingConstrStatsSubst, unitLiteral))
177+
cpy.DefDef(ddef)(rhs = Block(ddef.rhs :: assignModuleField :: Nil, unitLiteral))
177178
case _ =>
178-
DefDef(clInitSymbol, Block(assignModuleField :: remainingConstrStatsSubst, unitLiteral))
179+
DefDef(clInitSymbol, Block(assignModuleField :: Nil, unitLiteral))
179180
}
180181

181-
val constr2 = {
182-
val rhs = Block(uptoSuperStats, impl.constr.rhs.asInstanceOf[Block].expr)
183-
cpy.DefDef(impl.constr)(rhs = rhs)
184-
}
182+
// val constr2 = {
183+
// val rhs = Block(uptoSuperStats, impl.constr.rhs.asInstanceOf[Block].expr)
184+
// cpy.DefDef(impl.constr)(rhs = rhs)
185+
// }
185186

186-
val impl2 = cpy.Template(impl)(constr = constr2, body = clinit :: body)
187+
val impl2 = cpy.Template(impl)(body = clinit :: body)
187188
cpy.TypeDef(cd0)(rhs = impl2)
188189
} else cd0
189190

0 commit comments

Comments
 (0)