Skip to content

Conversation

@Sergio0694
Copy link
Member

Title.

Example of codegen:

image

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 enhances the cswinrt generator so that it can recognize and expand covariant constructed generic Windows Runtime interface types (e.g., IEnumerable<T> hierarchies) when discovering and emitting interop types.

Changes:

  • Introduced WindowsRuntimeTypeAnalyzer to encapsulate logic for finding the most derived Windows Runtime interface implemented by a type and for computing covariant interface combinations (e.g., IEnumerable<IEnumerable<object>>).
  • Extended TypeSignatureExtensions and TypeDefinitionExtensions with base-type enumeration helpers and wired those into InteropTypeDiscovery.TryTrackExposedUserDefinedType to track both direct and covariant Windows Runtime interfaces for exposed user-defined types.
  • Updated proxy generation to use the new analyzer instead of the removed UserDefinedTypeAnalyzer, ensuring runtime class names are based on the most derived Windows Runtime interface.

Reviewed changes

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

Show a summary per file
File Description
src/WinRT.Interop.Generator/Helpers/WindowsRuntimeTypeAnalyzer.cs New helper that finds the most derived Windows Runtime interface and enumerates covariant generic interface combinations (e.g., for IEnumerable<T>, IEnumerator<T>, IReadOnlyList<T>).
src/WinRT.Interop.Generator/Helpers/UserDefinedTypeAnalyzer.cs Removed now-redundant helper; its functionality (most-derived Windows Runtime interface lookup) is moved into WindowsRuntimeTypeAnalyzer.
src/WinRT.Interop.Generator/Extensions/TypeSignatureExtensions.cs Makes EnumerateAllInterfaces’s working variable nullable and adds EnumerateBaseTypes() for TypeSignature, used by covariant expansion.
src/WinRT.Interop.Generator/Extensions/TypeDefinitionExtensions.cs Adds EnumerateBaseTypes() for TypeDefinition and aligns comments in EnumerateBaseTypesAndSelf() with the new helper.
src/WinRT.Interop.Generator/Discovery/InteropTypeDiscovery.cs In TryTrackExposedUserDefinedType, uses WindowsRuntimeTypeAnalyzer.EnumerateCovariantInterfaceTypes(...) plus the original interface to track all relevant projected Windows Runtime interfaces and their constructed generic instances.
src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.UserDefinedType.cs Changes proxy generation to use WindowsRuntimeTypeAnalyzer.TryGetMostDerivedWindowsRuntimeInterfaceType when selecting the interface that drives the runtime class name.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Renamed the UserDefinedTypeAnalyzer class and its file to WindowsRuntimeTypeAnalyzer for improved clarity and accuracy. Updated all references to use the new class name.
Introduces a new EnumerateBaseTypes method to enumerate all base types of a given type. This provides a way to traverse the type hierarchy, complementing the existing method that includes the type itself.
Introduces EnumerateBaseTypes to enumerate all base types of a given type signature. This complements the existing interface enumeration and improves type hierarchy traversal capabilities.
Enhanced interface discovery to enumerate covariant combinations for Windows Runtime interfaces, ensuring vtable entries match .NET expectations. Introduced EnumerateCovariantInterfaceTypes in WindowsRuntimeTypeAnalyzer and updated InteropTypeDiscovery to use it. Also refactored TryGetMostDerivedWindowsRuntimeInterfaceType to accept a generic type parameter.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/covariant-expansion branch from 24d04bb to 48426d4 Compare January 24, 2026 07:06
Introduces IsNotExclusiveToWindowsRuntimeType to determine if a TypeSignature represents a Windows Runtime type that is not marked as '[exclusiveto]'. This is inferred by checking if the type is an interface and whether it is public, as '[exclusiveto]' interfaces are not public.
Updated InteropTypeDiscovery to skip [exclusiveto] Windows Runtime interfaces during covariant interface expansion, preventing unnecessary binary size increase and avoiding interfaces that cannot be resolved. Also clarified documentation in WindowsRuntimeTypeAnalyzer regarding validation of returned generic instantiations.
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.

2 participants