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

Duplicate WRN_NullAsNonNullable diagnoistics is reported for some scenarios #29910

Closed
AlekseyTs opened this issue Sep 14, 2018 · 1 comment
Closed

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void ParameterDefaultValue_03()
        {
            var source =
@"interface I { }
class C : I { }
class P
{
    static void F0<T>(T t = default) { }
    static void F1<T>(T t = null) where T : class { }
    static void F2<T>(T t = default) where T : struct { }
    static void F3<T>(T t = default) where T : new() { }
    static void F4<T>(T t = null) where T : C { }
    static void F5<T>(T t = default) where T : I { }
    static void F6<T, U>(T t = default) where T : U { }
    static void G0()
    {
        F0<object>();
        F0<object>(default);
        F0(new object());
        F1<object>();
        F1<object>(default);
        F1(new object());
        F2<int>();
        F2<int>(default);
        F2(2);
        F3<object>();
        F3<object>(default);
        F3(new object());
        F4<C>();
        F4<C>(default);
        F4(new C());
        F5<I>();
        F5<I>(default);
        F5(new C());
        F6<object, object>();
        F6<object, object>(default);
        F6<object, object>(new object());
    }
    static void G0<T>()
    {
        F0<T>(); // 0
        F0<T>(default); // 0
        F6<T, T>(); // 0
        F6<T, T>(default); // 0
    }
    static void G1<T>() where T : class
    {
        F0<T>(); // 1
        F0<T>(default); // 1
        F1<T>(); // 1
        F1<T>(default); // 1
        F6<T, T>(); // 1
        F6<T, T>(default); // 1
    }
    static void G2<T>() where T : struct
    {
        F0<T>(); // 2
        F0<T>(default); // 2
        F2<T>(); // 2
        F2<T>(default); // 2
        F3<T>(); // 2
        F3<T>(default); // 2
        F6<T, T>(); // 2
        F6<T, T>(default); // 2
    }
    static void G3<T>() where T : new()
    {
        F0<T>(); // 3
        F0<T>(default); // 3
        F0<T>(new T()); // 3
        F3<T>(); // 3
        F3<T>(default); // 3
        F3<T>(new T()); // 3
        F6<T, T>(); // 3
        F6<T, T>(default); // 3
        F6<T, T>(new T()); // 3
    }
    static void G4<T>() where T : C
    {
        F0<T>(); // 4
        F0<T>(default); // 4
        F1<T>(); // 4
        F1<T>(default); // 4
        F4<T>(); // 4
        F4<T>(default); // 4
        F5<T>(); // 4
        F5<T>(default); // 4
        F6<T, T>(); // 4
        F6<T, T>(default); // 4
    }
    static void G5<T>() where T : I
    {
        F0<T>(); // 5
        F0<T>(default); // 5
        F5<T>(); // 5
        F5<T>(default); // 5
        F6<T, T>(); // 5
        F6<T, T>(default); // 5
    }
    static void G6<T, U>() where T : U
    {
        F0<T>(); // 6
        F0<T>(default); // 6
        F6<T, U>(); // 6
        F6<T, U>(default); // 6
    }
}";
            var comp = CreateCompilation(new[] { source, NonNullTypesTrue, NonNullTypesAttributesDefinition });
            // PROTOTYPE(NullableReferenceTypes): Duplicate WRN_NullAsNonNullable diagnoistics at some locations
            comp.VerifyDiagnostics(
                // (6,29): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //     static void F1<T>(T t = null) where T : class { }
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(6, 29),
                // (9,29): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //     static void F4<T>(T t = null) where T : C { }
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(9, 29),
                // (15,20): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<object>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(15, 20),
                // (18,20): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F1<object>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(18, 20),
                // (24,20): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F3<object>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(24, 20),
                // (27,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F4<C>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(27, 15),
                // (30,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<I>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(30, 15),
                // (33,28): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<object, object>(default);
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(33, 28),
                // (39,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 0
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(39, 15),
                // (41,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 0
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(41, 18),
                // (46,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 1
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(46, 15),
                // (48,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F1<T>(default); // 1
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(48, 15),
                // (50,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 1
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(50, 18),
                // (66,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 3
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(66, 15),
                // (69,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F3<T>(default); // 3
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(69, 15),
                // (72,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 3
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(72, 18),
                // (78,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 4
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(78, 15),
                // (80,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F1<T>(default); // 4
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(80, 15),
                // (82,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F4<T>(default); // 4
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(82, 15),
                // (84,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<T>(default); // 4
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(84, 15),
                // (86,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 4
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(86, 18),
                // (91,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(91, 15),
                // (91,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(91, 15),
                // (93,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(93, 15),
                // (93,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(93, 15),
                // (95,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(95, 18),
                // (95,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(95, 18),
                // (100,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 6
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(100, 15),
                // (102,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, U>(default); // 6
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(102, 18)
                );

            // No warnings with C#7.3.
            comp = CreateCompilation(new[] { source, NonNullTypesTrue, NonNullTypesAttributesDefinition }, parseOptions: TestOptions.Regular7_3, skipUsesIsNullable: true);
            comp.VerifyDiagnostics(
                // (1,10): error CS8630: Please use language version 8.0 or greater to use the NonNullTypes attribute.
                // [module: System.Runtime.CompilerServices.NonNullTypes(true)]
                Diagnostic(ErrorCode.ERR_NonNullTypesNotAvailable, "System.Runtime.CompilerServices.NonNullTypes(true)").WithArguments("8.0").WithLocation(1, 10)
                );
        }

Observed duplicates:

                // (91,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(91, 15),
                // (91,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F0<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(91, 15),
                // (93,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(93, 15),
                // (93,15): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F5<T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(93, 15),
                // (95,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(95, 18),
                // (95,18): warning CS8625: Cannot convert null literal to non-nullable reference or unconstrained type parameter.
                //         F6<T, T>(default); // 5
                Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(95, 18),
@cston
Copy link
Member

cston commented Nov 13, 2018

Fixed in #30913?

@cston cston closed this as completed Nov 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants