diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-01-20 12:31:22 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-01-20 17:02:11 +0100 |
commit | 1a7de4314ac72bca81e31ad3ac0af7bee7eed26b (patch) | |
tree | 45bf4f3f2e81dc32256da07b2225aa99e0cd4f82 /src/compiler | |
parent | 6f72ed85c3882d2a8c824a41e6e42d7f33b8d1d6 (diff) | |
download | scala-1a7de4314ac72bca81e31ad3ac0af7bee7eed26b.tar.gz scala-1a7de4314ac72bca81e31ad3ac0af7bee7eed26b.tar.bz2 scala-1a7de4314ac72bca81e31ad3ac0af7bee7eed26b.zip |
SI-6666 Restrict hidden `this` access in self/super calls.
Detect when classes (user authored or compiler generated)
local to a self or super constructor argument would require
premature access to the in-construction instance.
The same restriction applies for classes and objects; for objects,
the premature access would result in a null via MODULE$ field.
A residual error has been lodged as SI-6997.
I'd like to remove calls to `Symbol#outerClass` (which relies on
the flaky flag INCONSTRUCTOR, see my comments in the JIRA issue
for more discussion) from `LambdaLift` and `ExplicitOuter`, and
instead use the stack of active self/super calls to know when to
skip an enclosing class. That will obviate that flag.
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/LambdaLift.scala | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala index 4a23e65ad2..a4f75f424f 100644 --- a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala +++ b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala @@ -320,12 +320,24 @@ abstract class LambdaLift extends InfoTransform { private def memberRef(sym: Symbol) = { val clazz = sym.owner.enclClass //Console.println("memberRef from "+currentClass+" to "+sym+" in "+clazz) - val qual = if (clazz == currentClass) gen.mkAttributedThis(clazz) - else { - sym resetFlag(LOCAL | PRIVATE) - if (clazz.isStaticOwner) gen.mkAttributedQualifier(clazz.thisType) - else outerPath(outerValue, currentClass.outerClass, clazz) - } + def prematureSelfReference() { + val what = + if (clazz.isStaticOwner) clazz.fullLocationString + else s"the unconstructed `this` of ${clazz.fullLocationString}" + val msg = s"Implementation restriction: access of ${sym.fullLocationString} from ${currentClass.fullLocationString}, would require illegal premature access to $what" + currentUnit.error(curTree.pos, msg) + } + val qual = + if (clazz == currentClass) gen.mkAttributedThis(clazz) + else { + sym resetFlag (LOCAL | PRIVATE) + if (selfOrSuperCalls exists (_.owner == clazz)) { + prematureSelfReference() + EmptyTree + } + else if (clazz.isStaticOwner) gen.mkAttributedQualifier(clazz.thisType) + else outerPath(outerValue, currentClass.outerClass, clazz) + } Select(qual, sym) setType sym.tpe } @@ -495,13 +507,25 @@ abstract class LambdaLift extends InfoTransform { private def preTransform(tree: Tree) = super.transform(tree) setType lifted(tree.tpe) + /** The stack of constructor symbols in which a call to this() or to the super + * constructor is active. + */ + private val selfOrSuperCalls = mutable.Stack[Symbol]() + @inline private def inSelfOrSuperCall[A](sym: Symbol)(a: => A) = try { + selfOrSuperCalls push sym + a + } finally selfOrSuperCalls.pop() + override def transform(tree: Tree): Tree = tree match { case Select(ReferenceToBoxed(idt), elem) if elem == nme.elem => postTransform(preTransform(idt), isBoxedRef = false) case ReferenceToBoxed(idt) => postTransform(preTransform(idt), isBoxedRef = true) case _ => - postTransform(preTransform(tree)) + def transformTree = postTransform(preTransform(tree)) + if (treeInfo isSelfOrSuperConstrCall tree) + inSelfOrSuperCall(currentOwner)(transformTree) + else transformTree } /** Transform statements and add lifted definitions to them. */ |