-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[analyzer] Analyzer reports misleading type when type-promotion #49235
Comments
@stereotype441 How difficult would it be to add a context message here explaining why promotion didn't work as expected? |
Hi guys, do we have any thoughts or progress here? |
Just a random drive-by comment: At Any explanation that shows the promoted type at Pragmatically, @AlexV525, you might want to use a different construct where the flow analysis doesn't promote as aggressively from one case to the next: Object? _testVariable;
void main() {
final Object? testVariable = _testVariable;
switch (testVariable) {
case A? _:
Usage<A>(testVariable);
case B? _: // At this point `testVariable` has type `Object?` and is promoted to `B?`.
Usage<B>(testVariable);
/* default: no-op */
}
}
class A {}
class B {}
class Usage<T> {
const Usage(this.field);
final T? field;
} The reason why a switch statement or expression doesn't promote (much) based on the knowledge that a certain sequence of pattern matching steps failed is discussed here and here. @stereotype441, you might have some further comments, is this working as expected? |
@eernstg That makes more sense if |
It's 'and' rather than 'or' because of pure logic (De Morgan's laws). So you could say that we have |
This tracker is for issues related to Analyzer.
Summary
Consider the code:
The analyzer gives the error: The argument type 'Object' can't be assigned to the parameter type 'B?'. (argument_type_not_assignable)
is A?
judgesA
andNull
, but the after flow only promotes non-nullable without the type context. As I can imagine it should be types thatis! A?
.Changing
is B?
tois B
solves the issue, though it seems to be lost nullability here, theis A?
has already judged the nullability.Version Information
Dart SDK: 2.17.3
The text was updated successfully, but these errors were encountered: