@@ -5,7 +5,10 @@ package core
5
5
import Types ._ , Contexts ._ , Symbols ._ , Decorators ._
6
6
import util .Property
7
7
8
+ /** A utility module to produce match type reduction traces in error messages.
9
+ */
8
10
object MatchTypeTrace :
11
+
9
12
private enum TraceEntry :
10
13
case TryReduce (scrut : Type )
11
14
case NoMatches (scrut : Type , cases : List [Type ])
@@ -17,6 +20,9 @@ object MatchTypeTrace:
17
20
18
21
private val MatchTrace = new Property .Key [MatchTrace ]
19
22
23
+ /** Execute `op` and if it involves a failed match type reduction
24
+ * return the trace of that reduction. Otherwise return the empty string.
25
+ */
20
26
def record (op : Context ?=> Any )(using Context ): String =
21
27
val trace = new MatchTrace
22
28
inContext(ctx.fresh.setProperty(MatchTrace , trace)) {
@@ -30,6 +36,7 @@ object MatchTypeTrace:
30
36
| ${trace.entries.reverse.map(explainEntry)}%\n% """
31
37
}
32
38
39
+ /** Are we running an operation that records a match type trace? */
33
40
def isRecording (using Context ): Boolean =
34
41
ctx.property(MatchTrace ).isDefined
35
42
@@ -41,12 +48,22 @@ object MatchTypeTrace:
41
48
case _ =>
42
49
case _ =>
43
50
51
+ /** Record a failure that scrutinee `scrut` does not match any case in `cases`.
52
+ * Only the first failure is recorded.
53
+ */
44
54
def noMatches (scrut : Type , cases : List [Type ])(using Context ) =
45
55
matchTypeFail(NoMatches (scrut, cases))
46
56
57
+ /** Record a failure that scrutinee `scrut` does not match `stuckCase` but is
58
+ * not disjoint from it either, which means that the remaining cases `otherCases`
59
+ * cannot be visited. Only the first failure is recorded.
60
+ */
47
61
def stuck (scrut : Type , stuckCase : Type , otherCases : List [Type ])(using Context ) =
48
62
matchTypeFail(Stuck (scrut, stuckCase, otherCases))
49
63
64
+ /** Record in the trace that we are trying to reduce `scrut` when performing `op`
65
+ * If `op` succeeds the entry is removed after exit. If `op` fails, it stays.
66
+ */
50
67
def recurseWith (scrut : Type )(op : => Type )(using Context ): Type =
51
68
ctx.property(MatchTrace ) match
52
69
case Some (trace) =>
0 commit comments