Skip to content

Commit a00e629

Browse files
authored
Only check seen for LazyRef for TypeSizeAccumulator (#20459)
2 parents 8ee54c3 + 8a0bbdf commit a00e629

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7046,23 +7046,24 @@ object Types extends TypeUtils {
70467046
class TypeSizeAccumulator(using Context) extends TypeAccumulator[Int] {
70477047
var seen = util.HashSet[Type](initialCapacity = 8)
70487048
def apply(n: Int, tp: Type): Int =
7049-
if seen.contains(tp) then n
7050-
else {
7051-
seen += tp
7052-
tp match {
7053-
case tp: AppliedType =>
7054-
val tpNorm = tp.tryNormalize
7055-
if tpNorm.exists then apply(n, tpNorm)
7056-
else foldOver(n + 1, tp)
7057-
case tp: RefinedType =>
7058-
foldOver(n + 1, tp)
7059-
case tp: TypeRef if tp.info.isTypeAlias =>
7060-
apply(n, tp.superType)
7061-
case tp: TypeParamRef =>
7062-
apply(n, TypeComparer.bounds(tp))
7063-
case _ =>
7049+
tp match {
7050+
case tp: AppliedType =>
7051+
val tpNorm = tp.tryNormalize
7052+
if tpNorm.exists then apply(n, tpNorm)
7053+
else foldOver(n + 1, tp)
7054+
case tp: RefinedType =>
7055+
foldOver(n + 1, tp)
7056+
case tp: TypeRef if tp.info.isTypeAlias =>
7057+
apply(n, tp.superType)
7058+
case tp: TypeParamRef =>
7059+
apply(n, TypeComparer.bounds(tp))
7060+
case tp: LazyRef =>
7061+
if seen.contains(tp) then n
7062+
else
7063+
seen += tp
70647064
foldOver(n, tp)
7065-
}
7065+
case _ =>
7066+
foldOver(n, tp)
70667067
}
70677068
}
70687069

tests/neg/i15692.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait TC[X]
2+
object TC {
3+
given [T, S <: TC[S]](using TC[S]): TC[T] = ???
4+
summon[TC[Int]] // error
5+
}

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)