Skip to content

Commit 5043c38

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
Flow analysis: additional tests for error types.
Verify that the presence of an error type doesn't accidentally trigger "unnecessary cast pattern" or "unnecessary wildcard pattern" warnings (this verifies that `promoteForPattern` returns the proper value when an error type is encountered). Verify that the presence of an error type doesn't alter reachability conclusions that were established by previous subpatterns (this verifies that `promoteForPattern` uses the proper join to update `_unmatched` when an error type is encountered). Change-Id: I351c71574dff31602c40b257c28681b81fad2867 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303260 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent dff0007 commit 5043c38

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,9 @@ abstract class FlowAnalysis<Node extends Object, Statement extends Node,
719719
/// If [matchMayFailEvenIfCorrectType] is `true`, flow analysis would always
720720
/// update the unmatched value.
721721
///
722-
/// Returns `true` if [matchedType] is a subtype of [knownType].
722+
/// Returns `true` if [matchedType] is a subtype of [knownType] (and thus the
723+
/// user might need to be warned of an unnecessary cast or unnecessary
724+
/// wildcard pattern).
723725
bool promoteForPattern(
724726
{required Type matchedType,
725727
required Type knownType,

pkg/_fe_analyzer_shared/test/flow_analysis/flow_analysis_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6690,6 +6690,12 @@ main() {
66906690
]);
66916691
});
66926692
});
6693+
6694+
test('Error type does not trigger unnecessary cast warning', () {
6695+
h.run([
6696+
ifCase(expr('int'), wildcard().as_('error'), []),
6697+
]);
6698+
});
66936699
});
66946700

66956701
group('Constant pattern:', () {
@@ -8240,6 +8246,25 @@ main() {
82408246
]);
82418247
});
82428248
});
8249+
8250+
test('Error type does not alter previous reachability conclusions', () {
8251+
var x = Var('x');
8252+
h.run([
8253+
declare(x, initializer: expr('(Null, Object?)')),
8254+
ifCase(
8255+
x.expr,
8256+
recordPattern([
8257+
relationalPattern('!=', nullLiteral).recordField(),
8258+
wildcard(type: 'error').recordField()
8259+
]),
8260+
[
8261+
checkReachable(false),
8262+
],
8263+
[
8264+
checkReachable(true),
8265+
]),
8266+
]);
8267+
});
82438268
});
82448269

82458270
group('Relational pattern:', () {
@@ -9670,6 +9695,13 @@ main() {
96709695
]),
96719696
]);
96729697
});
9698+
9699+
test('Error type does not trigger unnecessary wildcard warning', () {
9700+
h.run([
9701+
ifCase(expr('num'), wildcard(type: 'int').and(wildcard(type: 'error')),
9702+
[]),
9703+
]);
9704+
});
96739705
});
96749706
}
96759707

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,7 @@ walker
17491749
walking
17501750
wanting
17511751
wants
1752+
warned
17521753
waste
17531754
wasted
17541755
watch

0 commit comments

Comments
 (0)