Skip to content

Fix NativeAOT dispatch to covariant return GVM overrides - #137707

Merged
MichalStrehovsky merged 5 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-gvm-covariant-return
Nov 24, 2026
Merged

MichalStrehovsky merged 5 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/fix-gvm-covariant-return

Conversation

@Sergio0694

Copy link
Copy Markdown
Contributor

In NativeAOT, a C# covariant-return override of a generic virtual method is not called when the override is only reachable through the base slot. The call goes to the base implementation, or hits a FailFast ("Failed to create generic virtual method implementation"). CoreCLR behaves correctly.

class Base { public virtual object M<T>() => "Base"; }
class Derived : Base { public override string M<T>() => "Derived"; }

Base b = new Derived();
Console.WriteLine(b.M<int>()); // NativeAOT prints "Base"

Roslyn emits covariant overrides as newslot with a .override MethodImpl, so the override is its own slot-defining method. EETypeNode.AnalyzeVirtualMethods only marks a type as interesting for dynamic GVM dependency analysis when one of its GVMs has a different slot-defining method, so these types never reach GVMDependenciesNode.SearchDynamicDependencies and the override bodies are never compiled. The fix also marks types whose non-interface MethodImpls have a generic body.

Added a NativeAOT smoke test: it fails without the fix (15 scenarios: class, interface, generic types, shared code, awaited calls) and passes with it. Also added TypeSystem tests for resolving generic covariant overrides.

Note

This PR description was generated with GitHub Copilot.

A C# covariant return override of a generic virtual method is emitted as a
newslot virtual method with a MethodImpl pointing at the base slot. Such a
method is its own slot defining method, so EETypeNode.AnalyzeVirtualMethods
did not mark the type as interesting for dynamic dependency analysis. As a
result GVMDependenciesNode never resolved the base GVM on that type and the
override body (or its async variant / return dropping thunk) was never
compiled. At runtime the call either went to the base implementation or
failed fast with "Failed to create generic virtual method implementation".

Also consider non-interface MethodImpls with a generic body when computing
InterestingForDynamicDependencies.

Co-authored-by: Copilot App <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 17, 2026
@azure-pipelines

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

@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.

Sergio0694 and others added 3 commits November 19, 2026 15:16
Abstract bodies have no code to compile, so they don't make a type interesting for dynamic GVM analysis.

Co-authored-by: Copilot App <[email protected]>
After the MethodImpl check sets InterestingForDynamicDependencies, all flags are set and the interface scan can't add anything.

Co-authored-by: Copilot App <[email protected]>

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.

🟢 Approval recommended

No active review issues are reported for the NativeAOT generic covariant dispatch changes.

No open issues 🎉

What changed in this PR

This PR fixes NativeAOT dispatch to covariant-return generic virtual method overrides that are reachable only through a base slot. It extends dependency discovery so those override implementations are compiled.

Changes:

  • Recognizes concrete generic MethodImpl bodies when selecting types for dynamic dependency analysis.
  • Adds NativeAOT smoke coverage for class and interface dispatch, generic types, shared code, and awaited calls.
  • Adds type-system tests for resolving generic covariant overrides across inheritance chains.
File Description
src/​tests/​nativeaot/​SmokeTests/​UnitTests/​Generics.cs Adds and registers covariant generic virtual dispatch regression scenarios.
src/​coreclr/​tools/​aot/​ILCompiler.TypeSystem.Tests/​VirtualFunctionOverrideTests.cs Tests override resolution for generic definitions and instantiated methods.
src/​coreclr/​tools/​aot/​ILCompiler.TypeSystem.Tests/​CoreTestAssembly/​VirtualFunctionOverride.cs Adds generic covariant methods to existing inheritance fixtures.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​DependencyAnalysis/​EETypeNode.cs Includes MethodImpl-based generic overrides in dynamic dependency discovery.

Review effort: Balanced

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

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants