Support NotNullIfNotNullAttribute#19977
Conversation
❗ Release notes requiredYou can open this PR in browser to add release notes: open in github.dev
|
|
🔍 Tooling Safety Check — Affects-Compiler-Output
|
| open System.IO | ||
|
|
||
| let maybeNull : string | null = "file.txt" | ||
| let ext : string = Path.GetExtension(maybeNull) |
There was a problem hiding this comment.
I remember especially the Path APIs being heavy users of that - can we eliminate some "useless" null checking now? ( I guess too much hassle to do it via ifdefs now?)
There was a problem hiding this comment.
Other than the 2 usages I had to adjust, it seems the return values are either used in a way where nullness doesn't matter (passing to String.Equals) or Unchecked.nonNull is used. I'd probably just strip the latter later once the new SDK has flowed back into the repo.
| #nullable enable | ||
| using System.Diagnostics.CodeAnalysis; | ||
| namespace NotNullLib { | ||
| public class C { |
There was a problem hiding this comment.
Does C# support it also on generic arguments? (possibly also non-restricted to reference types, I remember some craziness with T? working )
There was a problem hiding this comment.
Yes,
[return: NotNullIfNotNull(nameof(x))]
public static T? Echo<T>(T? x) => x;|
|
||
| // The result is non-null when the SECOND parameter is non-null. | ||
| [return: NotNullIfNotNull("second")] | ||
| public static string? DependsOnSecond(string? first, string? second) => second; |
There was a problem hiding this comment.
Could you pls also do a test for type inference of an F# function argument ? And assert what is being inferred.
let f x =
C.DependsOnSecond(x,x)
| open System.IO | ||
|
|
||
| let maybeNull : string | null = "file.txt" | ||
| let ext : string = Path.GetExtension(maybeNull) |
There was a problem hiding this comment.
Can you pls also do one where the null literal is passed directly as a method argument? Both when inlined into the method application, or as vanilla let x = null above.
There have been some regressions due to how the literal is type inferred initially.
Only supported on C# and F# methods. Functions use an entirely different pathway. Also only one instance of the attribute is supported because at that point we can't evaluate nullness of relevant arguments to link the right one (but maybe we could introduce a new type of nullness linkage). It's also exceedingly rare...
Checklist