Skip to content

Commit 7b6964a

Browse files
committed
Invalidate companions of classes lifted by FlatMap
RestoreScopes enters classes lifted by FlatMap into the enclosing package scope. It should also invalidate any companions because otherwise we risk linking up a new class with a previously generated companion which exists only as a Java classfile.
1 parent f440077 commit 7b6964a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/dotty/tools/dotc/transform/RestoreScopes.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import Symbols._
88
import Scopes._
99
import collection.mutable
1010
import TreeTransforms.MiniPhaseTransform
11+
import SymDenotations._
1112
import ast.Trees._
13+
import NameOps._
14+
import typer.Mode
1215
import TreeTransforms.TransformerInfo
1316

1417
/** The preceding lambda lift and flatten phases move symbols to different scopes
@@ -19,16 +22,32 @@ class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { t
1922
import ast.tpd._
2023
override def phaseName = "restoreScopes"
2124

25+
private def invalidateUndefinedCompanions(pkg: ClassSymbol, cls: ClassSymbol)(implicit ctx: Context): Unit = {
26+
val otherNames =
27+
if (cls is Flags.Module)
28+
List(cls.name.sourceModuleName, cls.name.stripModuleClassSuffix.toTypeName)
29+
else
30+
List(cls.name.toTermName, cls.name.moduleClassName)
31+
for (otherName <- otherNames) {
32+
val other = pkg.info.decl(otherName).asSymDenotation
33+
if (other.exists && !other.isCompleted) other.markAbsent
34+
}
35+
}
36+
2237
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context, info: TransformerInfo) = {
2338
val TypeDef(_, impl: Template) = tree
39+
//
2440
val restoredDecls = newScope
2541
for (stat <- impl.constr :: impl.body)
2642
if (stat.isInstanceOf[MemberDef] && stat.symbol.exists)
2743
restoredDecls.enter(stat.symbol)
28-
val cls = tree.symbol.asClass
29-
cls.owner.asClass.enter(cls)
3044
// Enter class in enclosing package scope, in case it was an inner class before flatten.
3145
// For top-level classes this does nothing.
46+
val cls = tree.symbol.asClass
47+
val pkg = cls.owner.asClass
48+
pkg.enter(cls)
49+
invalidateUndefinedCompanions(pkg, cls)(
50+
ctx.withPhase(cls.initial.validFor.phaseId).addMode(Mode.FutureDefsOK))
3251
val cinfo = cls.classInfo
3352
tree.symbol.copySymDenotation(
3453
info = cinfo.derivedClassInfo( // Dotty deviation: Cannot expand cinfo inline without a type error

0 commit comments

Comments
 (0)