Skip to content

Commit f616cae

Browse files
committed
Try just disallowing allowing widening abstract types
1 parent 96d4ccd commit f616cae

12 files changed

+89
-73
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -3134,10 +3134,8 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
31343134
case _ =>
31353135
MatchResult.Reduced(body)
31363136

3137-
if caseLambda.exists && matches(canWidenAbstract = false) then
3137+
if matches(canWidenAbstract = false) then
31383138
redux(canApprox = true)
3139-
else if matches(canWidenAbstract = true) then
3140-
redux(canApprox = false)
31413139
else if (provablyDisjoint(scrut, pat))
31423140
// We found a proof that `scrut` and `pat` are incompatible.
31433141
// The search continues.

tests/neg/11982.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ type Head[X] = X match {
44
}
55

66
object Unpair {
7-
def unpair[X <: Tuple2[Any, Any]]: Head[X] = 1
8-
unpair[Tuple2["msg", 42]]: "msg" // error
7+
def unpair[X <: Tuple2[Any, Any]]: Head[X] = 1 // error
8+
unpair[Tuple2["msg", 42]]: "msg"
99
}
1010

1111

@@ -14,8 +14,8 @@ type Head2[X] = X match {
1414
}
1515

1616
object Unpair2 {
17-
def unpair[X <: Tuple2[Tuple2[Any, Any], Tuple2[Any, Any]]]: Head2[X] = 1
18-
unpair[Tuple2[Tuple2["msg", 42], Tuple2[41, 40]]]: "msg" // error
17+
def unpair[X <: Tuple2[Tuple2[Any, Any], Tuple2[Any, Any]]]: Head2[X] = 1 // error
18+
unpair[Tuple2[Tuple2["msg", 42], Tuple2[41, 40]]]: "msg"
1919
}
2020

2121

@@ -35,6 +35,6 @@ type Head4[X] = X match {
3535
}
3636

3737
object Unpair4 {
38-
def unpair[X <: Foo[Any, Any]]: Head4[Foo[X, X]] = 1
39-
unpair[Foo["msg", 42]]: "msg" // error
38+
def unpair[X <: Foo[Any, Any]]: Head4[Foo[X, X]] = 1 // error
39+
unpair[Foo["msg", 42]]: "msg"
4040
}

tests/neg/6570-1.check

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
|
2525
| trying to reduce M[T]
2626
| failed since selector T
27-
| does not uniquely determine parameter x in
28-
| case Cov[x] => N[x]
29-
| The computed bounds for the parameter are:
30-
| x >: Box[Int]
27+
| does not match case Cov[x] => N[x]
28+
| and cannot be shown to be disjoint from it either.
3129
|
3230
| longer explanation available when compiling with `-explain`

tests/neg/i11982.check

-4
This file was deleted.

tests/neg/i11982.scala

-27
This file was deleted.

tests/neg/i11982a.check

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
|
88
| trying to reduce Tuple.Tail[X]
99
| failed since selector X
10-
| does not uniquely determine parameter xs in
11-
| case _ *: xs => xs
12-
| The computed bounds for the parameter are:
13-
| xs >: Any *: EmptyTuple.type <: Tuple
10+
| does not match case _ *: xs => xs
11+
| and cannot be shown to be disjoint from it either.
1412
|
1513
| longer explanation available when compiling with `-explain`
1614
-- [E057] Type Mismatch Error: tests/neg/i11982a.scala:10:38 -----------------------------------------------------------
@@ -22,10 +20,8 @@
2220
|
2321
| trying to reduce Tuple.Tail[X]
2422
| failed since selector X
25-
| does not uniquely determine parameter xs in
26-
| case _ *: xs => xs
27-
| The computed bounds for the parameter are:
28-
| xs >: Any *: EmptyTuple.type <: Tuple
23+
| does not match case _ *: xs => xs
24+
| and cannot be shown to be disjoint from it either.
2925
|
3026
| longer explanation available when compiling with `-explain`
3127
-- [E057] Type Mismatch Error: tests/neg/i11982a.scala:12:25 -----------------------------------------------------------
@@ -37,9 +33,7 @@
3733
|
3834
| trying to reduce Tuple.Tail[X]
3935
| failed since selector X
40-
| does not uniquely determine parameter xs in
41-
| case _ *: xs => xs
42-
| The computed bounds for the parameter are:
43-
| xs >: Any *: EmptyTuple.type <: Tuple
36+
| does not match case _ *: xs => xs
37+
| and cannot be shown to be disjoint from it either.
4438
|
4539
| longer explanation available when compiling with `-explain`

tests/neg/i13780.check

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i13780.scala:12:32 ------------------------------------------------------------
2+
12 | def unpair[X <: Y]: Head[X] = "" // error
3+
| ^^
4+
| Found: ("" : String)
5+
| Required: Head[X]
6+
|
7+
| where: X is a type in method unpair with bounds <: A.this.Y
8+
|
9+
|
10+
| Note: a match type could not be fully reduced:
11+
|
12+
| trying to reduce Head[X]
13+
| failed since selector X
14+
| does not match case (a, b) => a
15+
| and cannot be shown to be disjoint from it either.
16+
|
17+
| longer explanation available when compiling with `-explain`
118
-- [E007] Type Mismatch Error: tests/neg/i13780.scala:18:31 ------------------------------------------------------------
219
18 | def int[X <: Y]: Int = unpair[X] // error
320
| ^^^^^^^^^
@@ -11,11 +28,8 @@
1128
|
1229
| trying to reduce Head[X]
1330
| failed since selector X
14-
| does not uniquely determine parameters a, b in
15-
| case (a, b) => a
16-
| The computed bounds for the parameters are:
17-
| a >: Int
18-
| b >: Int
31+
| does not match case (a, b) => a
32+
| and cannot be shown to be disjoint from it either.
1933
|
2034
| longer explanation available when compiling with `-explain`
2135
-- [E007] Type Mismatch Error: tests/neg/i13780.scala:23:37 ------------------------------------------------------------
@@ -31,10 +45,7 @@
3145
|
3246
| trying to reduce Head[X]
3347
| failed since selector X
34-
| does not uniquely determine parameters a, b in
35-
| case (a, b) => a
36-
| The computed bounds for the parameters are:
37-
| a >: String
38-
| b >: String
48+
| does not match case (a, b) => a
49+
| and cannot be shown to be disjoint from it either.
3950
|
4051
| longer explanation available when compiling with `-explain`

tests/neg/i13780.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait Z {
99

1010
class A extends Z {
1111
type Y <: Tuple2[Any, Any]
12-
def unpair[X <: Y]: Head[X] = ""
12+
def unpair[X <: Y]: Head[X] = "" // error
1313
def any[X <: Y]: Any = unpair[X]
1414
}
1515

tests/pos/i15155.scala renamed to tests/neg/i15155.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ type EnumValue[A <: Enumeration] = A match {
77

88
// https://github.com/json4s/json4s/blob/355d8751391773e0d79d04402a4f9fb7bfc684ec/ext/src/main/scala/org/json4s/ext/EnumSerializer.scala#L25-L26
99
class EnumSerializer[E <: Enumeration: ClassTag](enumeration: E) {
10-
val EnumerationClass = classOf[EnumValue[E]]
11-
}
10+
val EnumerationClass = classOf[EnumValue[E]] // error
11+
}

tests/neg/wildcard-match.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ type ContravToList[X] = X match
2424
class C
2525

2626
def f[X <: Box[C], Y <: Cov[C], Z <: Contrav[C]] =
27-
def a: BoxElem[X] = ??? // OK
28-
val _: C = a
27+
def a: BoxElem[X] = ???
28+
val _: C = a // error
2929

3030
def a1: CovElem[Y] = ???
3131
val _: C = a1 // error
3232

3333
def a2: ContravElem[Z] = ???
3434
val _: C = a2 // error
3535

36-
def b: BoxToList[X] = ??? // OK
37-
val _: List[C] = b
36+
def b: BoxToList[X] = ???
37+
val _: List[C] = b // error
3838

3939
def b1: CovToList[Y] = ???
4040
val _: List[C] = b1 // error

tests/pos/i15926.min.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
sealed trait Nat
2+
final case class Zero() extends Nat
3+
final case class Succ[+N <: Nat]() extends Nat
4+
5+
final case class Neg[+N <: Succ[Nat]]()
6+
7+
type Sum[X, Y] = Y match
8+
case Zero => X
9+
case Succ[y] => Sum[Succ[X], y]
10+
11+
type IntSum[A, B] = B match
12+
case Neg[b] => A match
13+
case Neg[a] => Neg[Sum[a, b]]
14+
15+
type One = Succ[Zero]
16+
type Two = Succ[One]
17+
18+
class Test:
19+
def test() = summon[IntSum[Neg[One], Neg[One]] =:= Neg[Two]]

tests/pos/i15926.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@main def main(): Unit =
2+
println(summon[Sum[Minus[Succ[Zero]], Minus[Succ[Zero]]] =:= Minus[Succ[Succ[Zero]]]])
3+
4+
sealed trait IntT
5+
sealed trait NatT extends IntT
6+
final case class Zero() extends NatT
7+
final case class Succ[+N <: NatT](n: N) extends NatT
8+
final case class Minus[+N <: Succ[NatT]](n: N) extends IntT
9+
10+
type NatSum[X <: NatT, Y <: NatT] <: NatT = Y match
11+
case Zero => X
12+
case Succ[y] => NatSum[Succ[X], y]
13+
14+
type NatDif[X <: NatT, Y <: NatT] <: IntT = Y match
15+
case Zero => X
16+
case Succ[y] => X match
17+
case Zero => Minus[Y]
18+
case Succ[x] => NatDif[x, y]
19+
20+
type Sum[X <: IntT, Y <: IntT] <: IntT = Y match
21+
case Zero => X
22+
case Minus[y] => X match
23+
case Minus[x] => Minus[NatSum[x, y]]
24+
case _ => NatDif[X, y]
25+
case _ => X match
26+
case Minus[x] => NatDif[Y, x]
27+
case _ => NatSum[X, Y]

0 commit comments

Comments
 (0)