matchCaseClause method
Whether value matches the pattern of an if-case clause and passes
its guard: true / false, or the AsyncSuspensionRequest of an
awaiting guard. The pattern's variables are bound in caseEnvironment,
where the guard is evaluated.
ghissues62: shared by visitIfStatement and the async state machine, which runs an if-case of an async function itself and treated it as a plain condition ("If condition must evaluate to a boolean, but got int").
Implementation
Object matchCaseClause(
SCaseClause clause,
Object? value,
Environment caseEnvironment,
) {
final pattern = clause.guardedPattern.pattern;
try {
_matchAndBind(pattern, value, caseEnvironment);
} on PatternMatchD4rtException {
if (Logger.isDebug) {
Logger.debug(
"[SIfStatement] Pattern ${pattern.runtimeType} did not match.",
);
}
return false;
}
final guard = clause.guardedPattern.whenClause?.expression;
if (guard == null) return true;
final previousVisitorEnv = environment;
try {
environment = caseEnvironment; // Evaluate guard in case scope
final guardResult = guard.accept<Object?>(this);
if (guardResult is AsyncSuspensionRequest) return guardResult;
if (guardResult is! bool) {
throw RuntimeD4rtException(
"If-case 'when' clause must evaluate to a boolean.",
);
}
return guardResult;
} finally {
environment = previousVisitorEnv;
}
}