Skip to content

Commit a3e69f1

Browse files
Merge pull request #9035 from dotty-staging/make-NonEmptyTuple-Product
Make NonEmptyTuple a Product
2 parents d853348 + 31b7a65 commit a3e69f1

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ object Applications {
116116
if (sel.exists) sel :: tupleSelectors(n + 1, tp) else Nil
117117
}
118118
def genTupleSelectors(n: Int, tp: Type): List[Type] = tp match {
119-
case tp: AppliedType if !tp.derivesFrom(defn.ProductClass) && tp.derivesFrom(defn.PairClass) =>
119+
case tp: AppliedType if !defn.isTupleClass(tp.typeSymbol) && tp.derivesFrom(defn.PairClass) =>
120120
val List(head, tail) = tp.args
121121
head :: genTupleSelectors(n, tail)
122122
case _ => tupleSelectors(n, tp)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,9 @@ class Typer extends Namer
11471147

11481148
val desugared =
11491149
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
1150-
val isGenericTuple = !protoFormals.head.derivesFrom(defn.ProductClass)
1150+
val isGenericTuple =
1151+
protoFormals.head.derivesFrom(defn.TupleClass)
1152+
&& !defn.isTupleClass(protoFormals.head.typeSymbol)
11511153
desugar.makeTupledFunction(params, fnBody, isGenericTuple)
11521154
}
11531155
else {

library/src/scala/Tuple.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ object Tuple {
186186
}
187187

188188
/** Tuple of arbitrary non-zero arity */
189-
sealed trait NonEmptyTuple extends Tuple {
189+
sealed trait NonEmptyTuple extends Tuple with Product {
190190
import Tuple._
191191

192192
/** Get the i-th element of this tuple.

tests/run/tuple-product.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@main def Test = {
2+
val a: Product = 1 *: ()
3+
assert(a.productArity == 1)
4+
val b: Product = 1 *: 2 *: ()
5+
assert(b.productArity == 2)
6+
val c: Product = 1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: 22 *: 23 *: 24 *: 25 *: ()
7+
assert(c.productArity == 25)
8+
val d: NonEmptyTuple = (1, 2)
9+
val e: Product = d
10+
assert(e.productArity == 2)
11+
}

0 commit comments

Comments
 (0)