From 85dc04bdfa14c2f6cfc8fc71ea5968d11690ab93 Mon Sep 17 00:00:00 2001 From: Decel <8268812+Decel@users.noreply.github.com> Date: Thu, 30 Mar 2023 21:58:00 +0200 Subject: [PATCH] Allow Tuple.Head and Tuple.Tail to accept any Tuple --- compiler/test/dotc/pos-test-pickling.blacklist | 2 ++ library/src/scala/Tuple.scala | 4 ++-- tests/{neg => pos}/i11982a.scala | 8 ++++---- tests/pos/i17186a.scala | 5 +++++ tests/pos/i17186b.scala | 5 +++++ 5 files changed, 18 insertions(+), 6 deletions(-) rename tests/{neg => pos}/i11982a.scala (59%) create mode 100644 tests/pos/i17186a.scala create mode 100644 tests/pos/i17186b.scala diff --git a/compiler/test/dotc/pos-test-pickling.blacklist b/compiler/test/dotc/pos-test-pickling.blacklist index eb4b861eb324..b0da78f0a1eb 100644 --- a/compiler/test/dotc/pos-test-pickling.blacklist +++ b/compiler/test/dotc/pos-test-pickling.blacklist @@ -27,6 +27,8 @@ i9804.scala i13433.scala i16649-irrefutable.scala strict-pattern-bindings-3.0-migration.scala +i17186b.scala +i11982a.scala # Tree is huge and blows stack for printing Text i7034.scala diff --git a/library/src/scala/Tuple.scala b/library/src/scala/Tuple.scala index 6993b8202082..ae46f7fbf347 100644 --- a/library/src/scala/Tuple.scala +++ b/library/src/scala/Tuple.scala @@ -96,7 +96,7 @@ object Tuple { } /** Type of the head of a tuple */ - type Head[X <: NonEmptyTuple] = X match { + type Head[X <: Tuple] = X match { case x *: _ => x } @@ -108,7 +108,7 @@ object Tuple { } /** Type of the tail of a tuple */ - type Tail[X <: NonEmptyTuple] <: Tuple = X match { + type Tail[X <: Tuple] <: Tuple = X match { case _ *: xs => xs } diff --git a/tests/neg/i11982a.scala b/tests/pos/i11982a.scala similarity index 59% rename from tests/neg/i11982a.scala rename to tests/pos/i11982a.scala index 53126eaf3986..b3c68443ff04 100644 --- a/tests/neg/i11982a.scala +++ b/tests/pos/i11982a.scala @@ -6,9 +6,9 @@ object Unpair { def unpair[X <: Tuple2[?, ?]]( using a: ValueOf[Tuple.Head[X]], - b: ValueOf[Tuple.Head[Tuple.Tail[X]]] // error - ): Tuple2[Tuple.Head[X], Tuple.Head[Tuple.Tail[X]]] = // error + b: ValueOf[Tuple.Head[Tuple.Tail[X]]] + ): Tuple2[Tuple.Head[X], Tuple.Head[Tuple.Tail[X]]] = type AA = Tuple.Head[X] - type BB = Tuple.Head[Tuple.Tail[X]] // error + type BB = Tuple.Head[Tuple.Tail[X]] pair[AA, BB](using a, b) -} \ No newline at end of file +} diff --git a/tests/pos/i17186a.scala b/tests/pos/i17186a.scala new file mode 100644 index 000000000000..7160ac001ba8 --- /dev/null +++ b/tests/pos/i17186a.scala @@ -0,0 +1,5 @@ +type second[X <: Tuple2[Any, Any]] = Tuple.Head[Tuple.Tail[X]] +type middle[X <: Tuple3[Any, Any, Any]] = Tuple.Head[Tuple.Tail[X]] + +val a: Tuple.Head[Tuple.Tail[Tuple2[Int, String]]] = ??? +val b: Tuple.Head[Tuple.Tail[Tuple3[Int, String, Boolean]]] = ??? diff --git a/tests/pos/i17186b.scala b/tests/pos/i17186b.scala new file mode 100644 index 000000000000..36efd647816c --- /dev/null +++ b/tests/pos/i17186b.scala @@ -0,0 +1,5 @@ +type SecondOfTwo[X <: Tuple2[Any, Any]] = Tuple.Head[Tuple.Tail[X]] +val a = implicitly[SecondOfTwo[Tuple2[Int, String]] =:= String] + +type LastOfThree[X <: Tuple3[Any, Any, Any]] = Tuple.Tail[Tuple.Tail[X]] +val b = implicitly[LastOfThree[Tuple3[Int, String, Boolean]] =:= Tuple1[Boolean]]