Skip to content

Commit c5f4c27

Browse files
authored
Do not warn about an oblivious mismatch for an explicitly implemented interface. (#36761)
Related to #35619.
1 parent 8ae86d7 commit c5f4c27

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

src/Compilers/CSharp/Portable/Symbols/Source/ExplicitInterfaceHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static Symbol FindExplicitlyImplementedMember(
207207
// interface in its base class list that contains a member ..."
208208
MultiDictionary<NamedTypeSymbol, NamedTypeSymbol>.ValueSet set = containingType.InterfacesAndTheirBaseInterfacesNoUseSiteDiagnostics[explicitInterfaceNamedType];
209209
int setCount = set.Count;
210-
if (setCount == 0 || !set.Contains(explicitInterfaceNamedType))
210+
if (setCount == 0 || !set.Contains(explicitInterfaceNamedType, TypeSymbol.EqualsObliviousNullableModifierMatchesAny))
211211
{
212212
//we'd like to highlight just the type part of the name
213213
var explicitInterfaceSyntax = explicitInterfaceSpecifierSyntax.Name;

src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ private InterfaceInfo GetInterfaceInfo()
125125

126126
internal static readonly EqualityComparer<TypeSymbol> EqualsIgnoringNullableComparer = new TypeSymbolComparer(TypeCompareKind.IgnoreNullableModifiersForReferenceTypes);
127127

128+
internal static readonly EqualityComparer<TypeSymbol> EqualsObliviousNullableModifierMatchesAny = new TypeSymbolComparer(TypeCompareKind.ObliviousNullableModifierMatchesAny);
129+
128130
internal static readonly EqualityComparer<TypeSymbol> EqualsAllIgnoreOptionsPlusNullableWithUnknownMatchesAnyComparer =
129131
new TypeSymbolComparer(TypeCompareKind.AllIgnoreOptions & ~(TypeCompareKind.IgnoreNullableModifiersForReferenceTypes));
130132

src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -85371,11 +85371,7 @@ void Interface.Method()
8537185371
}
8537285372
";
8537385373
var comp = CreateNullableCompilation(text);
85374-
comp.VerifyDiagnostics(
85375-
// (10,14): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85376-
// void Interface.Method()
85377-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "Interface").WithLocation(10, 14)
85378-
);
85374+
comp.VerifyDiagnostics();
8537985375
}
8538085376

8538185377
[Fact]
@@ -85402,11 +85398,7 @@ void Inner<U>.Interface.Method()
8540285398
}
8540385399
}
8540485400
";
85405-
CreateCompilation(text, options: WithNonNullTypesTrue()).VerifyDiagnostics(
85406-
// (14,18): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85407-
// void Inner<U>.Interface.Method()
85408-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "Inner<U>.Interface").WithLocation(14, 18)
85409-
);
85401+
CreateCompilation(text, options: WithNonNullTypesTrue()).VerifyDiagnostics();
8541085402
}
8541185403

8541285404
[Fact]
@@ -85466,15 +85458,7 @@ void Outer<
8546685458
}
8546785459
";
8546885460

85469-
CreateCompilation(text, options: WithNonNullTypesTrue()).VerifyDiagnostics(
85470-
// (14,18): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85471-
// void Outer<
85472-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, @"Outer<
85473-
#nullable disable
85474-
T
85475-
#nullable enable
85476-
>.Inner<U>.Interface").WithLocation(14, 18)
85477-
);
85461+
CreateCompilation(text, options: WithNonNullTypesTrue()).VerifyDiagnostics();
8547885462
}
8547985463

8548085464
[Fact]
@@ -85555,15 +85539,9 @@ void Inner<U>.Interface<U, T>.Method<K>(T a, U[] b, List<U> c, Dictionary<T, K>
8555585539
// (14,39): error CS0535: 'Outer<T>.Inner<U>.Derived4.Derived5' does not implement interface member 'Outer<T>.Inner<U>.Interface<U, T>.Method<Z>(T, U[], List<U>, Dictionary<T, Z>)'
8555685540
// internal class Derived5 : Outer<T>.Inner<U>.Interface<U, T>
8555785541
Diagnostic(ErrorCode.ERR_UnimplementedInterfaceMember, "Outer<T>.Inner<U>.Interface<U, T>").WithArguments("Outer<T>.Inner<U>.Derived4.Derived5", "Outer<T>.Inner<U>.Interface<U, T>.Method<Z>(T, U[], System.Collections.Generic.List<U>, System.Collections.Generic.Dictionary<T, Z>)").WithLocation(14, 39),
85558-
// (20,22): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85559-
// void Inner<U>.Interface<U, T>.Method<K>(T a, U[] b, List<U> c, Dictionary<K, T> D)
85560-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "Inner<U>.Interface<U, T>").WithLocation(20, 22),
8556185542
// (20,47): error CS0539: 'Outer<T>.Inner<U>.Derived4.Derived5.Method<K>(T, U[], List<U>, Dictionary<K, T>)' in explicit interface declaration is not found among members of the interface that can be implemented
8556285543
// void Inner<U>.Interface<U, T>.Method<K>(T a, U[] b, List<U> c, Dictionary<K, T> D)
85563-
Diagnostic(ErrorCode.ERR_InterfaceMemberNotFound, "Method").WithArguments("Outer<T>.Inner<U>.Derived4.Derived5.Method<K>(T, U[], System.Collections.Generic.List<U>, System.Collections.Generic.Dictionary<K, T>)").WithLocation(20, 47),
85564-
// (30,22): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85565-
// void Inner<U>.Interface<U, T>.Method<K>(T a, U[] b, List<U> c, Dictionary<T, K> D)
85566-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "Inner<U>.Interface<U, T>").WithLocation(30, 22)
85544+
Diagnostic(ErrorCode.ERR_InterfaceMemberNotFound, "Method").WithArguments("Outer<T>.Inner<U>.Derived4.Derived5.Method<K>(T, U[], System.Collections.Generic.List<U>, System.Collections.Generic.Dictionary<K, T>)").WithLocation(20, 47)
8556785545
);
8556885546
}
8556985547

@@ -85596,11 +85574,7 @@ void Inner<U>.Interface<long, string>.Method<K>(T a, U[] B, List<long> C, Dictio
8559685574
}
8559785575
";
8559885576

85599-
CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics(
85600-
// (18,18): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85601-
// void Inner<U>.Interface<long, string>.Method<K>(T a, U[] B, List<long> C, Dictionary<string, K> d)
85602-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "Inner<U>.Interface<long, string>").WithLocation(18, 18)
85603-
);
85577+
CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics();
8560485578
}
8560585579

8560685580
[Fact]
@@ -85705,17 +85679,11 @@ void I1<C1<CT1, CT2>>.M()
8570585679
}
8570685680
}
8570785681
";
85708-
var expected = new DiagnosticDescription[] {
85709-
// (4,10): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85710-
// void I1<C1<CT1, CT2>>.M()
85711-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "I1<C1<CT1, CT2>>").WithLocation(4, 10)
85712-
};
85713-
8571485682
var comp2 = CreateCompilation(source2, options: WithNonNullTypesTrue(), references: new[] { comp1.EmitToImageReference() });
85715-
comp2.VerifyDiagnostics(expected);
85683+
comp2.VerifyDiagnostics();
8571685684

8571785685
var comp3 = CreateCompilation(source2, options: WithNonNullTypesTrue(), references: new[] { comp1.ToMetadataReference() });
85718-
comp3.VerifyDiagnostics(expected);
85686+
comp3.VerifyDiagnostics();
8571985687
}
8572085688

8572185689
[Fact]
@@ -85967,10 +85935,7 @@ void I1<C1<CT1, CT2>>.M()
8596785935
var expected = new DiagnosticDescription[] {
8596885936
// (2,7): warning CS8645: 'I1<C1<CT1, CT2>>' is already listed in the interface list on type 'C<CT1, CT2>' with different nullability of reference types.
8596985937
// class C<CT1, CT2> : I2<CT1, CT2>,
85970-
Diagnostic(ErrorCode.WRN_DuplicateInterfaceWithNullabilityMismatchInBaseList, "C").WithArguments("I1<C1<CT1, CT2>>", "C<CT1, CT2>").WithLocation(2, 7),
85971-
// (7,10): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
85972-
// void I1<C1<CT1, CT2>>.M()
85973-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "I1<C1<CT1, CT2>>").WithLocation(7, 10)
85938+
Diagnostic(ErrorCode.WRN_DuplicateInterfaceWithNullabilityMismatchInBaseList, "C").WithArguments("I1<C1<CT1, CT2>>", "C<CT1, CT2>").WithLocation(2, 7)
8597485939
};
8597585940

8597685941
var comp2 = CreateCompilation(source2, options: WithNonNullTypesTrue(), references: new[] { comp1.EmitToImageReference() });
@@ -86015,17 +85980,12 @@ void I1<C1<CT1, CT2>>.M()
8601585980
}
8601685981
}
8601785982
";
86018-
var expected = new DiagnosticDescription[] {
86019-
// (9,10): warning CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
86020-
// void I1<C1<CT1, CT2>>.M()
86021-
Diagnostic(ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface, "I1<C1<CT1, CT2>>").WithLocation(9, 10)
86022-
};
8602385983

8602485984
var comp2 = CreateCompilation(source2, options: WithNonNullTypesTrue(), references: new[] { comp1.EmitToImageReference() });
86025-
comp2.VerifyDiagnostics(expected);
85985+
comp2.VerifyDiagnostics();
8602685986

8602785987
var comp3 = CreateCompilation(source2, options: WithNonNullTypesTrue(), references: new[] { comp1.ToMetadataReference() });
86028-
comp3.VerifyDiagnostics(expected);
85988+
comp3.VerifyDiagnostics();
8602985989
}
8603085990

8603185991
[Fact]

0 commit comments

Comments
 (0)