Skip to content

Commit 0bd4b15

Browse files
committed
Do not use LazyList in CheckUnused.
It is not efficient when the results are always used exactly once.
1 parent 69664f7 commit 0bd4b15

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.dotc.transform
22

3+
import scala.annotation.tailrec
4+
35
import dotty.tools.dotc.ast.tpd
46
import dotty.tools.dotc.core.Symbols.*
57
import dotty.tools.dotc.ast.tpd.{Inlined, TreeTraverser}
@@ -88,11 +90,17 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
8890

8991
override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
9092
if tree.symbol.exists then
91-
val prefixes = LazyList.iterate(tree.typeOpt.normalizedPrefix)(_.normalizedPrefix).takeWhile(_ != NoType)
92-
.take(10) // Failsafe for the odd case if there was an infinite cycle
93-
for prefix <- prefixes do
94-
unusedDataApply(_.registerUsed(prefix.classSymbol, None))
95-
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
93+
unusedDataApply { ud =>
94+
@tailrec
95+
def loopOnNormalizedPrefixes(prefix: Type, depth: Int): Unit =
96+
// limit to 10 as failsafe for the odd case where there is an infinite cycle
97+
if depth < 10 && prefix.exists then
98+
ud.registerUsed(prefix.classSymbol, None)
99+
loopOnNormalizedPrefixes(prefix.normalizedPrefix, depth + 1)
100+
101+
loopOnNormalizedPrefixes(tree.typeOpt.normalizedPrefix, depth = 0)
102+
ud.registerUsed(tree.symbol, Some(tree.name))
103+
}
96104
else if tree.hasType then
97105
unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
98106
else

0 commit comments

Comments
 (0)