Skip to content

Commit 7465e3c

Browse files
Revert "ErrorType instead of throwing in match type "no cases""
This reverts commit 9ae1598 Note that the changes in Typer: ``` val unsimplifiedType = result.tpe simplify(result, pt, locked) result.tpe.stripTypeVar match case e: ErrorType if !unsimplifiedType.isErroneous => errorTree(xtree, e.msg, xtree.srcPos) case _ => result ``` cannot be reverted yet since the MatchReducer now also reduces to an `ErrorType` for MatchTypeLegacyPatterns, introduced after 9ae1598.
1 parent 180a671 commit 7465e3c

File tree

8 files changed

+82
-39
lines changed

8 files changed

+82
-39
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ object MatchTypeTrace:
1212

1313
private enum TraceEntry:
1414
case TryReduce(scrut: Type)
15+
case NoMatches(scrut: Type, cases: List[MatchTypeCaseSpec])
1516
case Stuck(scrut: Type, stuckCase: MatchTypeCaseSpec, otherCases: List[MatchTypeCaseSpec])
1617
case NoInstance(scrut: Type, stuckCase: MatchTypeCaseSpec, fails: List[(Name, TypeBounds)])
1718
case EmptyScrutinee(scrut: Type)
@@ -50,6 +51,12 @@ object MatchTypeTrace:
5051
case _ =>
5152
case _ =>
5253

54+
/** Record a failure that scrutinee `scrut` does not match any case in `cases`.
55+
* Only the first failure is recorded.
56+
*/
57+
def noMatches(scrut: Type, cases: List[MatchTypeCaseSpec])(using Context) =
58+
matchTypeFail(NoMatches(scrut, cases))
59+
5360
/** Record a failure that scrutinee `scrut` does not match `stuckCase` but is
5461
* not disjoint from it either, which means that the remaining cases `otherCases`
5562
* cannot be visited. Only the first failure is recorded.
@@ -95,6 +102,11 @@ object MatchTypeTrace:
95102
private def explainEntry(entry: TraceEntry)(using Context): String = entry match
96103
case TryReduce(scrut: Type) =>
97104
i" trying to reduce $scrut"
105+
case NoMatches(scrut, cases) =>
106+
i""" failed since selector $scrut
107+
| matches none of the cases
108+
|
109+
| ${casesText(cases)}"""
98110
case EmptyScrutinee(scrut) =>
99111
i""" failed since selector $scrut
100112
| is uninhabited (there are no values of that type)."""

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,22 +3595,9 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
35953595
NoType
35963596
case Nil =>
35973597
val casesText = MatchTypeTrace.noMatchesText(scrut, cases)
3598-
ErrorType(reporting.MatchTypeNoCases(casesText))
3599-
3600-
inFrozenConstraint {
3601-
if scrut.isError then
3602-
// if the scrutinee is an error type
3603-
// then just return that as the result
3604-
// not doing so will result in the first type case matching
3605-
// because ErrorType (as a FlexType) is <:< any type case
3606-
// this situation can arise from any kind of nesting of match types,
3607-
// e.g. neg/i12049 `Tuple.Concat[Reverse[ts], (t2, t1)]`
3608-
// if Reverse[ts] fails with no matches,
3609-
// the error type should be the reduction of the Concat too
3610-
scrut
3611-
else
3612-
recur(cases)
3613-
}
3598+
throw MatchTypeReductionError(em"Match type reduction $casesText")
3599+
3600+
inFrozenConstraint(recur(cases))
36143601
}
36153602
}
36163603

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ object TypeError:
5353
def toMessage(using Context) = msg
5454
end TypeError
5555

56+
class MatchTypeReductionError(msg: Message)(using Context) extends TypeError:
57+
def toMessage(using Context) = msg
58+
5659
class MalformedType(pre: Type, denot: Denotation, absMembers: Set[Name])(using Context) extends TypeError:
5760
def toMessage(using Context) = em"malformed type: $pre is not a legal prefix for $denot because it contains abstract type member${if (absMembers.size == 1) "" else "s"} ${absMembers.mkString(", ")}"
5861

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ trait ImplicitRunInfo:
663663
traverseChildren(t)
664664
case t =>
665665
traverseChildren(t)
666-
traverse(t.normalized)
666+
traverse(try t.normalized catch case _: MatchTypeReductionError => t)
667667
catch case ex: Throwable => handleRecursive("collectParts of", t.show, ex)
668668

669669
def apply(tp: Type): collection.Set[Type] =

tests/neg-macros/toexproftuple.scala

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
import scala.quoted._, scala.deriving.*
1+
import scala.quoted._, scala.deriving.* // error
2+
// ^
3+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
4+
// matches none of the cases
5+
//
6+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
7+
// case EmptyTuple => EmptyTuple
28

3-
inline def mcr: Any = ${mcrImpl}
9+
inline def mcr: Any = ${mcrImpl} // error
10+
// ^
11+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
12+
// matches none of the cases
13+
//
14+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
15+
// case EmptyTuple => EmptyTuple
416

5-
def mcrImpl(using ctx: Quotes): Expr[Any] = {
17+
def mcrImpl(using ctx: Quotes): Expr[Any] = { // error // error
18+
//^
19+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
20+
// matches none of the cases
21+
//
22+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
23+
// case EmptyTuple => EmptyTuple
24+
25+
// ^
26+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
27+
// matches none of the cases
28+
//
29+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
30+
// case EmptyTuple => EmptyTuple
631

732
val tpl: (Expr[1], Expr[2], Expr[3]) = ('{1}, '{2}, '{3})
833
'{val res: (1, 3, 3) = ${Expr.ofTuple(tpl)}; res} // error
@@ -11,12 +36,28 @@ def mcrImpl(using ctx: Quotes): Expr[Any] = {
1136
// Required: quoted.Expr[((1 : Int), (3 : Int), (3 : Int))]
1237

1338
val tpl2: (Expr[1], 2, Expr[3]) = ('{1}, 2, '{3})
14-
'{val res = ${Expr.ofTuple(tpl2)}; res} // error
39+
'{val res = ${Expr.ofTuple(tpl2)}; res} // error // error // error // error
1540
// ^
1641
// Cannot prove that (quoted.Expr[(1 : Int)], (2 : Int), quoted.Expr[(3 : Int)]) =:= scala.Tuple.Map[
1742
// scala.Tuple.InverseMap[
1843
// (quoted.Expr[(1 : Int)], (2 : Int), quoted.Expr[(3 : Int)])
1944
// , quoted.Expr]
2045
// , quoted.Expr].
2146

47+
// ^
48+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
49+
// matches none of the cases
50+
//
51+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
52+
// case EmptyTuple => EmptyTuple
53+
54+
// ^
55+
// Cyclic reference involving val res
56+
57+
// ^
58+
// Match type reduction failed since selector ((2 : Int), quoted.Expr[(3 : Int)])
59+
// matches none of the cases
60+
//
61+
// case quoted.Expr[x] *: t => x *: scala.Tuple.InverseMap[t, quoted.Expr]
62+
// case EmptyTuple => EmptyTuple
2263
}

tests/neg/i12049.check

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
| case B => String
1616
|
1717
| longer explanation available when compiling with `-explain`
18-
-- [E184] Type Error: tests/neg/i12049.scala:14:23 ---------------------------------------------------------------------
18+
-- Error: tests/neg/i12049.scala:14:23 ---------------------------------------------------------------------------------
1919
14 |val y3: String = ??? : Last[Int *: Int *: Boolean *: String *: EmptyTuple] // error
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21-
| Match type reduction failed since selector EmptyTuple
20+
| ^
21+
| Match type reduction failed since selector EmptyTuple.type
2222
| matches none of the cases
2323
|
2424
| case _ *: _ *: t => Last[t]
2525
| case t *: EmptyTuple => t
26-
-- [E184] Type Error: tests/neg/i12049.scala:22:26 ---------------------------------------------------------------------
26+
-- Error: tests/neg/i12049.scala:22:26 ---------------------------------------------------------------------------------
2727
22 |val z3: (A, B, A) = ??? : Reverse[(A, B, A)] // error
28-
| ^^^^^^^^^^^^^^^^^^
28+
| ^
2929
| Match type reduction failed since selector A *: EmptyTuple.type
3030
| matches none of the cases
3131
|
@@ -45,17 +45,17 @@
4545
| Therefore, reduction cannot advance to the remaining case
4646
|
4747
| case B => String
48-
-- [E184] Type Error: tests/neg/i12049.scala:25:26 ---------------------------------------------------------------------
48+
-- Error: tests/neg/i12049.scala:25:26 ---------------------------------------------------------------------------------
4949
25 |val _ = summon[String =:= Last[Int *: Int *: Boolean *: String *: EmptyTuple]] // error
50-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51-
| Match type reduction failed since selector EmptyTuple
50+
| ^
51+
| Match type reduction failed since selector EmptyTuple.type
5252
| matches none of the cases
5353
|
5454
| case _ *: _ *: t => Last[t]
5555
| case t *: EmptyTuple => t
56-
-- [E184] Type Error: tests/neg/i12049.scala:26:29 ---------------------------------------------------------------------
56+
-- Error: tests/neg/i12049.scala:26:29 ---------------------------------------------------------------------------------
5757
26 |val _ = summon[(A, B, A) =:= Reverse[(A, B, A)]] // error
58-
| ^^^^^^^^^^^^^^^^^^
58+
| ^
5959
| Match type reduction failed since selector A *: EmptyTuple.type
6060
| matches none of the cases
6161
|
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
object Test:
2-
type AnyKindMatchType1[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded
2+
type AnyKindMatchType1[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
33
case Option[a] => Int
44

55
type AnyKindMatchType2[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded
66
case Option => Int // error: Missing type parameter for Option
77

8-
type AnyKindMatchType3[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded
8+
type AnyKindMatchType3[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
99
case _ => Int
1010

11-
type AnyKindMatchType4[X <: Option] = X match // error // error: the scrutinee of a match type cannot be higher-kinded
11+
type AnyKindMatchType4[X <: Option] = X match // error // error: the scrutinee of a match type cannot be higher-kinded // error
1212
case _ => Int
1313

14-
type AnyKindMatchType5[X[_]] = X match // error: the scrutinee of a match type cannot be higher-kinded
14+
type AnyKindMatchType5[X[_]] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
1515
case _ => Int
1616
end Test

tests/neg/matchtype-seq.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
-- [E184] Type Error: tests/neg/matchtype-seq.scala:9:11 ---------------------------------------------------------------
1+
-- Error: tests/neg/matchtype-seq.scala:9:11 ---------------------------------------------------------------------------
22
9 | identity[T1[3]]("") // error
3-
| ^^^^^
3+
| ^
44
| Match type reduction failed since selector (3 : Int)
55
| matches none of the cases
66
|
77
| case (1 : Int) => Int
88
| case (2 : Int) => String
9-
-- [E184] Type Error: tests/neg/matchtype-seq.scala:10:11 --------------------------------------------------------------
9+
-- Error: tests/neg/matchtype-seq.scala:10:11 --------------------------------------------------------------------------
1010
10 | identity[T1[3]](1) // error
11-
| ^^^^^
11+
| ^
1212
| Match type reduction failed since selector (3 : Int)
1313
| matches none of the cases
1414
|

0 commit comments

Comments
 (0)