Skip to content

Commit 38b983c

Browse files
Merge pull request #13184 from dotty-staging/fix-12800
Fix #12800: Clarify match type reduction error on empty scrutinee
2 parents 91a9a02 + 92f5806 commit 38b983c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ object MatchTypeTrace:
1313
case TryReduce(scrut: Type)
1414
case NoMatches(scrut: Type, cases: List[Type])
1515
case Stuck(scrut: Type, stuckCase: Type, otherCases: List[Type])
16+
case EmptyScrutinee(scrut: Type)
1617
import TraceEntry._
1718

1819
private class MatchTrace:
@@ -61,6 +62,12 @@ object MatchTypeTrace:
6162
def stuck(scrut: Type, stuckCase: Type, otherCases: List[Type])(using Context) =
6263
matchTypeFail(Stuck(scrut, stuckCase, otherCases))
6364

65+
/** Record a failure that scrutinee `scrut` is provably empty.
66+
* Only the first failure is recorded.
67+
*/
68+
def emptyScrutinee(scrut: Type)(using Context) =
69+
matchTypeFail(EmptyScrutinee(scrut))
70+
6471
/** Record in the trace that we are trying to reduce `scrut` when performing `op`
6572
* If `op` succeeds the entry is removed after exit. If `op` fails, it stays.
6673
*/
@@ -91,6 +98,9 @@ object MatchTypeTrace:
9198
| matches none of the cases
9299
|
93100
| ${casesText(cases)}"""
101+
case EmptyScrutinee(scrut) =>
102+
i""" failed since selector $scrut
103+
| is uninhabited (there are no values of that type)."""
94104
case Stuck(scrut, stuckCase, otherCases) =>
95105
i""" failed since selector $scrut
96106
| does not match ${caseText(stuckCase)}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -2928,8 +2928,11 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
29282928
// obviously sound, but quite restrictive. With the current formulation,
29292929
// we need to be careful that `provablyEmpty` covers all the conditions
29302930
// used to conclude disjointness in `provablyDisjoint`.
2931-
if (provablyEmpty(scrut)) NoType
2932-
else recur(cases)
2931+
if (provablyEmpty(scrut))
2932+
MatchTypeTrace.emptyScrutinee(scrut)
2933+
NoType
2934+
else
2935+
recur(cases)
29332936
}
29342937
}
29352938
}

tests/neg/12800.scala

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
object Test {
2+
type FieldType2[K, +V] = V with KeyTag2[K, V]
3+
trait KeyTag2[K, +V] extends Any
4+
5+
type WrapUpper = Tuple
6+
type Wrap[A] = Tuple1[A]
7+
8+
type Extract[A <: WrapUpper] = A match {
9+
case Wrap[h] => h
10+
}
11+
12+
summon[Extract[Wrap[FieldType2["foo", Int]]] =:= FieldType2["foo", Int]] // error
13+
// ^
14+
// Cannot prove that Main.Extract[Tuple1[Main.FieldType2[("foo" : String), Int]]] =:= Main.FieldType2[("foo" : String), Int].
15+
//
16+
// Note: a match type could not be fully reduced:
17+
//
18+
// trying to reduce Main.Extract[Tuple1[Main.FieldType2[("foo" : String), Int]]]
19+
// failed since selector Tuple1[Main.FieldType2[("foo" : String), Int]]
20+
// is uninhabited.
21+
}

tests/neg/matchtype-seq.check

+8
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ longer explanation available when compiling with `-explain`
257257
| Note: a match type could not be fully reduced:
258258
|
259259
| trying to reduce Test.T9[(Nothing, String)]
260+
| failed since selector (Nothing, String)
261+
| is uninhabited (there are no values of that type).
260262

261263
longer explanation available when compiling with `-explain`
262264
-- [E007] Type Mismatch Error: tests/neg/matchtype-seq.scala:106:40 ----------------------------------------------------
@@ -268,6 +270,8 @@ longer explanation available when compiling with `-explain`
268270
| Note: a match type could not be fully reduced:
269271
|
270272
| trying to reduce Test.T9[(String, Nothing)]
273+
| failed since selector (String, Nothing)
274+
| is uninhabited (there are no values of that type).
271275

272276
longer explanation available when compiling with `-explain`
273277
-- [E007] Type Mismatch Error: tests/neg/matchtype-seq.scala:107:37 ----------------------------------------------------
@@ -279,6 +283,8 @@ longer explanation available when compiling with `-explain`
279283
| Note: a match type could not be fully reduced:
280284
|
281285
| trying to reduce Test.T9[(Int, Nothing)]
286+
| failed since selector (Int, Nothing)
287+
| is uninhabited (there are no values of that type).
282288

283289
longer explanation available when compiling with `-explain`
284290
-- [E007] Type Mismatch Error: tests/neg/matchtype-seq.scala:108:37 ----------------------------------------------------
@@ -290,6 +296,8 @@ longer explanation available when compiling with `-explain`
290296
| Note: a match type could not be fully reduced:
291297
|
292298
| trying to reduce Test.T9[(Nothing, Int)]
299+
| failed since selector (Nothing, Int)
300+
| is uninhabited (there are no values of that type).
293301

294302
longer explanation available when compiling with `-explain`
295303
-- [E007] Type Mismatch Error: tests/neg/matchtype-seq.scala:109:29 ----------------------------------------------------

0 commit comments

Comments
 (0)