You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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.
The text was updated successfully, but these errors were encountered:
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.
Observed:
No WRN_NullabilityMismatchInParameterTypeOnExplicitImplementation warning for B.IA.M2. Reported when mismatch is in return type, see existing unit-test ImplementingNullableWithNonNull.
The text was updated successfully, but these errors were encountered: