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

GM interfaces implementations can be self constrained #6168

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol symbo
{
if (IsKnownInterfaceInTheChain(anInterface) &&
FirstTypeParameterNameIsNotTheSymbolName(symbol, anInterface) &&
(!symbol.IsGenericType || NotConstrainedToTheInterface(anInterface, symbol.TypeParameters)))
(!symbol.IsGenericType || NotConstrainedToTheInterfaceOrSelf(anInterface, symbol)))
{
SyntaxNode? typeParameter = FindTheTypeArgumentOfTheInterfaceFromTypeDeclaration(symbol, anInterface);
context.ReportDiagnostic(CreateDiagnostic(GMIRule, typeParameter, anInterface.OriginalDefinition.ToDisplayString(
Expand All @@ -79,7 +79,7 @@ private void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol symbo
{
if (IsKnownInterfaceInTheChain(anInterface) &&
FirstTypeParameterNameIsNotTheSymbolName(symbol, anInterface) &&
(!symbol.IsGenericType || NotConstrainedToTheInterface(anInterface, symbol.TypeParameters)))
(!symbol.IsGenericType || NotConstrainedToTheInterfaceOrSelf(anInterface, symbol)))
{
SyntaxNode? typeParameter = FindTheTypeArgumentOfTheInterfaceFromTypeDeclaration(symbol, symbol.BaseType);
context.ReportDiagnostic(CreateDiagnostic(GMIRule, typeParameter, symbol.BaseType.OriginalDefinition.ToDisplayString(
Expand Down Expand Up @@ -113,13 +113,14 @@ bool IsKnownInterfaceInTheChain(INamedTypeSymbol anInterface)
return false;
}

static bool NotConstrainedToTheInterface(INamedTypeSymbol anInterface, ImmutableArray<ITypeParameterSymbol> typeParameters)
static bool NotConstrainedToTheInterfaceOrSelf(INamedTypeSymbol anInterface, INamedTypeSymbol symbol)
{
foreach (var typeParameter in typeParameters)
foreach (var typeParameter in symbol.TypeParameters)
{
foreach (var constraint in typeParameter.ConstraintTypes)
{
if (constraint.Equals(anInterface, SymbolEqualityComparer.Default))
if (constraint.Equals(symbol) ||
constraint.Equals(anInterface, SymbolEqualityComparer.Default))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,40 @@ public interface IParsable<TSelf> where TSelf : IParsable<TSelf>
}").RunAsync();
}

[Fact]
public async Task SelfConstrainedInterfaceDerivedFromGMInterfaceTest()
{
await PopulateTestCs(@"
using System;

namespace MyNamespace
{
public interface IMyInterface<TSelf> : IParsable<TSelf> where TSelf : IMyInterface<TSelf>
{ }
}").RunAsync();
}

[Fact]
public async Task SelfConstrainedClassDerivedFromGMInterfaceTest()
{
await PopulateTestCs(@"
using System;

public class MyDate<TSelf> : IParsable<TSelf> where TSelf : MyDate<TSelf>
{
public static TSelf Parse(string s, IFormatProvider provider)
{
throw new NotImplementedException();
}

public static bool TryParse(string s, IFormatProvider provider, out TSelf result)
{
throw new NotImplementedException();
}
}
").RunAsync();
}

[Fact]
public async Task InterfacesImplementedCorrectlyNotWarn()
{
Expand Down