Skip to content

Commit a1ee57d

Browse files
committed
Merge pull request scala#4178 from retronym/ticket/9018
SI-9018 Fix regression: cycle in LUBs
2 parents 56b9f06 + 081b82f commit a1ee57d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/reflect/scala/reflect/internal/Depth.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ final class Depth private (val depth: Int) extends AnyVal with Ordered[Depth] {
2121

2222
object Depth {
2323
// A don't care value for the depth parameter in lubs/glbs and related operations.
24-
final val AnyDepth = new Depth(Int.MinValue)
24+
// When passed this value, the recursion budget will be inferred from the shape of
25+
// the `typeDepth` of the list of types.
26+
final val AnyDepthValue = -3
27+
final val AnyDepth = new Depth(AnyDepthValue)
28+
2529
final val Zero = new Depth(0)
2630

27-
@inline final def apply(depth: Int): Depth = if (depth < 0) AnyDepth else new Depth(depth)
31+
// SI-9018: A negative depth is used to signal that we have breached the recursion limit.
32+
// The LUB/GLB implementation will then truncate to Any/Nothing.
33+
//
34+
// We only really need one of these, but we allow representation of Depth(-1) and Depth(-2)
35+
// to mimic the historical choice of 2.10.4.
36+
@inline final def apply(depth: Int): Depth = {
37+
if (depth < AnyDepthValue) AnyDepth
38+
else new Depth(depth)
39+
}
2840
}

test/files/pos/t9018.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object TestObject {
2+
3+
def m(i: Int): AnyRef = i match {
4+
case 0 => new C()
5+
case 1 => Some(E.A).getOrElse("")
6+
}
7+
8+
class C extends Ordered[C] {
9+
def compare(that: C): Int = ???
10+
}
11+
12+
object E extends Enumeration {
13+
type CharacterClass = Value
14+
val A = Value
15+
}
16+
}

0 commit comments

Comments
 (0)