Skip to content

Commit dae622d

Browse files
author
Emil Ejbyfeldt
committed
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.
1 parent e0c030c commit dae622d

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
@@ -6896,25 +6896,20 @@ object Types extends TypeUtils {
68966896
}
68976897

68986898
class TypeSizeAccumulator(using Context) extends TypeAccumulator[Int] {
6899-
var seen = util.HashSet[Type](initialCapacity = 8)
69006899
def apply(n: Int, tp: Type): Int =
6901-
if seen.contains(tp) then n
6902-
else {
6903-
seen += tp
6904-
tp match {
6905-
case tp: AppliedType =>
6906-
val tpNorm = tp.tryNormalize
6907-
if tpNorm.exists then apply(n, tpNorm)
6908-
else foldOver(n + 1, tp)
6909-
case tp: RefinedType =>
6910-
foldOver(n + 1, tp)
6911-
case tp: TypeRef if tp.info.isTypeAlias =>
6912-
apply(n, tp.superType)
6913-
case tp: TypeParamRef =>
6914-
apply(n, TypeComparer.bounds(tp))
6915-
case _ =>
6916-
foldOver(n, tp)
6917-
}
6900+
tp match {
6901+
case tp: AppliedType =>
6902+
val tpNorm = tp.tryNormalize
6903+
if tpNorm.exists then apply(n, tpNorm)
6904+
else foldOver(n + 1, tp)
6905+
case tp: RefinedType =>
6906+
foldOver(n + 1, tp)
6907+
case tp: TypeRef if tp.info.isTypeAlias =>
6908+
apply(n, tp.superType)
6909+
case tp: TypeParamRef =>
6910+
apply(n, TypeComparer.bounds(tp))
6911+
case _ =>
6912+
foldOver(n, tp)
69186913
}
69196914
}
69206915

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)