-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Nullable state tracking for the ternary operator #44498
Comments
But I guess many people will treat that types of warnings as errors. At least I'm doing that right now. |
It's just that This is true for definite assignment too, if (o is string s == true) {
_ = s; // error
} I think extending that would be more involved that it seems. |
It does do that for some operators, like the |
The pattern I would recommend here is It feels like when one of the conditional expression branches is a boolean constant, we should be able to propagate the flow state from the non-constant branch back up to the if-statement's condition somehow. I think code which uses @svick, do you mind if I just move this issue to roslyn? I think it would be a reasonable thing to put on our nullable backlog. |
Sure, if you want to cherry pick from a whole set of possible forms - there will be uncovered cases. though I agree that's better than not doing anything. |
@RikkiGibson Feel free to move this to roslyn. |
@alrz Cherry picking forms is pretty much how NRTs are designed, so I think it makes sense to discuss new forms to pick. Though with that approach, there has to be a cutoff somewhere (probably based on how common a form is and how hard would it be to support it), otherwise you're pretty much just implementing a theorem prover, badly. |
Once we come to a consensus internally on this, I'll either move to Roslyn or close. Thanks! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I see no reason why we shouldn't do this for nullable, but doing it for some other forms of analysis (such as definite assignment of 'out' arguments described in dotnet/csharplang#3485) may require spec changes. |
This will be resolved by the "improved definite assignment" feature in C# 10. dotnet/csharplang#4465 #51463 The feature branch currently handles this scenario. SharpLab |
Consider the following code:
This currently produces "warning CS8602: Dereference of a possibly null reference" in the body of the
if
, even thoughresponse
can't possibly benull
there.This code can be simplified to the following:
This version does not produce any warnings.
So, my question is: should nullability tracking be changed so that it also works for the ternary operator in situations like the above (i.e. where one of the possible results is a constant boolean)?
One argument for why that's not necessary is that all such cases can (and should?) be simplified to use
&&
or||
, which track nullability properly. But personally, I think the language should work well even for code that's still being written, before it's refactored to its final form, so it makes sense to support this.The text was updated successfully, but these errors were encountered: