Skip to content

Commit 421a9e8

Browse files
authored
Fix handling of cycles in AST traversal in Dependency Analyzer (#1519)
1 parent d94ee55 commit 421a9e8

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinder.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.bazel.rulesscala.dependencyanalyzer
22

3+
import scala.collection.mutable
34
import scala.reflect.io.AbstractFile
45
import scala.tools.nsc.Global
56

@@ -10,6 +11,7 @@ class AstUsedJarFinder(
1011

1112
def findUsedJars: Map[AbstractFile, Global#Position] = {
1213
val jars = collection.mutable.Map[AbstractFile, global.Position]()
14+
val visitedTrees = mutable.Set.empty[Tree]
1315

1416
def recordUse(source: AbstractFile, pos: Position): Unit = {
1517
// We prefer to report locations which have information (e.g.
@@ -94,12 +96,7 @@ class AstUsedJarFinder(
9496
tree.attachments
9597
.get[global.treeChecker.MacroExpansionAttachment]
9698
.foreach { attach =>
97-
// When we explore the original, the original also has
98-
// this attachment. So we should not examine the original
99-
// again if so.
100-
if (attach.expandee != tree) {
101-
fullyExploreTree(attach.expandee)
102-
}
99+
fullyExploreTree(attach.expandee)
103100
}
104101

105102
val shouldExamine =
@@ -133,7 +130,11 @@ class AstUsedJarFinder(
133130
}
134131
}
135132

136-
tree.foreach(visitNode)
133+
// handle possible cycles in macro expandees
134+
if (!visitedTrees.contains(tree)) {
135+
visitedTrees += tree
136+
tree.foreach(visitNode)
137+
}
137138
}
138139

139140
currentRun.units.foreach { unit =>

0 commit comments

Comments
 (0)