-
Notifications
You must be signed in to change notification settings - Fork 788
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
Nullness issue - NotNullWhenAttribute not taken into account for byref? #18018
Comments
Essentially a duplicate of #18019, this will require introducing a full blown flow analysis "engine" and significant changes to type inference. |
For this attribute specifically, I would recommend wrapping it into a custom active pattern which would do both the branching parsed/not parsed logic, as well as binding for the non-null values of ipString and address. |
let (|InvalidIP|ValidIP|) (ipString:string|null) =
match IPAddress.TryParse ipString with
| true, NonNull ipAddress -> ValidIP ipAddress
| _ -> InvalidIP |
Thanks. I think it's beter to just use let validIpAddress (str: string) : Result<IPAddress, string> =
match IPAddress.TryParse str with
| true, NonNullQuick ip -> Ok ip
| _ -> Error "The value must be a valid IP address" |
NonNullQuick will fail with an exception if the value turns out to be null at runtime. If you can trust the API's contract, both should be fine. |
Sure, and since this is the BCL, I believe the API contract can be trusted. In any case, my main point was using the active pattern (either, really) inside the original function instead of rewriting the function to a separate active pattern, which is not needed and orthogonal to this issue. |
Flow analysis is not part of F# NRTs , it is a Roslyn-only feature for C# as of now. Adding flow analysis would be a separate (large) change for the compiler - not saying it will never happen, but is not like other bugs. |
Issue description
System.Net.IPAddress.TryParse
has this signature in Rider:IPAddress.TryParse([<NotNullWhenAttribute (true)>] ipString: string, [<NotNullWhenAttribute (true)>] address: byref<IPAddress>) : bool
My understanding is that this should indicate that when the function returns
true
, then the byrefIPAddress
is not null. However, F# seems to think theip
value van be null in the code below:Choose one or more from the following categories of impact
null
constructs in code not using the checknulls switch.null
,not null
).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 SDK 9.0.100
Reproducible code snippet and actual behavior
Repro solution: Repro.zip
Possible workarounds
Add
NonNullQuick
:The text was updated successfully, but these errors were encountered: