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

Lambdas in array initializers not checked in nullable walker #35302

Open
gafter opened this issue Apr 26, 2019 · 1 comment
Open

Lambdas in array initializers not checked in nullable walker #35302

gafter opened this issue Apr 26, 2019 · 1 comment

Comments

@gafter
Copy link
Member

gafter commented Apr 26, 2019

Lambda expressions in an array initializer are not checked against the new target type of the lambda expression. Instead, the type from initial binding is used, and (improperly) feeds into inference.

        [Fact, WorkItem(35302, "https://github.com/dotnet/roslyn/issues/35302")]
        public void CheckLambdaInArrayInitializer_02()
        {
            var source =
@"using System;

class C
{
    static void G(object? o, string s)
    {
        if (o == null) return;
        var f = M(o);
        _ = f /*T:System.Func<object!>!*/;
        var fa3 = new[] {
            f,
            () => null, // 1
            () => {
                    s = null; // 2
                    return null; // 3
                },
            };
        _ = fa3 /*T:System.Func<object!>![]!*/;
        fa3[0]().ToString();
    }
    static Func<T> M<T>(T t) => () => t;
}
";
            var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
            comp.VerifyTypes();
            comp.VerifyDiagnostics(
                // (12,19): warning CS8603: Possible null reference return.
                //             () => null, // 1
                Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(12, 19),
                // (14,25): warning CS8600: Converting null literal or possible null value to non-nullable type.
                //                     s = null; // 2
                Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(14, 25),
                // (15,28): warning CS8603: Possible null reference return.
                //                     return null; // 3
                Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(15, 28));
        }

There may be similar issues in method type inference, ?:, switch expressions, etc.

@gafter
Copy link
Member Author

gafter commented Apr 26, 2019

A couple of tests are skipped on this issue.

@gafter gafter added this to Nullable in Compiler: Gafter Apr 26, 2019
@gafter gafter added this to Lambda in Nullable Board Apr 27, 2019
@gafter gafter modified the milestones: 16.2, 16.3 Jun 3, 2019
@jcouv jcouv modified the milestones: 16.3, Compiler.Next Jul 10, 2019
@gafter gafter removed their assignment Jul 14, 2019
@gafter gafter removed this from Nullable in Compiler: Gafter Jul 14, 2019
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Nullable Board
Local functions and lambdas
Development

No branches or pull requests

3 participants