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 best type contribute oblivious type arguments #34392

Open
jcouv opened this issue Mar 24, 2019 · 0 comments
Open

Lambdas in best type contribute oblivious type arguments #34392

jcouv opened this issue Mar 24, 2019 · 0 comments

Comments

@jcouv
Copy link
Member

jcouv commented Mar 24, 2019

        [Fact]
        [WorkItem(33664, "https://github.com/dotnet/roslyn/issues/33664")]
        public void ConditionalOperator_WithLambdaConversions()
        {
            var source = @"
using System;
class C
{
    Func<bool, T> D1<T>(T t) => k => t;

    void M1(bool b, string? s)
    {
        _ = (b ? D1(s) : k => s) /*T:System.Func<bool, string?>!*/;
        _ = (b ? k => s : D1(s)) /*T:System.Func<bool, string?>!*/;
        _ = (true ? D1(s) : k => s) /*T:System.Func<bool, string?>!*/;
        _ = (true ? k => s : D1(s)) /*T:System.Func<bool, string>!*/; // unexpected type
        _ = (false ? D1(s) : k => s) /*T:System.Func<bool, string>!*/; // unexpected type
        _ = (false ? k => s : D1(s)) /*T:System.Func<bool, string?>!*/;

        _ = (b ? D1(s!) : k => s) /*T:System.Func<bool, string>!*/; // 1, unexpected type
        _ = (b ? k => s : D1(s!)) /*T:System.Func<bool, string>!*/; // 2, unexpected type
        _ = (true ? D1(s!) : k => s) /*T:System.Func<bool, string>!*/; // unexpected type
        _ = (true ? k => s : D1(s!)) /*T:System.Func<bool, string>!*/; // 3, unexpected type
        _ = (false ? D1(s!) : k => s) /*T:System.Func<bool, string>!*/; // 4, unexpected type
        _ = (false ? k => s : D1(s!)) /*T:System.Func<bool, string>!*/; // unexpected type

        _ = (b ? D1(true ? throw null! : s) : k => s) /*T:System.Func<bool, string>!*/; // 5, unexpected type
        _ = (b ? k => s : D1(true ? throw null! : s)) /*T:System.Func<bool, string>!*/; // 6, unexpected type
        _ = (true ? D1(true ? throw null! : s) : k => s) /*T:System.Func<bool, string>!*/; // unexpected type
        _ = (true ? k => s : D1(true ? throw null! : s)) /*T:System.Func<bool, string>!*/; // 7, unexpected type
        _ = (false ? D1(true ? throw null! : s) : k => s) /*T:System.Func<bool, string>!*/; // 8, unexpected type
        _ = (false ? k => s : D1(true ? throw null! : s)) /*T:System.Func<bool, string>!*/; // unexpected type
    }

    delegate T MyDelegate<T>(bool b);
    ref MyDelegate<T> D2<T>(T t) => throw null!;

    void M(bool b, string? s)
    {
        _ = (b ? ref D2(s) : ref D2(s!)) /*T:C.MyDelegate<string>!*/; // 9
        _ = (b ? ref D2(s!) : ref D2(s)) /*T:C.MyDelegate<string>!*/; // 10
        _ = (true ? ref D2(s) : ref D2(s!)) /*T:C.MyDelegate<string>!*/; // 11
        _ = (true ? ref D2(s!) : ref D2(s)) /*T:C.MyDelegate<string!>!*/;
        _ = (false ? ref D2(s) : ref D2(s!)) /*T:C.MyDelegate<string!>!*/;
        _ = (false ? ref D2(s!) : ref D2(s)) /*T:C.MyDelegate<string>!*/; // 12
    }
}";

            // See TODO2
            // Best type inference involving lambda conversion should agree with method type inference
            // Missing diagnostics

            var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
            comp.VerifyTypes();
            comp.VerifyDiagnostics(
                // (36,14): warning CS8619: Nullability of reference types in value of type 'C.MyDelegate<string?>' doesn't match target type 'C.MyDelegate<string>'.
                //         _ = (b ? ref D2(s) : ref D2(s!)) /*T:C.MyDelegate<string>!*/; // 9
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b ? ref D2(s) : ref D2(s!)").WithArguments("C.MyDelegate<string?>", "C.MyDelegate<string>").WithLocation(36, 14),
                // (37,14): warning CS8619: Nullability of reference types in value of type 'C.MyDelegate<string>' doesn't match target type 'C.MyDelegate<string?>'.
                //         _ = (b ? ref D2(s!) : ref D2(s)) /*T:C.MyDelegate<string>!*/; // 10
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "b ? ref D2(s!) : ref D2(s)").WithArguments("C.MyDelegate<string>", "C.MyDelegate<string?>").WithLocation(37, 14),
                // (38,14): warning CS8619: Nullability of reference types in value of type 'C.MyDelegate<string?>' doesn't match target type 'C.MyDelegate<string>'.
                //         _ = (true ? ref D2(s) : ref D2(s!)) /*T:C.MyDelegate<string>!*/; // 11
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "true ? ref D2(s) : ref D2(s!)").WithArguments("C.MyDelegate<string?>", "C.MyDelegate<string>").WithLocation(38, 14),
                // (41,14): warning CS8619: Nullability of reference types in value of type 'C.MyDelegate<string>' doesn't match target type 'C.MyDelegate<string?>'.
                //         _ = (false ? ref D2(s!) : ref D2(s)) /*T:C.MyDelegate<string>!*/; // unexpected type
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "false ? ref D2(s!) : ref D2(s)").WithArguments("C.MyDelegate<string>", "C.MyDelegate<string?>").WithLocation(41, 14)
                );
        }
        [Fact]
        public void ReturnTypeInference_DelegateTypes()
        {
            var source = @"
class C
{
    System.Func<bool, T> D1<T>(T t) => k => t;
    void M1(bool b, string? s, string s2)
    {
        M2(k => s, D1(s)) /*T:System.Func<bool, string?>!*/;
        M2(D1(s), k => s) /*T:System.Func<bool, string?>!*/;

         M2(k => s2, D1(s2)) /*T:System.Func<bool, string!>!*/;
        M2(D1(s2), k => s2) /*T:System.Func<bool, string!>!*/;

         _ = (new[] { k => s, D1(s) }) /*T:System.Func<bool, string?>![]!*/;
        _ = (new[] { D1(s), k => s }) /*T:System.Func<bool, string?>![]!*/;

         _ = (new[] { k => s2, D1(s2) }) /*T:System.Func<bool, string>![]!*/; // wrong
        _ = (new[] { D1(s2), k => s2 }) /*T:System.Func<bool, string>![]!*/; // wrong
    }
    T M2<T>(T x, T y) => throw null!;
}";
            // See TODO2
            // Best type inference involving lambda conversion should agree with method type inference
            var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
            comp.VerifyTypes();
            comp.VerifyDiagnostics();
        }
@gafter gafter added this to Next Nullable Work in Compiler: Gafter Apr 3, 2019
@gafter gafter added this to Lambda in Nullable Board Apr 11, 2019
@gafter gafter moved this from Next Nullable Work to Nullable in Compiler: Gafter Apr 12, 2019
@gafter gafter self-assigned this Apr 16, 2019
@jcouv jcouv added this to the 16.3 milestone Jun 26, 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