Skip to content

Tweaks to ExplicitOuter and TreeTypeMap #1168

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 5 commits into from
Mar 14, 2016
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
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/ast/TreeTypeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ final class TreeTypeMap(
constr = tmap.transformSub(constr),
parents = parents mapconserve transform,
self = tmap.transformSub(self),
body = impl.body mapconserve tmap.transform
body = impl.body mapconserve
(tmap.transform(_)(ctx.withOwner(mapOwner(impl.symbol.owner))))
).withType(tmap.mapType(impl.tpe))
case tree1 =>
tree1.withType(mapType(tree1.tpe)) match {
Expand Down
14 changes: 1 addition & 13 deletions src/dotty/tools/dotc/transform/Constructors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import collection.mutable
* - also moves private fields that are accessed only from constructor
* into the constructor if possible.
*/
class Constructors extends MiniPhaseTransform with SymTransformer { thisTransform =>
class Constructors extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform =>
import tpd._

override def phaseName: String = "constructors"
Expand Down Expand Up @@ -99,18 +99,6 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
}
}

/** Symbols that are owned by either <local dummy> or a class field move into the
* primary constructor.
*/
override def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {
def ownerBecomesConstructor(owner: Symbol): Boolean =
(owner.isLocalDummy || owner.isTerm && !owner.is(MethodOrLazy)) &&
owner.owner.isClass
if (ownerBecomesConstructor(sym.owner))
sym.copySymDenotation(owner = sym.owner.enclosingClass.primaryConstructor)
else sym
}

/** @return true if after ExplicitOuter, all references from this tree go via an
* outer link, so no parameter accessors need to be rewired to parameters
*/
Expand Down
41 changes: 24 additions & 17 deletions src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,34 @@ object ExplicitOuter {
/** Tree references an outer class of `cls` which is not a static owner.
*/
def referencesOuter(cls: Symbol, tree: Tree)(implicit ctx: Context): Boolean = {
def isOuter(sym: Symbol) =
def isOuterSym(sym: Symbol) =
!sym.isStaticOwner && cls.isProperlyContainedIn(sym)
def isOuterRef(ref: Type): Boolean = ref match {
case ref: ThisType =>
isOuterSym(ref.cls)
case ref: TermRef =>
if (ref.prefix ne NoPrefix)
!ref.symbol.isStatic && isOuterRef(ref.prefix)
else if (ref.symbol is Hoistable)
// ref.symbol will be placed in enclosing class scope by LambdaLift, so it might need
// an outer path then.
isOuterSym(ref.symbol.owner.enclosingClass)
else
// ref.symbol will get a proxy in immediately enclosing class. If this properly
// contains the current class, it needs an outer path.
ctx.owner.enclosingClass.owner.enclosingClass.isContainedIn(ref.symbol.owner)
case _ => false
}
def hasOuterPrefix(tp: Type) = tp match {
case TypeRef(prefix, _) => isOuterRef(prefix)
case _ => false
}
tree match {
case thisTree @ This(_) =>
isOuter(thisTree.symbol)
case id: Ident =>
id.tpe match {
case ref @ TermRef(NoPrefix, _) =>
if (ref.symbol is Hoistable)
// ref.symbol will be placed in enclosing class scope by LambdaLift, so it might need
// an outer path then.
isOuter(ref.symbol.owner.enclosingClass)
else
// ref.symbol will get a proxy in immediately enclosing class. If this properly
// contains the current class, it needs an outer path.
ctx.owner.enclosingClass.owner.enclosingClass.isContainedIn(ref.symbol.owner)
case _ => false
}
case _: This | _: Ident => isOuterRef(tree.tpe)
case nw: New =>
val newCls = nw.tpe.classSymbol
isOuter(newCls.owner.enclosingClass) ||
isOuterSym(newCls.owner.enclosingClass) ||
hasOuterPrefix(nw.tpe) ||
newCls.owner.isTerm && cls.isProperlyContainedIn(newCls)
// newCls might get proxies for free variables. If current class is
// properly contained in newCls, it needs an outer path to newCls access the
Expand Down
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
val vsym = stat.symbol
val isym = initializer(vsym)
val rhs = Block(
initBuf.toList.map(_.changeOwner(impl.symbol, isym)),
stat.rhs.changeOwner(vsym, isym).wildcardToDefault)
initBuf.toList.map(_.changeOwnerAfter(impl.symbol, isym, thisTransform)),
stat.rhs.changeOwnerAfter(vsym, isym, thisTransform).wildcardToDefault)
initBuf.clear()
cpy.DefDef(stat)(rhs = EmptyTree) :: DefDef(isym, rhs) :: Nil
case stat: DefDef if stat.symbol.isSetter =>
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i1131.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Outer {
class Inner
}

trait MustBeATrait {
val o = new Outer
val inner = {
class C {
new o.Inner
}
new C
}
val inner2 = new o.Inner {}
}