var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,11): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y);
// (7,11): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (8,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, x);
// (8,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (8,17): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, x);
// (8,17): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (10,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// (10,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (10,20): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// (10,20): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (11,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: x, x: y);
// (11,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (11,20): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: x, x: y);
// (11,20): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (15,14): warning CS8620: Nullability of reference types in argument of type 'I<object>[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, new[] { x, x });
// (15,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (17,14): warning CS8620: Nullability of reference types in argument of type 'I<object>?[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, new[] { x, y, z });
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, y, z }").WithArguments("I<object>?[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(17, 14),
// (17,25): warning CS8619: Nullability of reference types in value of type 'I<object?>' doesn't match target type 'I<object>'.
// (18,14): warning CS8620: Nullability of reference types in argument of type 'I<object>[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// (18,33): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// (19,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, new[] { x, y, z }); // 12, 13
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, y, z }").WithArguments("I<object>[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(19, 14),
// (19,25): warning CS8619: Nullability of reference types in value of type 'I<object?>' doesn't match target type 'I<object>'.
// (20,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// (20,33): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
public void NotNullConstrainedParam_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(T t) where T : notnull { }
void M1(string? s)
{
M0(s); // 1
_ = s.ToString(); // 2
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,9): warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'Program.M0<T>(T)'. Nullability of type argument 'string?' doesn't match 'notnull' constraint.
public void Params_NotNullConstrainedElement_AnnotatedArray_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(params T[]? s0) where T : notnull { }
void M1(string? s1)
{
M0(s1); // 1
_ = s1.ToString(); // 2
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,9): warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'Program.M0<T>(params T[]?)'. Nullability of type argument 'string?' doesn't match 'notnull' constraint.
// (20,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (26,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (21,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (27,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (20,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (26,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (21,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// (27,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (19,28): warning CS8620: Nullability of reference types in argument of type 'D<object?>' doesn't match target type 'D<object>' for parameter 'key' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)'.
// _ = new D<int>() { y, y }; // warn 5 and 6
// (19,28): warning CS8620: Argument of type 'D<object?>' cannot be used for parameter 'key' of type 'D<object>' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)' due to differences in the nullability of reference types.
// (19,32): warning CS8620: Nullability of reference types in argument of type 'D<object?>' doesn't match target type 'D<object>' for parameter 'key' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)'.
// _ = new D<int>() { y, y }; // warn 5 and 6
// (19,32): warning CS8620: Argument of type 'D<object?>' cannot be used for parameter 'key' of type 'D<object>' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)' due to differences in the nullability of reference types.
// (9,28): warning CS8620: Nullability of reference types in argument of type 'C<object>' doesn't match target type 'C<object?>' for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// (9,28): warning CS8620: Argument of type 'C<object>' cannot be used for parameter 'key' of type 'C<object?>' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)' due to differences in the nullability of reference types.
// (9,31): warning CS8620: Nullability of reference types in argument of type 'C<object>' doesn't match target type 'C<object?>' for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// (9,31): warning CS8620: Argument of type 'C<object>' cannot be used for parameter 'key' of type 'C<object?>' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)' due to differences in the nullability of reference types.
// (8,12): warning CS8620: Argument of type 'G<string?>' cannot be used as an input of type 'G<string>' for parameter 'o' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (7,19): warning CS8604: Possible null reference argument for parameter 'o' in 'bool G<T>.M1(object o)'.
// (8,12): warning CS8620: Argument of type 'G<string?>' cannot be used for parameter 'o' of type 'G<string>' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (14,16): warning CS8620: Argument of type 'G<string?>' cannot be used as an input of type 'G<string>' for parameter 'o' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (14,16): warning CS8620: Argument of type 'G<string?>' cannot be used for parameter 'o' of type 'G<string>' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (8,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// (9,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// (8,16): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// (9,16): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// (11,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.