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

Nullability mismatch in parameter of explicit interface implementation is not detected #30845

Closed
AlekseyTs opened this issue Oct 30, 2018 · 2 comments

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void ImplementingNullableWithNonNull_Parameter()
        {
            var source = @"
interface IA
{
" + NonNullTypesOff() + @"
    void M1(string?[] x);
" + NonNullTypesOff() + @"
    void M2<T>(T?[] x) where T : class;
}
" + NonNullTypesOn() + @"
class B : IA
{
    void IA.M1(string[] x) => throw null;
    void IA.M2<S>(S[] x)
        => throw null;
}
";
            var compilation = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
            compilation.VerifyDiagnostics(
                // (5,19): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
                //     void M1(string?[] x);
                Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(5, 19),
                // (7,16): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
                //     void M2<T>(T?[] x) where T : class;
                Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(7, 16),
                // (7,17): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
                //     void M2<T>(T?[] x) where T : class;
                Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(7, 17),
                // (12,13): warning CS8617: Nullability of reference types in type of parameter 'x' doesn't match implemented member 'void IA.M1(string?[] x)'.
                //     void IA.M1(string[] x) => throw null;
                Diagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOnExplicitImplementation, "M1").WithArguments("x", "void IA.M1(string?[] x)").WithLocation(12, 13)
                );
        }

Observed:
No WRN_NullabilityMismatchInParameterTypeOnExplicitImplementation warning for B.IA.M2. Reported when mismatch is in return type, see existing unit-test ImplementingNullableWithNonNull.

@gafter
Copy link
Member

gafter commented Oct 30, 2018

I don't see why either warning is deserved. In general, contravariant changes in an argument's nullability, and covariant changes in the return type nullability are safe and do not deserve a warning.

@gafter
Copy link
Member

gafter commented Oct 30, 2018

Oh, I got that backwards! Both deserve a warning in this test.

This issue was closed.
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