Skip to content

Commit df7579f

Browse files
Add space complexity tracker for #9203
1 parent 4b3fdf6 commit df7579f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2861,7 +2861,38 @@ class Typer extends Namer
28612861
final def adapt(tree: Tree, pt: Type)(using Context): Tree =
28622862
adapt(tree, pt, ctx.typerState.ownedVars)
28632863

2864-
private def adapt1(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean)(using Context): Tree = {
2864+
// See https://github.com/lampepfl/dotty/issues/9203 for why this is needed
2865+
// So far it is guarding only `adapt1` since that's the highest known entry point
2866+
// stack call where the detection for the debugging of #9203 is possible.
2867+
private inline def spaceComplexityTracker[T](tree: Tree)(task: => T)(using Context) =
2868+
def size = ctx.base.uniquesSizes.values
2869+
.map { case (size, _, _) => size }.sum
2870+
val before = size
2871+
val res = task
2872+
val after = size
2873+
val delta = after - before
2874+
if delta > 100000
2875+
println( // ctx.warning does not output the message in this place, probably waits
2876+
// for the checking to finish – which never happens under the conditions
2877+
// `spaceComplexityTracker` was designed to track and report.
2878+
s"""
2879+
|[${Console.YELLOW}warn${Console.RESET}] Created ${delta} unique type objects on typecheck of the following tree:
2880+
|
2881+
|${tree.show}
2882+
|
2883+
|The meaning of this is that the compiler is doing an unreasonable amount
2884+
|of work while compiling your code. The compilation may take a lot of time
2885+
|and heap space. This situation can happen, e.g., when the lookup space for
2886+
|givens is too large. To resolve the problem, try to eliminate the offending
2887+
|code snippet from your program and rephrase your program using fewer advanced
2888+
|features.
2889+
""".stripMargin)
2890+
ctx.reporter.flush()
2891+
end if
2892+
res
2893+
end spaceComplexityTracker
2894+
2895+
private def adapt1(tree: Tree, pt: Type, locked: TypeVars, tryGadtHealing: Boolean)(using Context): Tree = spaceComplexityTracker(tree) {
28652896
assert(pt.exists && !pt.isInstanceOf[ExprType] || ctx.reporter.errorsReported)
28662897
def methodStr = err.refStr(methPart(tree).tpe)
28672898

0 commit comments

Comments
 (0)