You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julemand101 opened this issue
Mar 3, 2022
· 3 comments
Labels
area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
Dart SDK version: 2.16.1 (stable) (Tue Feb 8 12:02:33 2022 +0100) on "windows_x64"
Dart SDK version: 2.17.0-150.0.dev (dev) (Fri Feb 25 22:29:59 2022 -0800) on "windows_x64"
The following program shows some "interesting" behavior when it comes to promote a variable to Null when doing a null-check:
extensionNullExtensiononNull {
voidsomeMethod() {
print("Called Null.someMethod");
}
}
String?getString() =>null;
voidmain() {
final string =getString();
if (string ==null) {
//(string as Null).someMethod();
string.someMethod();
}
}
This fails by the analyzer with:
The method 'someMethod' can't be unconditionally invoked because the receiver can be 'null'. (Documentation)
Try making the call conditional (using '?.') or adding a null check to the target ('!').
And when running the program:
bin/test.dart:14:12: Error: The method 'someMethod' isn't defined for the class 'String?'.
Try correcting the name to the name of an existing method, or defining a method named 'someMethod'.
string.someMethod();
^^^^^^^^^^
The program does no longer have any warning or error from the analyzer and runs with the following result:
Called Null.someMethod
Called Null.someMethod
This behavior seems kinda weird since the promotion to Null seems to happen based on a local type-cast that should not affect the rest of the program.
This might also explain why auto-completion is not that smart (tested with IntelliJ) when it comes to showing what methods can be called on the value. If I use auto-complete before (string as Null).someMethod(); I will get the full list of methods as if the string variable was a String:
While if I do the same after (string as Null).someMethod(); I will get the possible methods for Null:
The text was updated successfully, but these errors were encountered:
eernstg
added
area-meta
Cross-cutting, high-level issues (for tracking many other implementation issues, ...).
type-bug
Incorrect behavior (everything from a crash to more subtle misbehavior)
labels
Mar 3, 2022
A check of the form e == null or of the form e is Null where e has static
type T promotes the type of e to Null in the true continuation, and to NonNull(T) in the false continuation.
I think we've discussed not performing this particular promotion (because of the rather low value of having an expression of type Null), but I can't find any evidence that we shouldn't promote.
I can recall some late changes to how we handle null in the promotion (something to do with unsound mode) but can't recall the details. @stereotype441 might remember more since he did most of the implementation.
I don't recall whether there was a deliberate decision at the time we implemented it, but sometime later, we did consider adding this functionality, in response to dart-lang/language#1505. I did some analysis back in November, and IMHO, enough code would be broken by the change that it's not worth it (you can see my analysis here: dart-lang/language#1505 (comment)). But the question hasn't risen to a high enough priority for the language team to discuss it and make a firm yes/no decision, so the issue remains open.
I'm going to close this issue on the grounds that it's a duplicate of dart-lang/language#1505. If folks want to discuss it further, let's have the discussion there so that all the information is in one place.
area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
Tested against the following Dart versions:
The following program shows some "interesting" behavior when it comes to promote a variable to
Null
when doing a null-check:This fails by the analyzer with:
And when running the program:
If I uncomment
(string as Null).someMethod();
as:The program does no longer have any warning or error from the analyzer and runs with the following result:
This behavior seems kinda weird since the promotion to
Null
seems to happen based on a local type-cast that should not affect the rest of the program.This might also explain why auto-completion is not that smart (tested with IntelliJ) when it comes to showing what methods can be called on the value. If I use auto-complete before

(string as Null).someMethod();
I will get the full list of methods as if thestring
variable was aString
:While if I do the same after

(string as Null).someMethod();
I will get the possible methods forNull
:The text was updated successfully, but these errors were encountered: