Roslyn fails to correctly infer nullability of foreach variables' types if deconstruction is used #56352
Labels
Area-Compilers
Bug
Concept-Null Annotations
The issue involves annotating an API for nullable reference types
Projects
Milestone
Version Used:
Steps to Reproduce:
Compile and run the following code
Expected Behavior:
Possible null dereference
warning forx.field.ToString()
inM1
Nullability of reference types in value of type 'C<string?>' doesn't match target type 'C<string>'
warning forreturn z
inM1
Actual Behavior:
No warnings at all in the program above.
It crashes with a
NullReferenceException
if you run it due to the missing warning forx.field.ToString()
;If you comment the
x.field.ToString()
line inM1
it crashes atx.field.ToString()
inMain
due to the missing warning onreturn z
Notes:
It looks like Roslyn fails to infer any nullability of type arguments at all for all variables in
M1
so it allows both dereferencing.field
and assigning it withnull
values without any warnings as if it thinks their type arguments have unknown nullability.Interestingly, you'll correctly get both warnings if you change the cycle to not use deconstruction i.e. change the foreach cycle header to the following:
The text was updated successfully, but these errors were encountered: