Skip to content

Conversation

@nytian
Copy link
Contributor

@nytian nytian commented Nov 17, 2025

Fix #453

Copilot AI review requested due to automatic review settings November 17, 2025 04:20
Copilot finished reviewing on behalf of nytian November 17, 2025 04:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes issue #453 by improving the Activity analyzer to correctly handle FunctionContext parameters and support polymorphic type conversions.

  • Adds special handling to skip validation for activities that use FunctionContext with [ActivityTrigger], preventing false positive warnings
  • Replaces exact type matching with type compatibility checking using Roslyn's ClassifyConversion API and custom collection compatibility logic
  • Adds comprehensive test coverage for FunctionContext handling, polymorphism, collection type compatibility, and incompatible type detection

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Analyzers/Activities/MatchingInputOutputTypeActivityAnalyzer.cs Adds FunctionContext detection to skip DI parameter validation; implements type compatibility checking with support for polymorphism, inheritance, and collection interface implementations (List to IReadOnlyList)
test/Analyzers.Tests/Activities/MatchingInputOutputTypeActivityAnalyzerTests.cs Adds 6 new test cases covering FunctionContext scenarios, polymorphic input/output types, collection type compatibility, and incompatible type detection

Comment on lines 460 to 466
foreach (INamedTypeSymbol @interface in sourceType.AllInterfaces)
{
if (SymbolEqualityComparer.Default.Equals(@interface.OriginalDefinition, targetInterface.OriginalDefinition))
{
return true;
}
}
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This foreach loop implicitly filters its target sequence - consider filtering the sequence explicitly using '.Where(...)'.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@bachuv bachuv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR!

Can you add more details in the PR description?

Copy link
Collaborator

@andystaples andystaples left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question

}

// One is null, the other isn't = not compatible
if (sourceType == null || targetType == null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking - does this logic support passing null as input to a nullable function parameter? That was also an issue with this analyzer previously

Copilot AI review requested due to automatic review settings November 20, 2025 23:14
Copilot finished reviewing on behalf of nytian November 20, 2025 23:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment on lines +462 to +468
// Array implements: IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
string targetName = targetInterface.OriginalDefinition.ToDisplayString();
return targetName == "System.Collections.Generic.IEnumerable<T>" ||
targetName == "System.Collections.Generic.ICollection<T>" ||
targetName == "System.Collections.Generic.IList<T>" ||
targetName == "System.Collections.Generic.IReadOnlyCollection<T>" ||
targetName == "System.Collections.Generic.IReadOnlyList<T>";
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The array interface compatibility check uses string comparison of type names, which may not be robust across different compilation contexts or type forwarding scenarios. Consider using GetTypeByMetadataName to get reference symbols for the standard collection interfaces and comparing using SymbolEqualityComparer.Default.Equals instead of string comparison. This would be more reliable and less prone to edge cases with type representation differences.

Copilot uses AI. Check for mistakes.
Comment on lines 365 to 374
if (sourceType == null && targetType != null)
{
// Check if target is a nullable reference type (NullableAnnotation.Annotated)
// or a nullable value type (Nullable<T>)
if (targetType.NullableAnnotation == NullableAnnotation.Annotated ||
targetType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
return true;
}
}
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null handling logic may incorrectly reject null values passed to reference type parameters when nullable reference types are disabled. When sourceType is null and targetType is a reference type (like string), the code checks for NullableAnnotation.Annotated (line 369), which will be false when nullable reference types are disabled. However, in such projects, all reference types can accept null. Consider adding || targetType.IsReferenceType to line 369 to allow null for all reference types, or checking if nullable context is enabled before applying strict nullable validation.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out this is not right

Copilot AI review requested due to automatic review settings November 21, 2025 19:58
@nytian nytian force-pushed the nytian/analyzer-bug branch from c47ebea to ec21a2a Compare November 21, 2025 19:58
Copilot finished reviewing on behalf of nytian November 21, 2025 20:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

Comments suppressed due to low confidence (1)

src/Analyzers/Activities/MatchingInputOutputTypeActivityAnalyzer.cs:374

  • These 'if' statements can be combined.
            if (sourceType == null && targetType != null)
            {
                // Check if target is a nullable reference type (NullableAnnotation.Annotated)
                // or a nullable value type (Nullable<T>)
                if (targetType.NullableAnnotation == NullableAnnotation.Annotated ||
                    targetType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
                {
                    return true;
                }
            }

Copilot AI review requested due to automatic review settings November 21, 2025 23:02
Copilot finished reviewing on behalf of nytian November 21, 2025 23:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@nytian nytian requested review from andystaples and bachuv November 24, 2025 23:39
Copilot AI review requested due to automatic review settings November 26, 2025 04:07
@nytian
Copy link
Contributor Author

nytian commented Nov 26, 2025

E2E test
-- with latest v1.11.1
image
--with fix
image

Copilot finished reviewing on behalf of nytian November 26, 2025 04:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment on lines +344 to +484
/// <summary>
/// Checks if the source type is compatible with (can be assigned to) the target type.
/// This handles polymorphism, interface implementation, inheritance, and collection type compatibility.
/// </summary>
static bool AreTypesCompatible(Compilation compilation, ITypeSymbol? sourceType, ITypeSymbol? targetType)
{
// Both null = compatible (no input/output on both sides)
if (sourceType == null && targetType == null)
{
return true;
}

// Special case: null (no input/output provided) can be passed to explicitly nullable parameters
// This handles nullable value types (int?) and nullable reference types (string?)
if (sourceType == null && targetType != null)
{
// Check if target is a nullable value type (Nullable<T>)
if (targetType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
return true;
}

// Check if target is a nullable reference type (string?)
if (targetType.NullableAnnotation == NullableAnnotation.Annotated)
{
return true;
}

// Not nullable, so null input is incompatible
return false;
}

// If targetType is null but sourceType is not, they're incompatible
if (targetType == null && sourceType != null)
{
return false;
}

// Check if types are exactly equal
if (SymbolEqualityComparer.Default.Equals(sourceType, targetType))
{
return true;
}

// Check if source type can be converted to target type (handles inheritance, interface implementation, etc.)
// At this point, both sourceType and targetType are guaranteed to be non-null
Conversion conversion = compilation.ClassifyConversion(sourceType!, targetType!);
if (conversion.IsImplicit || conversion.IsIdentity)
{
return true;
}

// Special handling for collection types since ClassifyConversion doesn't always recognize
// generic interface implementations (e.g., List<T> to IReadOnlyList<T>)
// At this point, both sourceType and targetType are guaranteed to be non-null
if (IsCollectionTypeCompatible(sourceType!, targetType!))
{
return true;
}

return false;
}

/// <summary>
/// Checks if the source collection type is compatible with the target collection type.
/// Handles common scenarios like List to IReadOnlyList, arrays to IEnumerable, etc.
/// </summary>
static bool IsCollectionTypeCompatible(ITypeSymbol sourceType, ITypeSymbol targetType)
{
// Check if source is an array and target is a collection interface
if (sourceType is IArrayTypeSymbol sourceArray && targetType is INamedTypeSymbol targetNamed)
{
return IsArrayCompatibleWithCollectionInterface(sourceArray, targetNamed);
}

// Both must be generic named types
if (sourceType is not INamedTypeSymbol sourceNamed || targetType is not INamedTypeSymbol targetNamedType)
{
return false;
}

// Both must be generic types with the same type arguments
if (!sourceNamed.IsGenericType || !targetNamedType.IsGenericType)
{
return false;
}

if (sourceNamed.TypeArguments.Length != targetNamedType.TypeArguments.Length)
{
return false;
}

// Check if type arguments are compatible (could be different but compatible types)
for (int i = 0; i < sourceNamed.TypeArguments.Length; i++)
{
if (!SymbolEqualityComparer.Default.Equals(sourceNamed.TypeArguments[i], targetNamedType.TypeArguments[i]))
{
// Type arguments must match exactly for collections (we don't support covariance/contravariance here)
return false;
}
}

// Check if source type implements or derives from target type
// This handles: List<T> → IReadOnlyList<T>, List<T> → IEnumerable<T>, etc.
return ImplementsInterface(sourceNamed, targetNamedType);
}

/// <summary>
/// Checks if an array type is compatible with a collection interface.
/// </summary>
static bool IsArrayCompatibleWithCollectionInterface(IArrayTypeSymbol arrayType, INamedTypeSymbol targetInterface)
{
if (!targetInterface.IsGenericType || targetInterface.TypeArguments.Length != 1)
{
return false;
}

// Check if array element type matches the generic type argument
if (!SymbolEqualityComparer.Default.Equals(arrayType.ElementType, targetInterface.TypeArguments[0]))
{
return false;
}

// Array implements: IEnumerable<T>, ICollection<T>, IList<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
string targetName = targetInterface.OriginalDefinition.ToDisplayString();
return targetName == "System.Collections.Generic.IEnumerable<T>" ||
targetName == "System.Collections.Generic.ICollection<T>" ||
targetName == "System.Collections.Generic.IList<T>" ||
targetName == "System.Collections.Generic.IReadOnlyCollection<T>" ||
targetName == "System.Collections.Generic.IReadOnlyList<T>";
}

/// <summary>
/// Checks if the source type implements the target interface.
/// </summary>
static bool ImplementsInterface(INamedTypeSymbol sourceType, INamedTypeSymbol targetInterface)
{
// Check all interfaces implemented by the source type
return sourceType.AllInterfaces.Any(@interface =>
SymbolEqualityComparer.Default.Equals(@interface.OriginalDefinition, targetInterface.OriginalDefinition));
}
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new AreTypesCompatible, IsCollectionTypeCompatible, IsArrayCompatibleWithCollectionInterface, and ImplementsInterface methods lack test coverage. Consider adding unit tests to verify:

  1. Polymorphic type compatibility (e.g., passing a List<string> where IReadOnlyList<string> is expected)
  2. Array to collection interface compatibility (e.g., string[] to IEnumerable<string>)
  3. Interface implementation checks
  4. Null handling for nullable reference types and nullable value types
  5. Edge cases like generic variance scenarios

Copilot uses AI. Check for mistakes.
Comment on lines +167 to +171
// If the parameter is FunctionContext, skip validation for this activity (it's a DI parameter, not real input)
if (functionContextSymbol != null && SymbolEqualityComparer.Default.Equals(inputParam.Type, functionContextSymbol))
{
return;
}
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FunctionContext check (lines 167-171) lacks test coverage. Consider adding a test case that verifies an activity method with a FunctionContext parameter marked with [ActivityTrigger] doesn't generate diagnostics, confirming that DI parameters are properly excluded from input type validation.

Copilot uses AI. Check for mistakes.
Comment on lines +436 to +444
// Check if type arguments are compatible (could be different but compatible types)
for (int i = 0; i < sourceNamed.TypeArguments.Length; i++)
{
if (!SymbolEqualityComparer.Default.Equals(sourceNamed.TypeArguments[i], targetNamedType.TypeArguments[i]))
{
// Type arguments must match exactly for collections (we don't support covariance/contravariance here)
return false;
}
}
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 436 "Check if type arguments are compatible (could be different but compatible types)" is misleading, as the implementation (lines 437-444) requires exact type argument equality, not just compatibility. Consider updating the comment to: "Check if type arguments match exactly (we don't support covariance/contravariance for collections)" to better reflect the actual behavior.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzer Rule DURABLE2001 False Positives

4 participants