Skip to content

Commit b8e3126

Browse files
authored
Merge pull request #1766 from dotty-staging/fix-#1756
Fix #1756: Use lexically enclosing class as start of outer path.
2 parents 4daf543 + 5175f1e commit b8e3126

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Erasure extends Phase with DenotTransformer { thisTransformer =>
9494
assertErased(tree)
9595
tree match {
9696
case res: tpd.This =>
97-
assert(!ExplicitOuter.referencesOuter(ctx.owner.enclosingClass, res),
97+
assert(!ExplicitOuter.referencesOuter(ctx.owner.lexicallyEnclosingClass, res),
9898
i"Reference to $res from ${ctx.owner.showLocated}")
9999
case ret: tpd.Return =>
100100
// checked only after erasure, as checking before erasure is complicated
@@ -412,7 +412,7 @@ object Erasure extends TypeTestsCasts{
412412
}
413413

414414
override def typedThis(tree: untpd.This)(implicit ctx: Context): Tree =
415-
if (tree.symbol == ctx.owner.enclosingClass || tree.symbol.isStaticOwner) promote(tree)
415+
if (tree.symbol == ctx.owner.lexicallyEnclosingClass || tree.symbol.isStaticOwner) promote(tree)
416416
else {
417417
ctx.log(i"computing outer path from ${ctx.owner.ownersIterator.toList}%, % to ${tree.symbol}, encl class = ${ctx.owner.enclosingClass}")
418418
outer.path(tree.symbol)

compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ object ExplicitOuter {
330330
/** The path of outer accessors that references `toCls.this` starting from
331331
* the context owner's this node.
332332
*/
333-
def path(toCls: Symbol, start: Tree = This(ctx.owner.enclosingClass.asClass)): Tree = try {
333+
def path(toCls: Symbol, start: Tree = This(ctx.owner.lexicallyEnclosingClass.asClass)): Tree = try {
334334
def loop(tree: Tree): Tree = {
335335
val treeCls = tree.tpe.widen.classSymbol
336336
val outerAccessorCtx = ctx.withPhaseNoLater(ctx.lambdaLiftPhase) // lambdalift mangles local class names, which means we cannot reliably find outer acessors anymore

tests/pos/i1756.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class A { { val x = this } }
2+
class B(x: Int) {
3+
class C(x: Int)
4+
extends B({
5+
val test = this
6+
x
7+
}) {
8+
def this() = {
9+
this({
10+
1
11+
})
12+
}
13+
}
14+
}
15+
16+
// Minimized version
17+
class D(x: Int) {
18+
class E(x: Int) extends D({val test = D.this; x})
19+
}
20+

0 commit comments

Comments
 (0)