Skip to content

Commit dbb0279

Browse files
committed
Fix-1756: Use lexically enclosing class as start of outer path.
We confused the enclosing class (which skips the current class in super call contexts) and the lexically enclosing class in three locations that all had to do with the start of an outer path.
1 parent 47d2084 commit dbb0279

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-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
@@ -82,7 +82,7 @@ class Erasure extends Phase with DenotTransformer { thisTransformer =>
8282
assertErased(tree)
8383
tree match {
8484
case res: tpd.This =>
85-
assert(!ExplicitOuter.referencesOuter(ctx.owner.enclosingClass, res),
85+
assert(!ExplicitOuter.referencesOuter(ctx.owner.lexicallyEnclosingClass, res),
8686
i"Reference to $res from ${ctx.owner.showLocated}")
8787
case ret: tpd.Return =>
8888
// checked only after erasure, as checking before erasure is complicated
@@ -389,7 +389,7 @@ object Erasure extends TypeTestsCasts{
389389
}
390390

391391
override def typedThis(tree: untpd.This)(implicit ctx: Context): Tree =
392-
if (tree.symbol == ctx.owner.enclosingClass || tree.symbol.isStaticOwner) promote(tree)
392+
if (tree.symbol == ctx.owner.lexicallyEnclosingClass || tree.symbol.isStaticOwner) promote(tree)
393393
else {
394394
ctx.log(i"computing outer path from ${ctx.owner.ownersIterator.toList}%, % to ${tree.symbol}, encl class = ${ctx.owner.enclosingClass}")
395395
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* class A { { val x = this } }
3+
class B(x: Int) {
4+
class C(x: Int)
5+
extends B({
6+
val test = this
7+
x
8+
}) {
9+
def this() = {
10+
this({
11+
1
12+
})
13+
}
14+
}
15+
}
16+
*/
17+
// Minimized version
18+
class D(x: Int) {
19+
class E(x: Int) extends D({val test = D.this; x})
20+
}
21+

0 commit comments

Comments
 (0)