Skip to content

CS8602 false positive due to inferred generic type argument being nullable #41705

Open
@AArnott

Description

Version Used: 16.6 Preview 1.0 (29813.275.master)

Steps to Reproduce:

Create a C# project with this content:

using System;

#nullable enable

public class VSTHRD109AvoidAssertInAsyncMethodsCodeFix
{
    public void ApplyChangeOptimistically()
    {
        var n = new GenericParameterHelper(1);
        ApplyChangeOptimistically(ref n, i => new GenericParameterHelper(i.Data + 1));
        int x = n.Data;
    }

    public static bool ApplyChangeOptimistically<T>(ref T hotLocation, Func<T, T> applyChange)
        where T : class?
    {
        return false;
    }
}

public class GenericParameterHelper
{
    public GenericParameterHelper(int data)
    {
        this.Data = data;
    }

    public int Data { get; set; }
}

Expected Behavior:

No warnings.

Actual Behavior:

Two CS8602 warnings where the Data property is consumed.

image

This is evidently because the inferred generic type argument is nullable:

image

If I explicitly specify the generic type argument as GenericParameterHelper, the compiler warnings go away. But why would C# think that GenericParameterHelper? was the generic type argument? Nothing was nullable anywhere. The n local variable is typed as GenericParameterHelper -- not GenericParameterHelper?.

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions