Skip to content

Commit 86c7620

Browse files
committed
Make pathTo operation more robust
Paths can contain non-tree elements. I observed a path that contained an untpd.Mod instead of a tree. Also, we should survive possible cyclic references when ccomputing the context of a path. (this was also observed in the wild but I can't reproduce it).
1 parent 4efe998 commit 86c7620

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ object Interactive {
344344
if (tree.pos.contains(pos)) {
345345
// FIXME: We shouldn't need a cast. Change NavigateAST.pathTo to return a List of Tree?
346346
val path = NavigateAST.pathTo(pos, tree, skipZeroExtent = true).asInstanceOf[List[untpd.Tree]]
347-
path.dropWhile(!_.hasType).asInstanceOf[List[tpd.Tree]]
347+
path.dropWhile(!_.hasType) collect { case t: tpd.Tree @unchecked => t }
348348
}
349349
else Nil
350350

@@ -365,7 +365,7 @@ object Interactive {
365365
case nested :: encl :: rest =>
366366
import typer.Typer._
367367
val outer = contextOfPath(encl :: rest)
368-
encl match {
368+
try encl match {
369369
case tree @ PackageDef(pkg, stats) =>
370370
assert(tree.symbol.exists)
371371
if (nested `eq` pkg) outer
@@ -401,6 +401,9 @@ object Interactive {
401401
case _ =>
402402
outer
403403
}
404+
catch {
405+
case ex: CyclicReference => outer
406+
}
404407
}
405408

406409
/** The first tree in the path that is a definition. */

0 commit comments

Comments
 (0)