diff --git a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart index a90563774d2..a22ed55dc59 100644 --- a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart +++ b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart @@ -19,11 +19,11 @@ class CaseHeadInfo { /// 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 ExpressionCaseInfo( {required super.node, required super.pattern, - super.when, + super.guard, required this.body}); } @@ -334,7 +334,7 @@ mixin TypeAnalyzer [ 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 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;