From a7907254adcd348aaca015996d3476641ace83ce Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Mon, 12 Dec 2022 20:28:29 +0000 Subject: [PATCH] Flow analysis: move _unmatched to _FlowAnalysisImpl. This will allow me to push additional pattern contexts to represent subpatterns, while still accumulating the "unmatched" flows for the pattern as a whole. Bug: https://github.com/dart-lang/sdk/issues/50419 Change-Id: I8a7b1255abeda1cd07d82828057c81f45954d173 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274941 Reviewed-by: Konstantin Shcheglov Commit-Queue: Paul Berry --- .../lib/src/flow_analysis/flow_analysis.dart | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart index b73b1bad73a..e331167e433 100644 --- a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart +++ b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart @@ -3266,6 +3266,10 @@ class _FlowAnalysisImpl _current = new FlowModel(Reachability.initial); + /// If a pattern is being analyzed, flow model representing all code paths + /// accumulated so far in which the pattern fails to match. Otherwise `null`. + FlowModel? _unmatched; + /// The most recently visited expression for which an [ExpressionInfo] object /// exists, or `null` if no expression has been visited that has a /// corresponding [ExpressionInfo] object. @@ -3396,8 +3400,7 @@ class _FlowAnalysisImpl context = _stack.last as _PatternContext; - context._unmatched = _join(context._unmatched, _current); + _unmatched = _join(_unmatched!, _current); } @override @@ -3418,10 +3421,10 @@ class _FlowAnalysisImpl _popPattern(Expression? guard) { - _TopPatternContext context = - _stack.removeLast() as _TopPatternContext; - FlowModel unmatched = context._unmatched; + _FlowContext context = _stack.removeLast(); + assert(context is _TopPatternContext); + FlowModel unmatched = _unmatched!; + _unmatched = null; if (guard != null) { ExpressionInfo guardInfo = _expressionEnd(guard); _current = guardInfo.ifTrue; @@ -4369,8 +4374,9 @@ class _FlowAnalysisImpl? scrutineeReference) { - _stack.add(new _TopPatternContext( - scrutineeReference, _current.setUnreachable())); + assert(_unmatched == null); + _unmatched = _current.setUnreachable(); + _stack.add(new _TopPatternContext(scrutineeReference)); } /// Associates [expression], which should be the most recently visited @@ -5088,10 +5094,6 @@ class _NullInfo implements ExpressionInfo { /// Base class for [_FlowContext]s representing patterns. abstract class _PatternContext extends _FlowContext { - /// Flow model representing all code paths accumulated so far in which the - /// pattern fails to match. - abstract FlowModel _unmatched; - /// Reference for the value being matched, if any. ReferenceWithType? get _scrutineeReference; } @@ -5185,15 +5187,11 @@ class _TopPatternContext extends _PatternContext { @override final ReferenceWithType? _scrutineeReference; - @override - FlowModel _unmatched; - - _TopPatternContext(this._scrutineeReference, this._unmatched); + _TopPatternContext(this._scrutineeReference); @override String toString() => - '_TopPatternContext(scrutineeReference: $_scrutineeReference, ' - 'unmatched: $_unmatched)'; + '_TopPatternContext(scrutineeReference: $_scrutineeReference)'; } /// Specialization of [ExpressionInfo] for the case where the information we