-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Roslyn incorrectly updates variables' states to not nullable upon passing them to [AllowNull] parameters #48605
Comments
This is a scenario we discussed at some length internally a few months ago. At the time we concluded that the flow state of the argument for such a parameter should be updated to not-null after the call, as part of our change to reduce redundant warnings for maybe-null arguments to not-null parameters (#43383). (I feel the need to note that I didn't agree with this decision, but that my point of view did not win the day 😉) Tagging @333fred. |
TBH I fail to see why this would be considered logical behavior. I wonder if you remember any arguments for it
❤️ |
The arguments for this are that we made a change to update the flow state of a parameter to the declared state of a parameter after such a method is called. Generally speaking, we didn't consider this case to be realistic code, and weren't too concerned with the simpler rule (always update the state) being implemented. Can you share a real scenario that is actually hurt by this? |
While I can't come up with a real scenario here, I agree with @TessenR and @RikkiGibson that this should produce a warning. |
For a real scenario, imagine that I called The parameter |
I'm not sure why "update the state to not nullable if parameter only allows not nullable values" isn't simple enough.
The original design of nullable reference types all but ensured that there's a ton of API out there using these attributes. One of the examples is linked above. The reasons for this are:
This leads to a situation where e.g. there's an implementation of the following interface from netcore3.1:
With the following type:
And I'll miss warnings in the following scenario which will crash with an NRE
Note that
So I'd consider this scenario a very real-world one. While this all can be solved by tremendous effort of software engineers across the world e.g.
etc I think the better solution would be to just support this attribute in the compiler especially given that this looks like the most obvious thing ever - the parameter allows null values, why would the compiler think it guarantees the argument won't be null after the call? |
I think I've got a bit lost in the terminology here. What exactly do you mean by Because it can't be neither What I find especially confusing is that the compiler already understands
But for some reason just ignores its |
Not yet certain how we are resolving this, but I am self-assigning in order to keep it on my radar. |
Trying to get this on the LDM agenda for this / next week so we can get a resolution. |
It looks like we will probably change analysis according to dotnet/csharplang#4102 (comment) |
…latest preview version of `Microsoft.CodeAnalysis.NetAnalyzers`. Basically, putting a null-check-override on something once is no longer sufficient to eliminate null-checks down the line, when extensions are used. In truth, a null-check-override was never intended to carry down the line to future uses, but it appeared this way because Roslyn would incorrectly mark values that have the . operator applied to them as not possibly null, not realizing that extension methods make it possible to invoke the . operator upon null. dotnet/roslyn#48605
Version Used:
Steps to Reproduce:
Compile and run the following code:
Expected Behavior:
Warning for possible null dereference in
s.ToString()
;Actual Behavior:
No warnings. The program crashes at runtime with a
NullReferenceException
The text was updated successfully, but these errors were encountered: