Skip to content

Commit

Permalink
Flow analysis: additional tests for error types.
Browse files Browse the repository at this point in the history
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 <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
stereotype441 authored and Commit Queue committed May 15, 2023
1 parent dff0007 commit 5043c38
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,9 @@ abstract class FlowAnalysis<Node extends Object, Statement extends Node,
/// If [matchMayFailEvenIfCorrectType] is `true`, flow analysis would always
/// update the unmatched value.
///
/// Returns `true` if [matchedType] is a subtype of [knownType].
/// Returns `true` if [matchedType] is a subtype of [knownType] (and thus the
/// user might need to be warned of an unnecessary cast or unnecessary
/// wildcard pattern).
bool promoteForPattern(
{required Type matchedType,
required Type knownType,
Expand Down
32 changes: 32 additions & 0 deletions pkg/_fe_analyzer_shared/test/flow_analysis/flow_analysis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6690,6 +6690,12 @@ main() {
]);
});
});

test('Error type does not trigger unnecessary cast warning', () {
h.run([
ifCase(expr('int'), wildcard().as_('error'), []),
]);
});
});

group('Constant pattern:', () {
Expand Down Expand Up @@ -8240,6 +8246,25 @@ main() {
]);
});
});

test('Error type does not alter previous reachability conclusions', () {
var x = Var('x');
h.run([
declare(x, initializer: expr('(Null, Object?)')),
ifCase(
x.expr,
recordPattern([
relationalPattern('!=', nullLiteral).recordField(),
wildcard(type: 'error').recordField()
]),
[
checkReachable(false),
],
[
checkReachable(true),
]),
]);
});
});

group('Relational pattern:', () {
Expand Down Expand Up @@ -9670,6 +9695,13 @@ main() {
]),
]);
});

test('Error type does not trigger unnecessary wildcard warning', () {
h.run([
ifCase(expr('num'), wildcard(type: 'int').and(wildcard(type: 'error')),
[]),
]);
});
});
}

Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/test/spell_checking_list_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,7 @@ walker
walking
wanting
wants
warned
waste
wasted
watch
Expand Down

0 comments on commit 5043c38

Please sign in to comment.