Skip to content

Commit

Permalink
Mitigate infinite recursion on canDirectlyPromote
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Apr 29, 2021
1 parent dffcc6e commit e2b758e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ object Checking {
/// A potential can be (currently) directly promoted if and only if:
/// - `pot == this` and all fields of this are initialized, or
/// - `pot == Warm(C, outer)` where `outer` can be directly promoted.
private def canDirectlyPromote(pot: Potential)(using state: State): Boolean = trace("checking direct promotion of " + pot.show, init) {
private def canDirectlyPromote(pot: Potential, visited: Set[Potential] = Set.empty)(using state: State): Boolean = trace("checking direct promotion of " + pot.show, init) {
if (state.safePromoted.contains(pot)) true
// If this potential's promotion depends on itself, we cannot directly promote it.
else if (visited.contains(pot)) false
else pot match {
case pot: ThisRef =>
// If we have all fields initialized, then we can promote This to hot.
Expand All @@ -286,7 +288,7 @@ object Checking {
val summary = expand(pot)
if (!summary.effs.isEmpty)
false // max depth of expansion reached
else summary.pots.forall(canDirectlyPromote)
else summary.pots.forall(canDirectlyPromote(_, visited + pot))
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/init/pos/early-promote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ class A { // checking A
List(b) // Direct promotion works here
val af = 42
}

class RecursiveF {
val a = f
def f: RecursiveF = f
class B(x: Int)

println(new a.B(5))
val n = 10
}

0 comments on commit e2b758e

Please sign in to comment.