Skip to content

Commit 4f31f01

Browse files
Emil Ejbyfeldttgodzik
authored andcommitted
Remove seen from TypeSizeAccumulator
This fixes scala#15692 and does not seem to break an existing compilation tests. The problem with seen when it comes scala#15692 is that seen means that type that has repeated types will get a lower size and this incorrectly triggers the divergence check since some of the steps involved less repeated types. The seen logic was introduced in scala#6329 and the motivation was to deal with F-bounds. Since not tests fail it not clear if this logic is still needed to deal with F-bounds? If it is still needed we can add a test and instead of removing the seen logic we can make it track only types the appear as a bound and could cause infinite recursion instead of tracking all. [Cherry-picked c2ef180]
1 parent 4de57e7 commit 4f31f01

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6585,25 +6585,20 @@ object Types extends TypeUtils {
65856585
}
65866586

65876587
class TypeSizeAccumulator(using Context) extends TypeAccumulator[Int] {
6588-
var seen = util.HashSet[Type](initialCapacity = 8)
65896588
def apply(n: Int, tp: Type): Int =
6590-
if seen.contains(tp) then n
6591-
else {
6592-
seen += tp
6593-
tp match {
6594-
case tp: AppliedType =>
6595-
val tpNorm = tp.tryNormalize
6596-
if tpNorm.exists then apply(n, tpNorm)
6597-
else foldOver(n + 1, tp)
6598-
case tp: RefinedType =>
6599-
foldOver(n + 1, tp)
6600-
case tp: TypeRef if tp.info.isTypeAlias =>
6601-
apply(n, tp.superType)
6602-
case tp: TypeParamRef =>
6603-
apply(n, TypeComparer.bounds(tp))
6604-
case _ =>
6605-
foldOver(n, tp)
6606-
}
6589+
tp match {
6590+
case tp: AppliedType =>
6591+
val tpNorm = tp.tryNormalize
6592+
if tpNorm.exists then apply(n, tpNorm)
6593+
else foldOver(n + 1, tp)
6594+
case tp: RefinedType =>
6595+
foldOver(n + 1, tp)
6596+
case tp: TypeRef if tp.info.isTypeAlias =>
6597+
apply(n, tp.superType)
6598+
case tp: TypeParamRef =>
6599+
apply(n, TypeComparer.bounds(tp))
6600+
case _ =>
6601+
foldOver(n, tp)
66076602
}
66086603
}
66096604

tests/pos/i15692.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
sealed trait Nat
2+
sealed trait Succ[Prev <: Nat] extends Nat
3+
sealed trait Zero extends Nat
4+
5+
class Sum[M <: Nat, N <: Nat] {
6+
type Out <: Nat
7+
}
8+
9+
object Sum {
10+
type Aux[M <: Nat, N <: Nat, R <: Nat] = Sum[M, N] { type Out = R }
11+
12+
implicit def sum0[N <: Nat]: Sum.Aux[Zero, N, N] = new Sum[Zero, N] { type Out = N }
13+
implicit def sum1[M <: Nat, N <: Nat, R <: Nat](implicit sum: Sum.Aux[M, Succ[N], R]): Sum.Aux[Succ[M], N, R] =
14+
new Sum[Succ[M], N] { type Out = R }
15+
}
16+
17+
object Test {
18+
def main(args: Array[String]): Unit = {
19+
type _3 = Succ[Succ[Succ[Zero]]]
20+
type _5 = Succ[Succ[_3]]
21+
22+
implicitly[Sum[_3, _5]]
23+
}
24+
}

0 commit comments

Comments
 (0)