Skip to content

Fix #476, restoreScopes should maintain companion links #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ trait Symbols { this: Context =>
owner.thisType, modcls, parents, decls, TermRef.withSymAndName(owner.thisType, module, name)),
privateWithin, coord, assocFile)

val companionMethodFlags = Flags.Synthetic | Flags.Private | Flags.Method

def synthesizeCompanionMethod(name: Name, target: SymDenotation, owner: SymDenotation)(implicit ctx: Context) =
if (owner.exists && target.exists && !owner.isAbsent && !target.isAbsent) {
val existing = owner.unforcedDecls.lookup(name)

existing.orElse{
ctx.newSymbol(owner.symbol, name, Flags.Synthetic | Flags.Private, ExprType(target.typeRef))
ctx.newSymbol(owner.symbol, name, companionMethodFlags , ExprType(target.typeRef))
}
} else NoSymbol

Expand Down
14 changes: 14 additions & 0 deletions src/dotty/tools/dotc/transform/RestoreScopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ast.Trees._
import NameOps._
import typer.Mode
import TreeTransforms.TransformerInfo
import StdNames._

/** The preceding lambda lift and flatten phases move symbols to different scopes
* and rename them. This miniphase cleans up afterwards and makes sure that all
Expand All @@ -33,6 +34,19 @@ class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { t
// For top-level classes this does nothing.
val cls = tree.symbol.asClass
val pkg = cls.owner.asClass

// Bring back companion links
val companionClass = cls.info.decls.lookup(nme.COMPANION_CLASS_METHOD)
val companionModule = cls.info.decls.lookup(nme.COMPANION_MODULE_METHOD)

if (companionClass.exists) {
restoredDecls.enter(companionClass)
}

if (companionModule.exists) {
restoredDecls.enter(companionModule)
}

pkg.enter(cls)
val cinfo = cls.classInfo
tree.symbol.copySymDenotation(
Expand Down
14 changes: 14 additions & 0 deletions src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ class TreeChecker extends Phase with SymTransformer {
registry(name) = sym
}

def checkCompanion(symd: SymDenotation)(implicit ctx: Context): Unit = {
val cur = symd.linkedClass
val prev = ctx.atPhase(ctx.phase.prev) {
ct => {
implicit val ctx: Context = ct.withMode(Mode.FutureDefsOK)
symd.symbol.linkedClass
}
}

if (prev.exists)
assert(cur.exists, i"companion disappeared from $symd")
}

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

checkCompanion(symd)

symd
}

Expand Down
4 changes: 3 additions & 1 deletion src/dotty/tools/dotc/transform/ValueClasses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Symbols._
import SymDenotations._
import Contexts._
import Flags._
import StdNames._

/** Methods that apply to user-defined value classes */
object ValueClasses {
Expand All @@ -22,7 +23,8 @@ object ValueClasses {
isDerivedValueClass(d.owner) &&
!d.isConstructor &&
!d.is(SuperAccessor) &&
!d.is(Macro)
!d.is(Macro) &&
!(d.name eq nme.COMPANION_MODULE_METHOD)

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