You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nullness issue - type inference on pattern match for nullable values is unexpected #18012
Area-NullnessIssues related to handling of Nullable Reference TypesBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.
with blip being inferred as type string. The above should reasonably be inferred as string | null, and I believe the equivalent of the above using if / then / else does indeed do that.
Choose one or more from the following categories of impact
Unexpected nullness warning (false positive in nullness checking, code uses --checknulls and langversion:preview).
Missing nullness warning in a case which can produce nulls (false negative, code uses --checknulls and langversion:preview).
Breaking change related to older null constructs in code not using the checknulls switch.
Breaking change related to generic code and explicit type constraints (null, not null).
Type inference issue (i.e. code worked without type annotations before, and applying the --checknulls enforces type annotations).
C#/F# interop issue related to nullness metadata.
Other (none of the categories above apply).
Operating System
Windows (Default)
What .NET runtime/SDK kind are you seeing the issue on
.NET SDK (.NET Core, .NET 5+)
.NET Runtime/SDK version
.NET 9
Reproducible code snippet and actual behavior
letblip=matchtruewith|true->null|false->"test"
Possible workarounds
Add a type annotation on blip e.g. blip : string | null.
Just to add to this - I'm not sure what one might expect to see if you flip the order around - I suspect the current behaviour is correct; I wouldn't want the compiler to infer (string | null) if I accidentally return a null on a subsequent branch.
IOW, perhaps first branch should "win" when determining the nullness of an expression?
Area-NullnessIssues related to handling of Nullable Reference TypesBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.
Activity
isaacabraham commentedon Nov 16, 2024
Just to add to this - I'm not sure what one might expect to see if you flip the order around - I suspect the current behaviour is correct; I wouldn't want the compiler to infer (string | null) if I accidentally return a null on a subsequent branch.
IOW, perhaps first branch should "win" when determining the nullness of an expression?
isaacabraham commentedon Nov 16, 2024
FYI: The behavior in
ifthenelseconstructs behaves exactly as what I am looking for. Pattern matching should IMHO follow that.T-Gro commentedon Nov 18, 2024
Yes, this should have been the case. It is a regression that it isn't.
T-Gro commentedon Nov 28, 2024
#18015 (comment)