@@ -5,7 +5,10 @@ package core
55import Types ._ , Contexts ._ , Symbols ._ , Decorators ._
66import util .Property
77
8+ /** A utility module to produce match type reduction traces in error messages.
9+ */
810object MatchTypeTrace :
11+
912 private enum TraceEntry :
1013 case TryReduce (scrut : Type )
1114 case NoMatches (scrut : Type , cases : List [Type ])
@@ -17,6 +20,9 @@ object MatchTypeTrace:
1720
1821 private val MatchTrace = new Property .Key [MatchTrace ]
1922
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+ */
2026 def record (op : Context ?=> Any )(using Context ): String =
2127 val trace = new MatchTrace
2228 inContext(ctx.fresh.setProperty(MatchTrace , trace)) {
@@ -30,6 +36,7 @@ object MatchTypeTrace:
3036 | ${trace.entries.reverse.map(explainEntry)}%\n% """
3137 }
3238
39+ /** Are we running an operation that records a match type trace? */
3340 def isRecording (using Context ): Boolean =
3441 ctx.property(MatchTrace ).isDefined
3542
@@ -41,12 +48,22 @@ object MatchTypeTrace:
4148 case _ =>
4249 case _ =>
4350
51+ /** Record a failure that scrutinee `scrut` does not match any case in `cases`.
52+ * Only the first failure is recorded.
53+ */
4454 def noMatches (scrut : Type , cases : List [Type ])(using Context ) =
4555 matchTypeFail(NoMatches (scrut, cases))
4656
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+ */
4761 def stuck (scrut : Type , stuckCase : Type , otherCases : List [Type ])(using Context ) =
4862 matchTypeFail(Stuck (scrut, stuckCase, otherCases))
4963
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+ */
5067 def recurseWith (scrut : Type )(op : => Type )(using Context ): Type =
5168 ctx.property(MatchTrace ) match
5269 case Some (trace) =>
0 commit comments