Skip to content

Commit f4b4983

Browse files
Use explicit bound in recursive match type
1 parent fccc8c2 commit f4b4983

File tree

7 files changed

+30
-24
lines changed

7 files changed

+30
-24
lines changed

library/src/scala/Tuple.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ sealed trait Tuple extends Any {
7575
object Tuple {
7676

7777
/** Type of the head of a tuple */
78-
type Head[X <: NonEmptyTuple] = X match {
78+
type Head[X <: NonEmptyTuple] <: Any = X match {
7979
case x *: _ => x
8080
}
8181

@@ -91,12 +91,14 @@ object Tuple {
9191
}
9292

9393
/** Type of the element a position N in the tuple X */
94-
type Elem[X <: Tuple, N <: Int] = X match {
94+
type Elem[X <: Tuple, N <: Int] <: Any = X match {
9595
case x *: xs =>
96-
N match {
97-
case 0 => x
98-
case S[n1] => Elem[xs, n1]
99-
}
96+
Elem0[x, xs, N]
97+
}
98+
99+
type Elem0[x, xs, N] <: Any = N match {
100+
case 0 => x
101+
case S[n1] => Elem[xs, n1]
100102
}
101103

102104
/** Literal constant Int size of a tuple */
@@ -139,19 +141,23 @@ object Tuple {
139141
/** Transforms a tuple `(T1, ..., Tn)` into `(T1, ..., Ti)`. */
140142
type Take[T <: Tuple, N <: Int] <: Tuple = N match {
141143
case 0 => Unit
142-
case S[n1] => T match {
143-
case Unit => Unit
144-
case x *: xs => x *: Take[xs, n1]
145-
}
144+
case S[n1] => Take0[T, n1]
145+
}
146+
147+
type Take0[T, n1] <: Tuple = T match {
148+
case Unit => Unit
149+
case x *: xs => x *: Take[xs, n1]
146150
}
147151

148152
/** Transforms a tuple `(T1, ..., Tn)` into `(Ti+1, ..., Tn)`. */
149153
type Drop[T <: Tuple, N <: Int] <: Tuple = N match {
150154
case 0 => T
151-
case S[n1] => T match {
152-
case Unit => Unit
153-
case x *: xs => Drop[xs, n1]
154-
}
155+
case S[n1] => Drop0[T, n1]
156+
}
157+
158+
type Drop0[T, n1] <: Tuple = T match {
159+
case Unit => Unit
160+
case x *: xs => Drop[xs, n1]
155161
}
156162

157163
/** Splits a tuple (T1, ..., Tn) into a pair of two tuples `(T1, ..., Ti)` and

tests/neg-custom-args/matchtype-loop.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
object Test {
2-
type L[X] = X match {
2+
type L[X] <: Any = X match {
33
case Int => L[X]
44
}
5-
type LL[X] = X match {
5+
type LL[X] <: Any = X match {
66
case Int => LL[LL[X]]
77
}
88
def a: L[Boolean] = ???

tests/neg/matchtype-loop2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
object Test {
2-
type L[X] = X match {
2+
type L[X] <: Any = X match {
33
case Int => L[X]
44
}
5-
type LL[X] = X match { // error: recursion limit exceeded
5+
type LL[X] <: Any = X match { // error: recursion limit exceeded
66
case Int => LL[LL[X]]
77
}
88
}

tests/pos/6322.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Test {
77
def apply(x: T1): Unit
88
}
99

10-
type F[N] = N match {
10+
type F[N] <: Any = N match {
1111
case A => F1[String]
1212
case B => F[A]
1313
case C => F[B]

tests/pos/6362.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Test {
2-
type LeafElem[X] = X match {
2+
type LeafElem[X] <: Any = X match {
33
case String => Char
44
case Array[t] => LeafElem[t]
55
case Iterable[t] => LeafElem[t]

tests/pos/i5625.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test {
22

3-
type LeafElem[X] = X match {
3+
type LeafElem[X] <: Any = X match {
44
case String => Char
55
case Array[t] => LeafElem[t]
66
case Iterable[t] => LeafElem[t]
@@ -11,4 +11,4 @@ object Test {
1111
summon[LeafElem[Array[Int]] =:= Int]
1212
summon[LeafElem[Iterable[Int]] =:= Int]
1313
summon[LeafElem[Int] =:= Int]
14-
}
14+
}

tests/pos/i5625b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object Test {
22

33
type AV[t <: AnyVal] = t
44

5-
type LeafElem[X] = X match {
5+
type LeafElem[X] <: Any = X match {
66
case String => Char
77
case Array[t] => LeafElem[t]
88
case Iterable[t] => LeafElem[t]
@@ -13,4 +13,4 @@ object Test {
1313
summon[LeafElem[Array[Int]] =:= Int]
1414
summon[LeafElem[Iterable[Int]] =:= Int]
1515
summon[LeafElem[Int] =:= Int]
16-
}
16+
}

0 commit comments

Comments
 (0)