-
Notifications
You must be signed in to change notification settings - Fork 207
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
Null-aware member access needlessly an error with special receiver types? #711
Comments
I have no problem keeping the error. If we disallow member access on |
I think this is a great place for a warning. The code is not actively harmful and its behavior is well-defined, but also very clearly does not do anything useful. If it were me, I'd have all dead code, unreachable code, unused variable, unused import, etc. diagnostics be warnings. And, like Lasse, I consider this to be dead code. |
The underlying issue is actually that we need a way to talk about a type which is obtained by stripping We discussed introducing a suffix We also don't have the concept in the "spec model" of types that we use to specify the subtyping relation. We do have other non-denotable types, e.g., the subtype rules talk about types of the form I think we need to introduce a suffix With that in place, we could replace the rule that relies on
Consider a variable However, this doesn't change the situation for the receiver type So, @munificent, I believe the best way to integrate your preferred approach consistently into the language would be to eliminate the rule that every member access on |
If member accesses on |
We are changing the specification of errors on |
Thanks to @sgrekhov for spotting this issue. Consider the following program:
Following the nnbd specification, the invocation of
toString
is a compile-time error: The null-shorting member access?.
is checked as if the type of the receiver were non-nullable (because it should not be an error to access a member on a receiver that might be null when that's the whole point of the mechanism), and more specifically: It is NonNull(T
) whereT
is the receiver type.However, we might want to introduce a notion of "the non-nullable version of a type" which is slightly different for this purpose:
NonNull(
Null
) isNever
, but ifn
has typeNever
thenn.toString()
is a compile-time error. The statement is guaranteed to simply do nothing (except that the evaluation of the receiver could have side-effects, and might not terminate), but there is nothing wrong in evaluatinga
and obtaining the null object and then skipping.toString()
. So it's slightly odd that it is an error.Another place where the NonNull function may not deliver the most useful result is
FutureOr<T>
: That type is returned unchanged by NonNull. Again, the anomaly is small, because it's difficult to invoke any other members than the ones fromObject
, even on a receiver of typeFutureOr<Future<T>>
for someT
.So do we just keep this special case as it is? @munificent, @lrhn, @leafpetersen, WDYT?
The text was updated successfully, but these errors were encountered: