roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Nullable type argument inference bug involving method group

Open RikkiGibson opened this issue 2 years ago • 2 comments

Version Used: f7634f7503665406e717d27bbac581b13a0c21b6

Steps to Reproduce: SharpLab

Based on the report in https://github.com/dotnet/roslyn/issues/44174#issuecomment-1002240375.

See the following 3 scenarios. The first scenario lacks a warning when one is expected, while the second and third scenarios have the expected warning.

#nullable enable
using System;

public class C1 {
    public static T Id<T>(T input) => input;
    public static U Apply<T, U>(T input, Func<T, U> func) => func(input);
    
    public static void Main()
    {
        string? item = null;
        item = Apply(item, Id);
        item.ToString(); // missing expected warning
    }
}

public class C2 {
    public static T Id<T>(T input) => input;
    public static T Apply<T>(T input, Func<T, T> func) => func(input);
    
    public static void Main()
    {
        string? item = null;
        item = Apply(item, Id);
        item.ToString(); // gives expected warning
    }
}

public class C3 {
    public static T Id<T>(T input) => input;
    public static U Apply<T, U>(T input, Func<T, U> func) => func(input);
    
    public static void Main()
    {
        string? item = null;
        item = Apply(item, x => Id(x));
        item.ToString(); // gives expected warning
    }
}

RikkiGibson avatar Jun 26 '22 21:06 RikkiGibson

Do you think https://github.com/dotnet/roslyn/issues/61863 could be related?

jnm2 avatar Aug 01 '22 15:08 jnm2

In this case, the correct behavior occurs with a lambda but not with a method group. It looks like your scenario behaves incorrectly with a lambda so probably a separate bug.

RikkiGibson avatar Aug 01 '22 19:08 RikkiGibson