Skip to content

Commit 9334e8b

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
[sound flow analysis] Implement behaviors for map patterns.
This change updates the flow analysis logic for map patterns, so that when the language feature `sound-flow-analysis` is enabled, an empty map pattern is considered to match a non-nullable map. There is no behavioral change if the feature `sound-flow-analysis` is disabled. Bug: #60438 Change-Id: I3f5a79c00cfe91d37528a3790b5cedb9a8f010fc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421584 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 09f467b commit 9334e8b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,16 @@ mixin TypeAnalyzer<
10901090
keyType: keyType,
10911091
valueType: valueType,
10921092
);
1093+
bool matchMayFailEvenIfCorrectType = true;
1094+
if (typeAnalyzerOptions.soundFlowAnalysisEnabled && elements.isEmpty) {
1095+
// With sound null safety, an empty map pattern can only fail to match if
1096+
// the types don't match.
1097+
matchMayFailEvenIfCorrectType = false;
1098+
}
10931099
flow.promoteForPattern(
10941100
matchedType: matchedValueType,
10951101
knownType: requiredType,
1096-
matchMayFailEvenIfCorrectType: true);
1102+
matchMayFailEvenIfCorrectType: matchMayFailEvenIfCorrectType);
10971103
// Stack: ()
10981104

10991105
Map<int, Error>? restPatternErrors;

pkg/_fe_analyzer_shared/test/flow_analysis/flow_analysis_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11051,6 +11051,35 @@ main() {
1105111051
});
1105211052
});
1105311053
});
11054+
11055+
group('Map pattern', () {
11056+
test('When enabled, guaranteed to match non-nullable map', () {
11057+
h.run([
11058+
ifCase(
11059+
expr('Map<int, int>'), mapPattern([])..errorId = 'mapPattern', [
11060+
checkReachable(true),
11061+
], [
11062+
checkReachable(false),
11063+
])
11064+
], expectedErrors: {
11065+
'emptyMapPattern(pattern: mapPattern)'
11066+
});
11067+
});
11068+
11069+
test('When disabled, not guaranteed to match non-nullable map', () {
11070+
h.disableSoundFlowAnalysis();
11071+
h.run([
11072+
ifCase(
11073+
expr('Map<int, int>'), mapPattern([])..errorId = 'mapPattern', [
11074+
checkReachable(true),
11075+
], [
11076+
checkReachable(true),
11077+
])
11078+
], expectedErrors: {
11079+
'emptyMapPattern(pattern: mapPattern)'
11080+
});
11081+
});
11082+
});
1105411083
});
1105511084
}
1105611085

0 commit comments

Comments
 (0)