Skip to content

Handle canonical types in constraints checks#129278

Open
MichalStrehovsky wants to merge 3 commits into
dotnet:mainfrom
MichalStrehovsky:fix126604
Open

Handle canonical types in constraints checks#129278
MichalStrehovsky wants to merge 3 commits into
dotnet:mainfrom
MichalStrehovsky:fix126604

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Fixes #126604

First commit is a revert of the revert.

The second commit is an update to the casting logic so that we can have both casting logic that treats __Canon as a class __Canon : object and nothing else (this is compatible with how casting in CoreCLR VM works), but also another casting logic that treats __Canon as "some reference type, could be something that does cast". Then update constraints check to use this flavor of the casting logic.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the NativeAOT/CoreCLR shared TypeSystem constraint-checking and casting helpers to correctly account for canonical definition types (__Canon, __UniversalCanon) when validating generic constraints, and wires that behavior into MakeGenericType dataflow so reflected generic instantiations can be preserved correctly under AOT/trimming.

Changes:

  • Introduces canonical-aware casting (CanCastToWithCanon) via a handler-based casting helper split (canon vs non-canon builds).
  • Updates TypeSystemConstraintsHelpers constraint validation to use canonical-aware casting and adds canon-specific handling for special/type constraints.
  • Adds coverage: new canonical constraint validation unit tests, and a NativeAOT trimming smoke test; also normalizes instantiations before constraints checking in MakeGenericType dataflow.

Reviewed changes

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

Show a summary per file
File Description
src/tests/nativeaot/SmokeTests/TrimmingBehaviors/Dataflow.cs Adds a new constrained MakeGenericType + Activator.CreateInstance smoke test.
src/coreclr/tools/ILVerification/ILVerification.projitems Adds NonCanon helper source inclusions for ILVerification build.
src/coreclr/tools/Common/TypeSystem/Common/TypeSystemConstraintsHelpers.NonCanon.cs Provides non-canon stubs for new partial constraint-helper hooks.
src/coreclr/tools/Common/TypeSystem/Common/TypeSystemConstraintsHelpers.cs Switches constraint validation to canonical-aware casting and adds canon-specific fast paths.
src/coreclr/tools/Common/TypeSystem/Common/CastingHelper.NonCanon.cs Intended NonCanon implementation of the canonical casting handler.
src/coreclr/tools/Common/TypeSystem/Common/CastingHelper.cs Refactors casting logic to support handler-based canon/non-canon behavior; adds CanCastToWithCanon.
src/coreclr/tools/Common/TypeSystem/Canon/TypeSystemConstraintsHelpers.Canon.cs Implements canonical-specific constraint logic for __Canon/__UniversalCanon.
src/coreclr/tools/Common/TypeSystem/Canon/CastingHelper.Canon.cs Implements canonical matching semantics for casting and generic argument matching.
src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj Includes new canon helper files in the ILCompiler.TypeSystem build.
src/coreclr/tools/aot/ILCompiler.TypeSystem.Tests/CoreTestAssembly/GenericConstraints.cs Adds new types used to test invariant interface constraints with canonical args.
src/coreclr/tools/aot/ILCompiler.TypeSystem.Tests/ConstraintsValidationTest.cs Adds a new test suite validating canonical type behavior in constraint checking.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/HandleCallAction.cs Normalizes instantiated types before checking constraints in MakeGenericType dependency computation.
src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj Includes the canon casting handler to satisfy CanCastToWithCanon usage.

Comment thread src/tests/nativeaot/SmokeTests/TrimmingBehaviors/Dataflow.cs
Comment thread src/coreclr/tools/Common/TypeSystem/Common/CastingHelper.cs
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NativeAOT MakeGenericType fails to activate reflected generic type when generic parameter has self-referential interface constraint

2 participants