Skip to content

Commit a6e6c64

Browse files
committed
Handle & and | types when computing tuple arity
Fixes #15302
1 parent 707f9cb commit a6e6c64

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ object TypeUtils {
6262
if self.termSymbol == defn.EmptyTupleModule then 0 else -1
6363
case self: TypeRef if relaxEmptyTuple && self.classSymbol == defn.EmptyTupleModule.moduleClass =>
6464
0
65-
case self if defn.isTupleClass(self.classSymbol) =>
66-
self.dealias.argInfos.length
65+
case self: AndOrType =>
66+
val arity1 = self.tp1.tupleArity(relaxEmptyTuple)
67+
val arity2 = self.tp2.tupleArity(relaxEmptyTuple)
68+
if arity1 == arity2 then arity1 else -1
6769
case _ =>
68-
-1
70+
if defn.isTupleClass(self.classSymbol) then self.dealias.argInfos.length
71+
else -1
6972
}
7073

7174
/** The element types of this tuple type, which can be made up of EmptyTuple, TupleX and `*:` pairs */

tests/run/i15302a.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
inline def flatConcat2[A, B](a: A, b: B) =
2+
b match
3+
case bs: *:[bh, bt] => a *: bs
4+
5+
@main def Test =
6+
val x: (Int, Int, Int) = flatConcat2(1, (2,3))

tests/run/i15302b.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@main def Test =
2+
val tup2: Any = (1, 2)
3+
val x1: (Int, Int) = tup2.asInstanceOf[(Int *: Int *: EmptyTuple) & (Int, Int)]
4+
val x2: (Int, Int) = tup2.asInstanceOf[(Int *: Int *: Tuple) & (Int, Int)]
5+
val x3: (Int, Int) = tup2.asInstanceOf[(Int *: Int *: EmptyTuple) | (Int, Int)]
6+
val x4: Tuple = tup2.asInstanceOf[(Int *: Int *: Tuple) | (Int, Int)]
7+
8+
val tup3: Any = (1, 2, 3)
9+
val x5: (Int, Int, Int) = tup3.asInstanceOf[Int *: ((Int *: Int *: EmptyTuple) & (Int, Int))]
10+
val x6: (Int, Int, Int) = tup3.asInstanceOf[Int *: ((Int *: Int *: Tuple) & (Int, Int))]
11+
val x7: (Int, Int, Int) = tup3.asInstanceOf[Int *: ((Int *: Int *: EmptyTuple) | (Int, Int))]
12+
val x8: Tuple = tup3.asInstanceOf[Int *: ((Int *: Int *: Tuple) | (Int, Int))]

0 commit comments

Comments
 (0)