Skip to content

Commit

Permalink
Allow Tuple.Head and Tuple.Tail to work with EmptyTuple (#17189)
Browse files Browse the repository at this point in the history
Fixes #17186. 

I think this is correct behaviour since we already allow things like

```Scala
val a = List(1).tail
val b = Tuple1(1).tail
```
  • Loading branch information
nicolasstucki authored Dec 7, 2023
2 parents 86565a4 + 85dc04b commit d09bfda
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions compiler/test/dotc/pos-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions tests/neg/i11982a.scala → tests/pos/i11982a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions tests/pos/i17186a.scala
Original file line number Diff line number Diff line change
@@ -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]]] = ???
5 changes: 5 additions & 0 deletions tests/pos/i17186b.scala
Original file line number Diff line number Diff line change
@@ -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]]

0 comments on commit d09bfda

Please sign in to comment.