Skip to content

Fix Crossgen2 variance validation for type constraints - #132809

Open
jtschuster wants to merge 1 commit into
dotnet:mainfrom
jtschuster:fix-crossgen2-variance
Open

Fix Crossgen2 variance validation for type constraints#132809
jtschuster wants to merge 1 commit into
dotnet:mainfrom
jtschuster:fix-crossgen2-variance

Conversation

@jtschuster

@jtschuster jtschuster commented Aug 27, 2026

Copy link
Copy Markdown
Member

Summary

Crossgen2 applied method generic-constraint variance rules to a generic type's own constraints. ECMA-335 II.9.7 explicitly leaves type-owned constraints unrestricted, so valid variant interfaces could fail automatic validation and omit READYTORUN_FLAG_SkipTypeValidation.

This change:

  • removes the invalid variance check for type-owned generic constraints
  • aligns interface method signature validation with the VM by checking all instance methods and virtual methods, including static virtual and static abstract declarations
  • adds a ReadyToRun regression test based on the reported constraint pattern

Fixes #132724

Note

This pull request was created with GitHub Copilot.

@@ -310,7 +309,7 @@ Task<bool> ValidateTypeWorkerHelper(TypeDesc typeToCheckForSkipValidation)


// Validate that generic variance is properly respected in method signatures
if (type.IsInterface && method.IsVirtual && !method.Signature.IsStatic && type.HasInstantiation)
if (type.IsInterface && type.HasInstantiation && (method.IsVirtual || !method.Signature.IsStatic))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should validate static virtuals and static abstract methods according the runtime's ECMA augments.

Do not apply method constraint variance rules to a generic type's own constraints. Validate static virtual interface method signatures and add coverage for emitting SkipTypeValidation for valid type constraints.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 87038ccf-f45b-40d3-a2d4-ee7d3d7f7a7f
@jtschuster
jtschuster force-pushed the fix-crossgen2-variance branch from ae7c278 to d2bf9a8 Compare August 27, 2026 01:14
@jtschuster
jtschuster marked this pull request as ready for review August 27, 2026 01:14
Copilot AI lite review requested due to automatic review settings August 27, 2026 01:14
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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 Crossgen2’s ReadyToRun type validation logic to better match CoreCLR’s variance validation behavior and to avoid incorrectly rejecting valid generic type-parameter constraints, and adds a regression test to prevent future regressions.

Changes:

  • Removes variance validation for a generic type’s own parameter constraints (leaving constraint usage unrestricted as intended).
  • Aligns interface method signature variance validation with the VM by validating all instance methods and all virtual methods (including static virtual/abstract).
  • Adds a ReadyToRun regression test covering the reported “variant type parameter used in a type-owned constraint” pattern.

Reviewed changes

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

File Description
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/TypeValidationChecker.cs Fixes variance validation rules to match VM behavior and avoids invalid checks on type-owned constraints.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/TypeValidation/GenericTypeConstraints.cs Adds a minimal source test case that exercises the constraint + variance pattern.
src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs Adds a test that runs Crossgen2 with automatic type validation and asserts the resulting image sets READYTORUN_FLAG_SkipTypeValidation.

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Crossgen2 10.0.2+ reports false generic variance error and clears SkipTypeValidation

4 participants