Skip to content

Commit c3844e5

Browse files
committed
Merge pull request scala#478 from dotty-staging/fix-restoreScopes
Fix scala#476, restoreScopes should maintain companion links
2 parents 4d60c90 + 4efca0f commit c3844e5

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ trait Symbols { this: Context =>
161161
owner.thisType, modcls, parents, decls, TermRef.withSymAndName(owner.thisType, module, name)),
162162
privateWithin, coord, assocFile)
163163

164+
val companionMethodFlags = Flags.Synthetic | Flags.Private | Flags.Method
165+
164166
def synthesizeCompanionMethod(name: Name, target: SymDenotation, owner: SymDenotation)(implicit ctx: Context) =
165167
if (owner.exists && target.exists && !owner.isAbsent && !target.isAbsent) {
166168
val existing = owner.unforcedDecls.lookup(name)
167169

168170
existing.orElse{
169-
ctx.newSymbol(owner.symbol, name, Flags.Synthetic | Flags.Private, ExprType(target.typeRef))
171+
ctx.newSymbol(owner.symbol, name, companionMethodFlags , ExprType(target.typeRef))
170172
}
171173
} else NoSymbol
172174

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ast.Trees._
1313
import NameOps._
1414
import typer.Mode
1515
import TreeTransforms.TransformerInfo
16+
import StdNames._
1617

1718
/** The preceding lambda lift and flatten phases move symbols to different scopes
1819
* and rename them. This miniphase cleans up afterwards and makes sure that all
@@ -33,6 +34,19 @@ class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { t
3334
// For top-level classes this does nothing.
3435
val cls = tree.symbol.asClass
3536
val pkg = cls.owner.asClass
37+
38+
// Bring back companion links
39+
val companionClass = cls.info.decls.lookup(nme.COMPANION_CLASS_METHOD)
40+
val companionModule = cls.info.decls.lookup(nme.COMPANION_MODULE_METHOD)
41+
42+
if (companionClass.exists) {
43+
restoredDecls.enter(companionClass)
44+
}
45+
46+
if (companionModule.exists) {
47+
restoredDecls.enter(companionModule)
48+
}
49+
3650
pkg.enter(cls)
3751
val cinfo = cls.classInfo
3852
tree.symbol.copySymDenotation(

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ class TreeChecker extends Phase with SymTransformer {
5656
registry(name) = sym
5757
}
5858

59+
def checkCompanion(symd: SymDenotation)(implicit ctx: Context): Unit = {
60+
val cur = symd.linkedClass
61+
val prev = ctx.atPhase(ctx.phase.prev) {
62+
ct => {
63+
implicit val ctx: Context = ct.withMode(Mode.FutureDefsOK)
64+
symd.symbol.linkedClass
65+
}
66+
}
67+
68+
if (prev.exists)
69+
assert(cur.exists, i"companion disappeared from $symd")
70+
}
5971

6072
def transformSym(symd: SymDenotation)(implicit ctx: Context): SymDenotation = {
6173
val sym = symd.symbol
@@ -69,6 +81,8 @@ class TreeChecker extends Phase with SymTransformer {
6981
testDuplicate(sym, seenClasses, "class")
7082
}
7183

84+
checkCompanion(symd)
85+
7286
symd
7387
}
7488

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Symbols._
77
import SymDenotations._
88
import Contexts._
99
import Flags._
10+
import StdNames._
1011

1112
/** Methods that apply to user-defined value classes */
1213
object ValueClasses {
@@ -22,7 +23,8 @@ object ValueClasses {
2223
isDerivedValueClass(d.owner) &&
2324
!d.isConstructor &&
2425
!d.is(SuperAccessor) &&
25-
!d.is(Macro)
26+
!d.is(Macro) &&
27+
!(d.name eq nme.COMPANION_MODULE_METHOD)
2628

2729
/** The member that of a derived value class that unboxes it. */
2830
def valueClassUnbox(d: ClassDenotation)(implicit ctx: Context): Symbol =

0 commit comments

Comments
 (0)