From 31b7a658ec3e34ecd21281acbd374b65d0fcf422 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 25 May 2020 11:41:28 +0200 Subject: [PATCH] Make NonEmptyTuple a Product --- .../src/dotty/tools/dotc/typer/Applications.scala | 2 +- compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 +++- library/src/scala/Tuple.scala | 2 +- tests/run/tuple-product.scala | 11 +++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 tests/run/tuple-product.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 90b89ac04ae6..c4cd1d95f0b9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -116,7 +116,7 @@ object Applications { if (sel.exists) sel :: tupleSelectors(n + 1, tp) else Nil } def genTupleSelectors(n: Int, tp: Type): List[Type] = tp match { - case tp: AppliedType if !tp.derivesFrom(defn.ProductClass) && tp.derivesFrom(defn.PairClass) => + case tp: AppliedType if !defn.isTupleClass(tp.typeSymbol) && tp.derivesFrom(defn.PairClass) => val List(head, tail) = tp.args head :: genTupleSelectors(n, tail) case _ => tupleSelectors(n, tp) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 63f7682a3003..890059da151c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1147,7 +1147,9 @@ class Typer extends Namer val desugared = if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) { - val isGenericTuple = !protoFormals.head.derivesFrom(defn.ProductClass) + val isGenericTuple = + protoFormals.head.derivesFrom(defn.TupleClass) + && !defn.isTupleClass(protoFormals.head.typeSymbol) desugar.makeTupledFunction(params, fnBody, isGenericTuple) } else { diff --git a/library/src/scala/Tuple.scala b/library/src/scala/Tuple.scala index 3575dcb6c628..e01f46df318a 100644 --- a/library/src/scala/Tuple.scala +++ b/library/src/scala/Tuple.scala @@ -186,7 +186,7 @@ object Tuple { } /** Tuple of arbitrary non-zero arity */ -sealed trait NonEmptyTuple extends Tuple { +sealed trait NonEmptyTuple extends Tuple with Product { import Tuple._ /** Get the i-th element of this tuple. diff --git a/tests/run/tuple-product.scala b/tests/run/tuple-product.scala new file mode 100644 index 000000000000..4441ee75f1af --- /dev/null +++ b/tests/run/tuple-product.scala @@ -0,0 +1,11 @@ +@main def Test = { + val a: Product = 1 *: () + assert(a.productArity == 1) + val b: Product = 1 *: 2 *: () + assert(b.productArity == 2) + 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 *: () + assert(c.productArity == 25) + val d: NonEmptyTuple = (1, 2) + val e: Product = d + assert(e.productArity == 2) +} \ No newline at end of file