Skip to content

Commit

Permalink
Fix handling of cycles in AST traversal in Dependency Analyzer (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaidas Pilkauskas committed Oct 17, 2023
1 parent d94ee55 commit 421a9e8
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.bazel.rulesscala.dependencyanalyzer

import scala.collection.mutable
import scala.reflect.io.AbstractFile
import scala.tools.nsc.Global

Expand All @@ -10,6 +11,7 @@ class AstUsedJarFinder(

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

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

val shouldExamine =
Expand Down Expand Up @@ -133,7 +130,11 @@ class AstUsedJarFinder(
}
}

tree.foreach(visitNode)
// handle possible cycles in macro expandees
if (!visitedTrees.contains(tree)) {
visitedTrees += tree
tree.foreach(visitNode)
}
}

currentRun.units.foreach { unit =>
Expand Down

0 comments on commit 421a9e8

Please sign in to comment.