-
Notifications
You must be signed in to change notification settings - Fork 209
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
Should enhanced type promotion track when a type is exactly Null? #461
Comments
We now track nullability via type promotion, e.g. by promoting from `int?` to `int` to represent a non-nullable value. I've added a few more unit tests of flow analysis that were helpful while preparing this CL. I plan to add more tests in follow-up CLs. A few behaviors are lost by this change: - We no longer track whether a variable's value is exactly null (previously the tests annotated such variables as "nullable", which was a bit misleading). I'm not sure whether we really want this feature or not; I've filed dart-lang/language#461 for discussion. - Assignment of a non-null value to a nullable-typed variable no longer promotes it to non-nullable. I think we want to get this behavior back, but we'll need to add a little bit more machinery to get it back (we need to allow assignments in general to promote. I'll address in follow-up CLs. See dart-lang/language#440 for discussion. - A promotion to non-nullable that happens during a "try" block is lost if there is a "finally" block. I think we want to get this behavior back, but we'll need to add a little bit more machinery to do so, to allow promotions in general to be preserved by try/finally. I'll address in follow-up CLs. Change-Id: Ibc45d70043725697fbee063a8dcefb1179506e9b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109780 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Promotion via value comparison is a weird corner case: Null gets a very special treatment during evaluation of But any expression that actually invokes On the other hand, we generally promote based on specific syntactic forms when we have enough information to know that it's sound. On balance I think it would make sense to promote PS: I would expect the response to |
I think it's reasonable. Consider: void f(int? i) {
if (i != null) {
print("not null");
return; // <--
}
print(i?.isEven); // ???
} The only way to reach the last line is when |
This is actually already specified here. That part of the specification should possibly be folded into the flow analysis based promotion spec though. |
As linked above by @leafpetersen, the answer was yes. |
The current (work in progress) implementation of enhanced type promotion allows a
!= null
check to promote a type to non-nullable, e.g.:In principle, we could also allow an
== null
check to promote a type toNull
, e.g.:But I'm not sure whether it's a good idea or not. Arguments in favor:
Arguments against:
Null
type is strictly less useful, so it wouldn't allow the user to avoid inserting any casts.The text was updated successfully, but these errors were encountered: