NullabilityInference icon indicating copy to clipboard operation
NullabilityInference copied to clipboard

[return: NotNullIfNotNull(paramName)]

Open dgrunwald opened this issue 4 years ago • 2 comments

[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 avatar Jun 13 '20 22:06 dgrunwald

    /// <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 07b2d7842cb49efcdaa2682f4c7560520661e0f4), and perform inference as usual in a second pass.

dgrunwald avatar Jun 13 '20 22:06 dgrunwald

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).

dgrunwald avatar Jun 14 '20 18:06 dgrunwald