Skip to content

Commit 6c7cdea

Browse files
committed
(Potentially mis) use 'bounds' to limit recursion
1 parent 8627f7c commit 6c7cdea

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class TypeUtils:
128128
case None => throw new AssertionError("not a tuple")
129129

130130
def namedTupleElementTypesUpTo(bound: Int, normalize: Boolean = true)(using Context): List[(TermName, Type)] =
131+
if bound < 0 then Nil else
131132
(if normalize then self.normalized else self).dealias match
132133
case defn.NamedTuple(nmes, vals) =>
133134
val names = nmes.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil).map(_.dealias).map:
@@ -136,14 +137,14 @@ class TypeUtils:
136137
val values = vals.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil)
137138
names.zip(values)
138139
case tp: TypeProxy =>
139-
tp.superType.namedTupleElementTypesUpTo(bound, normalize)
140+
tp.superType.namedTupleElementTypesUpTo(bound - 1, normalize)
140141
case tp: OrType =>
141-
val lhs = tp.tp1.namedTupleElementTypesUpTo(bound, normalize)
142-
val rhs = tp.tp2.namedTupleElementTypesUpTo(bound, normalize)
142+
val lhs = tp.tp1.namedTupleElementTypesUpTo(bound - 1, normalize)
143+
val rhs = tp.tp2.namedTupleElementTypesUpTo(bound - 1, normalize)
143144
if (lhs.map(_._1) != rhs.map(_._1)) throw TypeError(em"Malformed Union Type: Named Tuple elements must be the same, but $lhs and $rhs were found.")
144145
lhs.zip(rhs).map((lhs, rhs) => (lhs._1, lhs._2 | rhs._2))
145146
case tp: AndType =>
146-
(tp.tp1.namedTupleElementTypesUpTo(bound, normalize), tp.tp2.namedTupleElementTypesUpTo(bound, normalize)) match
147+
(tp.tp1.namedTupleElementTypesUpTo(bound - 1, normalize), tp.tp2.namedTupleElementTypesUpTo(bound - 1, normalize)) match
147148
case (Nil, rhs) => rhs
148149
case (lhs, Nil) => lhs
149150
case (lhs, rhs) =>

0 commit comments

Comments
 (0)