Skip to content

Track maximum depth of ASTs being typechecked #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/compiler/scala/tools/nsc/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,20 @@ class Global(var currentSettings: Settings, reporter0: LegacyReporter)
}

def createJavadoc = false

var typerDepth: Int = 0
var maxTyperDepth = 0
final def decTyperDepth(): Unit = {
typerDepth -= 1
}
final def incTyperDepth(): Unit = {
typerDepth += 1
maxTyperDepth = math.max(typerDepth, maxTyperDepth)
}
val typerDepthReportThreshold: Int = Integer.getInteger("scala.typer.ast.depth.threshhold", Int.MaxValue)
scala.sys.addShutdownHook {
println("max typer depth: " + maxTyperDepth)
}
}

object Global {
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Namers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2015,8 +2015,12 @@ trait Namers extends MethodSynthesis {

override def complete(sym: Symbol) = {
lockedCount += 1
val saved = typerDepth
try completeImpl(sym)
finally lockedCount -= 1
finally {
typerDepth = saved
lockedCount -= 1
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5633,6 +5633,13 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
}

def typed(tree: Tree, mode: Mode, pt: Type): Tree = {
incTyperDepth()
if (typerDepth > typerDepthReportThreshold) {
var pos = context.enclMethod.owner.pos
if (pos == NoPosition)
pos = tree.pos
reporter.warning(pos, "AST depth within " + context.owner.enclMethod.pos + " exceeds " + typerDepthReportThreshold + " at " + globalPhase.name )
}
lastTreeToTyper = tree
val statsEnabled = StatisticsStatics.areSomeHotStatsEnabled() && statistics.areHotStatsLocallyEnabled
val startByType = if (statsEnabled) statistics.pushTimer(byTypeStack, byTypeNanos(tree.getClass)) else null
Expand Down Expand Up @@ -5707,6 +5714,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
} finally {
if (shouldPopTypingStack) typingStack.pop(tree)
if (statsEnabled) statistics.popTimer(byTypeStack, startByType)
decTyperDepth()
}
}

Expand Down