Skip to content
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

[return: NotNullIfNotNull(paramName)] #6

Open
dgrunwald opened this issue Jun 13, 2020 · 2 comments
Open

[return: NotNullIfNotNull(paramName)] #6

dgrunwald opened this issue Jun 13, 2020 · 2 comments

Comments

@dgrunwald
Copy link
Member

[return: NotNullIfNotNull(paramName)] is a semi-common attribute to use, especially in some code bases that like to use:
if (input == null) return null;
at the start of many functions.

Unlike [NotNullWhen(bool)] for out parameters, I don't see a clean way to infer NotNullIfNotNull with our current algorithm.
But it would be valuable to figure something out, so I'm creating this issue to collect some cases of [NotNullIfNotNull] methods and their constraint graphs.

@dgrunwald
Copy link
Member Author

    /// <summary>
    /// Atomically performs the following operation:
    /// - If target is null: stores newValue in target and returns newValue.
    /// - If target is not null: returns target.
    /// </summary>
    [return: NotNullIfNotNull("newValue")]
    public static T? GetOrSet<T>(ref T? target, T? newValue) where T : class
    {
        T? oldValue = Interlocked.CompareExchange(ref target, newValue, null);
        return oldValue ?? newValue;
    }

GetOrSet
This one is trivial: the nullability of the return value directly depends on the parameter and on no other nodes.

We could detect this special case after building the graph, annotate the method with the attribute, then rebuild the graph from scratch, since callers of the method will profit from the attribute (via 07b2d78), and perform inference as usual in a second pass.

@dgrunwald
Copy link
Member Author

A different case is something like this:

    [return: NotNullIfNotNull("filename")]
    public static Stream? OpenFile(string? filename)
    {
        if (filename == null)
            return null;
        return new FileStream(filename, FileMode.Open);
    }

OpenFile
Here the graph shows no relation between the parameter and the return node.
We could maybe detect that all incoming edges for the return node were created by code under a if (param == null) check (i.e. control-flow guaranteeing that the parameter being definitely null, not just potentially null as tracked by the flow-state implemented in #5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant