Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 11, 2025

The AotTypeResolver source generator was incorrectly treating non-generic types like System.Type as generic type definitions, causing compilation errors when generating AOT-compatible code.

Problem

After updating from version 0.25.21 to 0.53.0, users encountered compilation errors like:

The non-generic type 'Type' cannot be used with type arguments.

This was caused by the generated AotTypeResolver.g.cs file containing invalid code:

if (genericTypeDefinition == typeof(global::System.Type) && typeArguments.Length == 1)
{
    if (typeArguments[0] == typeof(global::System.Type))
    {
        var result = typeof(global::System.Type<global::System.Type>); // ❌ Invalid!
        _genericTypeCache[cacheKey] = result;
        return result;
    }
}

Since System.Type is not a generic type, the syntax System.Type<System.Type> is invalid C# and causes compilation to fail.

Root Cause

The issue was in the ExtractGenericTypeCombinations method, which was creating GenericTypeCombination objects for any type that appeared in MakeGenericType calls, without validating whether the type was actually a generic type definition.

When code like typeof(List<>).MakeGenericType(typeof(Type)) was analyzed, the generator incorrectly treated System.Type (which was passed as a type argument) as a generic type definition instead of recognizing that List<> was the actual generic type definition.

Solution

  1. Added IsGenericTypeDefinition helper method that properly validates whether a type is actually a generic type definition using IsGenericType and IsUnboundGenericType checks.

  2. Enhanced ExtractGenericTypeCombinations to filter out non-generic types before creating combinations:

    // Only create combinations for actual generic type definitions
    if (IsGenericTypeDefinition(genericDef))
    {
        combinations.Add(new GenericTypeCombination
        {
            GenericDefinition = genericDef,
            TypeArguments = typeArgs
        });
    }
  3. Added defensive validation in GenerateGenericTypeCombination to prevent generation of invalid generic type constructions as an additional safety measure.

Verification

  • ✅ AOT tests now compile successfully without the original compilation error
  • ✅ Generated AotTypeResolver.g.cs files no longer contain invalid generic type constructions
  • ✅ Added comprehensive test cases to prevent regression
  • ✅ No breaking changes to public APIs

The fix is minimal and surgical, specifically targeting the reported issue without affecting other source generation functionality.

Fixes #2865.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] AotTypeResolver is generated with invalid code for MakeGenericType Fix AotTypeResolver generation for non-generic types like System.Type Aug 11, 2025
@Copilot Copilot AI requested a review from thomhurst August 11, 2025 15:19
Copilot finished work on behalf of thomhurst August 11, 2025 15:19
@thomhurst thomhurst marked this pull request as ready for review August 11, 2025 15:22
@Copilot Copilot AI temporarily deployed to Pull Requests August 11, 2025 15:22 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 11, 2025 15:22 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 11, 2025 15:22 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 11, 2025 15:22 Inactive
@Copilot Copilot AI temporarily deployed to Pull Requests August 11, 2025 15:22 Inactive
@thomhurst thomhurst enabled auto-merge (squash) August 11, 2025 15:29
@thomhurst thomhurst merged commit 9f0d4d0 into main Aug 11, 2025
12 of 16 checks passed
@thomhurst thomhurst deleted the copilot/fix-2865 branch August 11, 2025 15:48
This was referenced Aug 20, 2025
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.

AotTypeResolver is generated with invalid code for MakeGenericType
2 participants