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 <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Paul Berry 2023-05-15 23:11:46 +00:00 committed by Commit Queue
parent dff000708f
commit 5043c38d25
3 changed files with 36 additions and 1 deletions

View file

@ -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,

View file

@ -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:', () {
@ -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:', () {
@ -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')),
[]),
]);
});
});
}

View file

@ -1749,6 +1749,7 @@ walker
walking
wanting
wants
warned
waste
wasted
watch