Skip to content

Commit b290509

Browse files
committed
Fix inconsistent typeSize for TupleN vs nested pairs
Previously, `typeSize` reported different values for standard `TupleN` types (e.g., `(A, B)`) compared to their equivalent recursive pair encodings (e.g., `A *: B *: EmptyTuple`). This discrepancy occurred because `TupleN` is a flat `AppliedType`, while the nested encoding forms a deeper tree structure. This patch modifies `TypeSizeAccumulator` to canonicalize `TupleN` types into their recursive `*:` representation before calculating the size. This ensures that the size metric is consistent regardless of whether the tuple is represented syntactically or structurally. This change is verified by a new unit test in `TypesTest`, which confirms that both `Tuple3[Int, Boolean, Double]` and its recursive equivalent `Int *: Boolean *: Double *: EmptyTuple` now yield identical `typeSize` values. Fixes #24730
1 parent c82b623 commit b290509

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7125,6 +7125,8 @@ object Types extends TypeUtils {
71257125
var seen = util.HashSet[Type](initialCapacity = 8)
71267126
def apply(n: Int, tp: Type): Int =
71277127
tp match {
7128+
case tp: AppliedType if defn.isTupleNType(tp) =>
7129+
foldOver(n + 1, tp.toNestedPairs)
71287130
case tp: AppliedType =>
71297131
val tpNorm = tp.tryNormalize
71307132
if tpNorm.exists then apply(n, tpNorm)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dotty.tools.dotc.core
2+
3+
import dotty.tools.DottyTest
4+
import dotty.tools.dotc.core.Symbols.defn
5+
import dotty.tools.dotc.core.TypeOps
6+
7+
import org.junit.Test
8+
import org.junit.Assert.assertEquals
9+
10+
class TypesTest extends DottyTest:
11+
12+
@Test def tuple3TypeSize =
13+
val tpe = defn.TupleType(3).nn.appliedTo(List(defn.IntType, defn.BooleanType, defn.DoubleType))
14+
assertEquals(3, tpe.typeSize)
15+
16+
@Test def tuple3ConsTypeSize =
17+
val tpe = TypeOps.nestedPairs(List(defn.IntType, defn.BooleanType, defn.DoubleType))
18+
assertEquals(3, tpe.typeSize)

compiler/test/dotty/tools/dotc/typer/DivergenceChecker.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class DivergenceCheckerTests extends DottyTest {
5050
1,
5151
1,
5252
1,
53-
3,
54-
5
53+
4,
54+
6
5555
)
5656

5757
tpes.lazyZip(expectedSizes).lazyZip(expectedCoveringSets).foreach {

0 commit comments

Comments
 (0)