Skip to content

Avoid exponential retries on inlining overflow #12139

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

Merged
merged 1 commit into from
Apr 20, 2021
Merged
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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,9 @@ object Contexts {

private[core] var denotTransformers: Array[DenotTransformer] = _

/** Flag to suppress inlining, set after overflow */
private[dotc] var stopInlining: Boolean = false

// Reporters state
private[dotc] var indent: Int = 0

Expand Down
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ object Inliner {
|| (ctx.phase == Phases.typerPhase && needsTransparentInlining(tree))
)
&& !ctx.typer.hasInliningErrors
&& !ctx.base.stopInlining
}

private def needsTransparentInlining(tree: Tree)(using Context): Boolean =
Expand Down Expand Up @@ -140,6 +141,7 @@ object Inliner {
val body = bodyToInline(tree.symbol) // can typecheck the tree and thereby produce errors
new Inliner(tree, body).inlined(tree.srcPos)
else
ctx.base.stopInlining = true
val (reason, setting) =
if reachedInlinedTreesLimit then ("inlined trees", ctx.settings.XmaxInlinedTrees)
else ("successive inlines", ctx.settings.XmaxInlines)
Expand All @@ -150,6 +152,10 @@ object Inliner {
|You can use ${setting.name} to change the limit.""",
(tree :: enclosingInlineds).last.srcPos
)
if ctx.base.stopInlining && enclosingInlineds.isEmpty then
ctx.base.stopInlining = false
// we have completely backed out of the call that overflowed;
// reset so that further inline calls can be expanded
tree2
}

Expand Down
15 changes: 15 additions & 0 deletions tests/neg/i12116.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import compiletime.erasedValue

object test1:

transparent inline def length[T]: Int =
erasedValue[T] match
case _: (h *: t) => 1 + length[t]
case _: EmptyTuple => 0

transparent inline def foo(): Int = 1 + foo()

val y = length[(1, 2, 3)] // error
val x = foo() // error


15 changes: 15 additions & 0 deletions tests/neg/i12116a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import compiletime.erasedValue

object test1:

inline def length[T]: Int =
erasedValue[T] match
case _: (h *: t) => 1 + length[t]
case _: EmptyTuple => 0

inline def foo(): Int = 1 + foo()

val y = length[(1, 2, 3)] // error
val x = foo() // error