Shared pattern logic: clean up nomenclature around guards.

Change-Id: I596362dab53ac2efc68cb45dbb29feab80c6240a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259463
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
Paul Berry 2022-09-20 13:02:16 +00:00 committed by Commit Bot
parent da26deb0b4
commit 50ac31f286
2 changed files with 26 additions and 25 deletions

View file

@ -19,11 +19,11 @@ class CaseHeadInfo<Node extends Object, Expression extends Node> {
/// For a `case` clause, the case pattern. For a `default` clause, `null`.
final Node? pattern;
/// For a `case` clause that has a `when` part, the expression following
/// For a `case` clause that has a guard clause, the expression following
/// `when`. Otherwise `null`.
final Expression? when;
final Expression? guard;
CaseHeadInfo({required this.node, required this.pattern, this.when});
CaseHeadInfo({required this.node, required this.pattern, this.guard});
}
/// Information supplied by the client to [TypeAnalyzer.analyzeSwitchExpression]
@ -38,7 +38,7 @@ class ExpressionCaseInfo<Node extends Object, Expression extends Node>
ExpressionCaseInfo(
{required super.node,
required super.pattern,
super.when,
super.guard,
required this.body});
}
@ -334,7 +334,7 @@ mixin TypeAnalyzer<Node extends Object, Statement extends Node,
switchScrutinee: scrutinee,
topPattern: pattern));
// Stack: (Expression, i * ExpressionCase, Pattern)
Expression? guard = caseInfo.when;
Expression? guard = caseInfo.guard;
bool hasGuard = guard != null;
if (hasGuard) {
_checkGuardType(guard, analyzeExpression(guard, boolType));
@ -410,7 +410,7 @@ mixin TypeAnalyzer<Node extends Object, Statement extends Node,
topPattern: pattern));
// Stack: (Expression, numExecutionPaths * StatementCase,
// numHeads * CaseHead, Pattern),
Expression? guard = head.when;
Expression? guard = head.guard;
bool hasGuard = guard != null;
if (hasGuard) {
_checkGuardType(guard, analyzeExpression(guard, boolType));
@ -582,7 +582,7 @@ mixin TypeAnalyzer<Node extends Object, Statement extends Node,
void handleCase_afterCaseHeads(Statement node, int caseIndex, int numHeads);
/// Called after visiting a single `case` clause, consisting of a pattern and
/// a `when` condition.
/// an optional guard.
///
/// [node] is the enclosing switch statement or switch expression and
/// [caseIndex] is the index of the `case` clause.
@ -607,8 +607,8 @@ mixin TypeAnalyzer<Node extends Object, Statement extends Node,
/// Stack effect: pushes (CaseHead).
void handleDefault(Node node, int caseIndex);
/// Called when visiting a `case` that lacks a `when` clause. Since the lack
/// of a `when` clause is semantically equivalent to `when true`, this method
/// Called when visiting a `case` that lacks a guard clause. Since the lack
/// of a guard clause is semantically equivalent to `when true`, this method
/// should behave similarly to visiting the boolean literal `true`.
///
/// [node] is the enclosing switch statement, switch expression, or `if`, and

View file

@ -404,12 +404,12 @@ class ExpressionCase extends Node
final Pattern? pattern;
@override
final Expression? when;
final Expression? guard;
@override
final Expression body;
ExpressionCase._(this.pattern, this.when, this.body,
ExpressionCase._(this.pattern, this.guard, this.body,
{required super.location})
: super._();
@ -418,7 +418,7 @@ class ExpressionCase extends Node
String toString() => [
pattern == null ? 'default' : 'case $pattern',
if (when != null) ' when $when',
if (guard != null) ' when $guard',
': $body'
].join('');
@ -906,8 +906,8 @@ abstract class Pattern extends Node with CaseHead, CaseHeads {
PatternDispatchResult<Node, Expression, Var, Type> visit(Harness h);
CaseHead when(Expression whenExpression) =>
_When(this, whenExpression, location: location);
CaseHead when(Expression guard) =>
_GuardedCaseHead(this, guard, location: location);
String _debugString({required bool needsKeywordOrType});
}
@ -1741,6 +1741,17 @@ class _ForEach extends Statement {
}
}
class _GuardedCaseHead extends Node with CaseHead, CaseHeads {
@override
final Pattern _pattern;
@override
final Expression _guard;
_GuardedCaseHead(this._pattern, this._guard, {required super.location})
: super._();
}
class _If extends _IfBase {
final Expression condition;
@ -2447,7 +2458,7 @@ class _MiniAstTypeAnalyzer
return StatementCaseInfo([
for (var caseHead in case_._caseHeads._caseHeads)
CaseHeadInfo(
node: caseHead, pattern: caseHead._pattern, when: caseHead._guard)
node: caseHead, pattern: caseHead._pattern, guard: caseHead._guard)
], case_._body.statements, labels: case_._caseHeads._labels);
}
@ -3103,16 +3114,6 @@ class _VariableReference extends LValue {
}
}
class _When extends Node with CaseHead, CaseHeads {
@override
final Pattern _pattern;
@override
final Expression _guard;
_When(this._pattern, this._guard, {required super.location}) : super._();
}
class _While extends Statement {
final Expression condition;
final Statement body;