Skip to content

Missing nullability warnings in method group conversions caused by incorrect processing of [NotNullIfNotNull] annotation referencing parameters with type parameter types #49865

@TessenR

Description

@TessenR

Version Used:

Branch master (8 Dec 2020)
Latest commit 1e97fb9 by msftbot[bot]:
Merge pull request #49844 from davidwengier/RestoreInternalVersionAPI

Restore internal API used by VS for Mac

Steps to Reproduce:

Compile and run the following code

#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;

class C
{
    [return: NotNullIfNotNull("t")] static string? MethodGroup<T>(T t) => t?.ToString();
    
    static Func<T, string> GetFunc<T>()
    {
        Func<T, string> func = MethodGroup;
        return func;
    }
    
    static void Main()
    {
        Func<string?, string> func = C.GetFunc<string?>();
        string res = func(null);
        res.ToString();
    }
}

Expected Behavior:
Warning for assignment Func<T, string> func = MethodGroup since return type of MethodGroup is explicitly annotated as returning a nullable type and there are no guarantees about nullability of input parameter T

Actual Behavior:
No warnings. The program crashes at runtime with a NullReferenceException

Notes
I believe this line
https://github.com/RikkiGibson/roslyn/blob/278127fac18b475171495cf209e1d117fce92fbd/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs#L1271

Should be

if (notNullIfParameterNotNull.Contains(overrideParam.Name) && !baseParameters[i].TypeWithAnnotations.GetValueNullableAnnotation().IsAnnotated())

Currently it's

if (notNullIfParameterNotNull.Contains(overrideParam.Name) && !baseParameters[i].TypeWithAnnotations.NullableAnnotation.IsAnnotated())

Which means that not annotated unconstrained generic parameters that might be substituted with annotated types are considered not-nullable

/cc @RikkiGibson

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions