Skip to content

Commit 4cb1cda

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 a362084 commit 4cb1cda

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

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

0 commit comments

Comments
 (0)