var compilation = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
compilation.VerifyDiagnostics(
// (23,17): warning CS8613: Nullability of reference types in return type of 'S?[] B.M2<S>()' doesn't match implicitly implemented member 'S[] IA.M2<S>()'.
// (23,17): warning CS8613: Nullability of reference types in return type of 'S?[] B.M2<S>()' doesn't match implicitly implemented member 'T[] IA.M2<T>()'.
// (18,22): warning CS8613: Nullability of reference types in return type of 'string?[] B.M1()' doesn't match implicitly implemented member 'string[] IA.M1()'.
// (13,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override void F2<T>([MaybeNull]T t1, [MaybeNull] out T t2, [MaybeNull] ref T t3, [MaybeNull] in T t4) where T : class => throw null!; // t2, t3
// (13,26): error CS8762: Type of parameter 't3' doesn't match overridden member because of nullability attributes.
// (13,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override void F2<T>([MaybeNull]T t1, [MaybeNull] out T t2, [MaybeNull] ref T t3, [MaybeNull] in T t4) where T : class => throw null!; // t2, t3
@@ -38435,13 +38435,17 @@ public class Derived2 : Derived
}
";
var comp = CreateNullableCompilation(new[] { MaybeNullWhenAttributeDefinition, source });
// Note: we're missing warnings on F1 because the overridden method with substituted T from Derived has an oblivious T
// https://github.com/dotnet/roslyn/issues/41368
comp.VerifyDiagnostics(
// (13,26): warning CS8765: Type of parameter 't2' doesn't match overridden member because of nullability attributes.
// (12,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F1<T>([MaybeNullWhen(true)]T t1, [MaybeNullWhen(true)] out T t2, [MaybeNullWhen(true)] ref T t3, [MaybeNullWhen(true)] in T t4) => throw null!; // t2, t3
// (12,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F1<T>([MaybeNullWhen(true)]T t1, [MaybeNullWhen(true)] out T t2, [MaybeNullWhen(true)] ref T t3, [MaybeNullWhen(true)] in T t4) => throw null!; // t2, t3
// (13,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNullWhen(true)]T t1, [MaybeNullWhen(true)] out T t2, [MaybeNullWhen(true)] ref T t3, [MaybeNullWhen(true)] in T t4) where T : class => throw null!; // t2, t3
// (13,26): warning CS8765: Type of parameter 't3' doesn't match overridden member because of nullability attributes.
// (13,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNullWhen(true)]T t1, [MaybeNullWhen(true)] out T t2, [MaybeNullWhen(true)] ref T t3, [MaybeNullWhen(true)] in T t4) where T : class => throw null!; // t2, t3
@@ -38472,15 +38476,25 @@ public class Derived : Base
}
";
var comp = CreateNullableCompilation(new[] { MaybeNullWhenAttributeDefinition, source });
// Note: we're missing warnings on F1 and F6 because the overridden method with substituted T from Derived has an oblivious T
// https://github.com/dotnet/roslyn/issues/41368
comp.VerifyDiagnostics(
// (14,26): warning CS8765: Type of parameter 't2' doesn't match overridden member because of nullability attributes.
// (13,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F1<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) => throw null!; // t2, t3
// (13,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F1<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) => throw null!; // t2, t3
// (14,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) where T : class => throw null!; // t2, t3
// (14,26): warning CS8765: Type of parameter 't3' doesn't match overridden member because of nullability attributes.
// (14,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) where T : class => throw null!; // t2, t3
// (18,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F6<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) => throw null!;
// (18,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F6<T>([MaybeNullWhen(false)]T t1, [MaybeNullWhen(false)] out T t2, [MaybeNullWhen(false)] ref T t3, [MaybeNullWhen(false)] in T t4) => throw null!;
// (14,26): warning CS8765: Nullability of type of parameter 't2' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNull]T t1, [MaybeNull] out T t2, [MaybeNull] ref T t3, [MaybeNull] in T t4) where T : class => throw null!; // t2, t3
// (14,26): warning CS8765: Type of parameter 't3' doesn't match overridden member because of nullability attributes.
// (14,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override bool F2<T>([MaybeNull]T t1, [MaybeNull] out T t2, [MaybeNull] ref T t3, [MaybeNull] in T t4) where T : class => throw null!; // t2, t3
@@ -38638,8 +38662,6 @@ public class Derived : Base
[Fact]
public void DisallowNull_Parameter_Generic_OnOverrides()
{
// Note: we're missing warnings on F1 because the overridden method with substituted T from Derived has an oblivious T
// https://github.com/dotnet/roslyn/issues/41368
var source =
@"using System.Diagnostics.CodeAnalysis;
public class Base
...
...
@@ -38661,22 +38683,31 @@ public class Derived : Base
";
var comp = CreateNullableCompilation(new[] { DisallowNullAttributeDefinition, source });
comp.VerifyDiagnostics(
// (14,26): warning CS8765: Type of parameter 't1' doesn't match overridden member because of nullability attributes.
// (12,26): warning CS8765: Nullability of type of parameter 't1' doesn't match overridden member (possibly because of nullability attributes).
// public override void F1<T>([DisallowNull]T t1, [DisallowNull] out T t2, [DisallowNull] ref T t3, [DisallowNull] in T t4) => throw null!; // t1, t3, t4
// (12,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override void F1<T>([DisallowNull]T t1, [DisallowNull] out T t2, [DisallowNull] ref T t3, [DisallowNull] in T t4) => throw null!; // t1, t3, t4
// (12,26): warning CS8765: Nullability of type of parameter 't4' doesn't match overridden member (possibly because of nullability attributes).
// public override void F1<T>([DisallowNull]T t1, [DisallowNull] out T t2, [DisallowNull] ref T t3, [DisallowNull] in T t4) => throw null!; // t1, t3, t4
// (14,26): warning CS8765: Nullability of type of parameter 't1' doesn't match overridden member (possibly because of nullability attributes).
// public override void F3<T>([DisallowNull]T? t1, [DisallowNull] out T? t2, [DisallowNull] ref T? t3, [DisallowNull] in T? t4) where T : class => throw null!; // t1, t3, t4
// (14,26): warning CS8765: Type of parameter 't3' doesn't match overridden member because of nullability attributes.
// (14,26): warning CS8765: Nullability of type of parameter 't3' doesn't match overridden member (possibly because of nullability attributes).
// public override void F3<T>([DisallowNull]T? t1, [DisallowNull] out T? t2, [DisallowNull] ref T? t3, [DisallowNull] in T? t4) where T : class => throw null!; // t1, t3, t4
// (14,26): warning CS8765: Type of parameter 't4' doesn't match overridden member because of nullability attributes.
// (14,26): warning CS8765: Nullability of type of parameter 't4' doesn't match overridden member (possibly because of nullability attributes).
// public override void F3<T>([DisallowNull]T? t1, [DisallowNull] out T? t2, [DisallowNull] ref T? t3, [DisallowNull] in T? t4) where T : class => throw null!; // t1, t3, t4
// (18,20): error CS0452: The type 'TQ' must be a reference type in order to use it as parameter 'TCO' in the generic type or method 'C.FTCO<TCO>(TCO)'
// (86,11): error CS0452: The type 'TO' must be a reference type in order to use it as parameter 'TCO' in the generic type or method 'C.FTCO<TCO>(TCO)'
// (13,19): warning CS8618: Non-nullable property 'Property' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
// public Nested Property { get; set; } // implicitly means C<U>.Nested
// (16,18): warning CS8618: Non-nullable property 'Property2' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
// (13,24): warning CS8618: Non-nullable property 'Property' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
// (16,18): warning CS8618: Non-nullable property 'Property2' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public void IgnoredNullability_ImplementationReturnType_WithoutConstraint()
{
var src = @"
#nullable enable
public interface I
{
T M<T>();
}
public class D : I
{
public T? M<T>() => throw null!;
}
";
var comp = CreateCompilation(src);
comp.VerifyDiagnostics(
// (10,15): warning CS8766: Nullability of reference types in return type of 'T? D.M<T>()' doesn't match implicitly implemented member 'T I.M<T>()' (possibly because of nullability attributes).
public void IgnoredNullability_ImplementationReturnType_WithClassConstraint()
{
var src = @"
#nullable enable
public interface I
{
T M<T>() where T : class;
}
public class D : I
{
public T? M<T>() where T : class => throw null!;
}
";
var comp = CreateCompilation(src);
comp.VerifyDiagnostics(
// (10,15): warning CS8766: Nullability of reference types in return type of 'T? D.M<T>()' doesn't match implicitly implemented member 'T I.M<T>()' (possibly because of nullability attributes).
// public T? M<T>() where T : class => throw null!;