Skip to content

Commit 8d036aa

Browse files
committed
Handle & and | types when computing tuple arity
Fixes #15302
1 parent 77bcd0d commit 8d036aa

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
@@ -58,10 +58,13 @@ object TypeUtils {
5858
if (arity < 0) arity else arity + 1
5959
case self: SingletonType =>
6060
if self.termSymbol == defn.EmptyTupleModule then 0 else -1
61-
case self if defn.isTupleClass(self.classSymbol) =>
62-
self.dealias.argInfos.length
61+
case self: AndOrType =>
62+
val arity1 = self.tp1.tupleArity
63+
val arity2 = self.tp2.tupleArity
64+
if arity1 == arity2 then arity1 else -1
6365
case _ =>
64-
-1
66+
if defn.isTupleClass(self.classSymbol) then self.dealias.argInfos.length
67+
else -1
6568
}
6669

6770
/** 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)