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

View file

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