Skip to content

Commit 627c330

Browse files
committed
Use outer field instead of outer accessor where possible
This is a necessary first step to be able to move the field into the class constructor
1 parent 5674066 commit 627c330

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -386,26 +386,28 @@ object ExplicitOuter {
386386
*/
387387
def path(start: Tree = This(ctx.owner.lexicallyEnclosingClass.asClass),
388388
toCls: Symbol = NoSymbol,
389-
count: Int = -1): Tree = try {
390-
@tailrec def loop(tree: Tree, count: Int): Tree = {
391-
val treeCls = tree.tpe.widen.classSymbol
392-
val outerAccessorCtx = ctx.withPhaseNoLater(ctx.lambdaLiftPhase) // lambdalift mangles local class names, which means we cannot reliably find outer acessors anymore
393-
ctx.log(i"outer to $toCls of $tree: ${tree.tpe}, looking for ${outerAccName(treeCls.asClass)(outerAccessorCtx)} in $treeCls")
394-
if (count == 0 || count < 0 && treeCls == toCls) tree
395-
else {
396-
val acc = outerAccessor(treeCls.asClass)(outerAccessorCtx)
397-
assert(acc.exists,
398-
i"failure to construct path from ${ctx.owner.ownersIterator.toList}%/% to `this` of ${toCls.showLocated};\n${treeCls.showLocated} does not have an outer accessor")
399-
loop(tree.select(acc).ensureApplied, count - 1)
400-
}
401-
}
402-
ctx.log(i"computing outerpath to $toCls from ${ctx.outersIterator.map(_.owner).toList}")
403-
loop(start, count)
404-
}
405-
catch {
406-
case ex: ClassCastException =>
389+
count: Int = -1): Tree =
390+
try
391+
@tailrec def loop(tree: Tree, count: Int): Tree =
392+
val treeCls = tree.tpe.widen.classSymbol
393+
val outerAccessorCtx = ctx.withPhaseNoLater(ctx.lambdaLiftPhase) // lambdalift mangles local class names, which means we cannot reliably find outer acessors anymore
394+
ctx.log(i"outer to $toCls of $tree: ${tree.tpe}, looking for ${outerAccName(treeCls.asClass)(outerAccessorCtx)} in $treeCls")
395+
if (count == 0 || count < 0 && treeCls == toCls) tree
396+
else
397+
val enclClass = ctx.owner.lexicallyEnclosingClass.asClass
398+
val outerAcc = tree match
399+
case tree: This if tree.symbol == enclClass =>
400+
outerParamAccessor(enclClass)(using outerAccessorCtx)
401+
case _ =>
402+
outerAccessor(treeCls.asClass)(using outerAccessorCtx)
403+
assert(outerAcc.exists,
404+
i"failure to construct path from ${ctx.owner.ownersIterator.toList}%/% to `this` of ${toCls.showLocated};\n${treeCls.showLocated} does not have an outer accessor")
405+
loop(tree.select(outerAcc).ensureApplied, count - 1)
406+
407+
ctx.log(i"computing outerpath to $toCls from ${ctx.outersIterator.map(_.owner).toList}")
408+
loop(start, count)
409+
catch case ex: ClassCastException =>
407410
throw new ClassCastException(i"no path exists from ${ctx.owner.enclosingClass} to $toCls")
408-
}
409411

410412
/** The outer parameter definition of a constructor if it needs one */
411413
def paramDefs(constr: Symbol): List[ValDef] =

0 commit comments

Comments
 (0)