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

UnappliedConditionalImmutability: raise per-parameter #960

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/D2L.CodeStyle.Analyzers/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public static class Diagnostics {
public static readonly DiagnosticDescriptor UnappliedConditionalImmutability = new DiagnosticDescriptor(
id: "D2L0103",
title: "A type parameter marked [ConditionallyImmutable.OnlyIf] was not applied to a [ConditionallyImmutable] implemented type",
messageFormat: "{0} should apply all [ConditionallyImmutable.OnlyIf] type parameters to the [ConditionallyImmutable] {1} {2}",
messageFormat: "The conditional type parameter <{0}> was not applied to the [ConditionallyImmutable] {2} {3}",
category: "Correctness",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,39 +139,41 @@ void RaiseMissingAttribute() {
}

void InspectConditionalParameterApplication() {
foreach( ITypeParameterSymbol typeParameter in typeInfo.ConditionalTypeParameters ) {
foreach( var p in typeInfo.GetConditionalTypeParameters() ) {
bool parameterUsed = false;
for( int i = 0; i < baseTypeInfo.Type.TypeArguments.Length && !parameterUsed; i++ ) {
ITypeSymbol typeArgument = baseTypeInfo.Type.TypeArguments[ i ];
if( !SymbolEqualityComparer.Default.Equals( typeParameter, typeArgument ) ) {
foreach( var bp in baseTypeInfo.GetConditionalTypeParameters() ) {
ITypeSymbol typeArgument = baseTypeInfo.Type.TypeArguments[ bp.OriginalOrdinal ];

if( !SymbolEqualityComparer.Default.Equals( p.TypeParameter, typeArgument ) ) {
continue;
}

ITypeParameterSymbol baseTypeParameter = baseTypeInfo.Type.TypeParameters[ i ];
if( baseTypeInfo.ConditionalTypeParameters.Contains( baseTypeParameter, SymbolEqualityComparer.Default ) ) {
parameterUsed = true;
break;
}
}
parameterUsed = true;
break;
};

if( !parameterUsed ) {
(TypeDeclarationSyntax syntax, _) = typeInfo.Type.ExpensiveGetSyntaxImplementingType(
baseTypeOrInterface: baseTypeInfo.Type,
compilation: m_compilation,
cancellationToken
);
m_diagnosticSink(
Diagnostic.Create(
Diagnostics.UnappliedConditionalImmutability,
syntax.Identifier.GetLocation(),
typeInfo.Type.GetFullTypeName(),
baseTypeInfo.Type.TypeKind == TypeKind.Interface ? "interface" : "base class",
baseTypeInfo.Type.GetFullTypeName()
)
);
return;
if( parameterUsed ) {
continue;
}
}

(TypeDeclarationSyntax syntax, _) = typeInfo.Type.ExpensiveGetSyntaxImplementingType(
baseTypeOrInterface: baseTypeInfo.Type,
compilation: m_compilation,
cancellationToken
);
omsmith marked this conversation as resolved.
Show resolved Hide resolved

TypeParameterSyntax parameterSyntax = syntax.TypeParameterList!.Parameters[ p.OriginalOrdinal ];

m_diagnosticSink(
Diagnostic.Create(
Diagnostics.UnappliedConditionalImmutability,
parameterSyntax.Identifier.GetLocation(),
p.TypeParameter.Name,
baseTypeInfo.Type.TypeKind == TypeKind.Interface ? "interface" : "base class",
baseTypeInfo.Type.GetFullTypeName()
)
);
};
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/D2L.CodeStyle.Analyzers/Immutability/ImmutableTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ ImmutableArray<bool> conditionalTypeParameters

public bool IsConditional { get; }

public IEnumerable<ITypeParameterSymbol> ConditionalTypeParameters => Type.TypeParameters.Where( IsConditionalParameter );
private bool IsConditionalParameter( ITypeParameterSymbol _, int index ) => m_conditionalTypeParameters[ index ];
public IEnumerable<(ITypeParameterSymbol TypeParameter, int OriginalOrdinal)> GetConditionalTypeParameters() {
if( !IsConditional ) {
yield break;
}

for( int i = 0; i < Type.TypeParameters.Length; i++ ) {
if( !m_conditionalTypeParameters[ i ] ) {
continue;
}

yield return (Type.TypeParameters[ i ], i);
}
}

public bool IsImmutableDefinition(
ImmutabilityContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ public sealed class ImmutableClassImplementingConditionallyImmutable<[Immutable]
public sealed class ConditionallyImmutableClassImplementingConditionallyImmutable<[ConditionallyImmutable.OnlyIf] T> : ISomethingConditionallyImmutable<T> { }

[ConditionallyImmutable]
public sealed class /* UnappliedConditionalImmutability(ConsistencyTests.ConditionallyImmutableImplementerWithUnappliedCondition, interface, ConsistencyTests.ISomethingConditionallyImmutable) */ ConditionallyImmutableImplementerWithUnappliedCondition /**/<[ConditionallyImmutable.OnlyIf] T, [ConditionallyImmutable.OnlyIf] U> : ISomethingConditionallyImmutable<T> { }
public sealed class ConditionallyImmutableImplementerWithUnappliedCondition<[ConditionallyImmutable.OnlyIf] T, [ConditionallyImmutable.OnlyIf] /* UnappliedConditionalImmutability(U, interface, ConsistencyTests.ISomethingConditionallyImmutable) */ U /**/> : ISomethingConditionallyImmutable<T> { }

[ConditionallyImmutable]
public sealed class /* MissingTransitiveImmutableAttribute(ConsistencyTests.ConditionallyImmutableImplementingImmutable, , interface, ConsistencyTests.ISomethingImmutable) */ ConditionallyImmutableImplementingImmutable /**/<[ConditionallyImmutable.OnlyIf] T> : ISomethingImmutable { }
Expand Down
Loading