Skip to content

Crossgen: (bug) static virtual resolved to an inherited generic base method gets the derived type as its generic context #137845

Open

Description

@EgorBo

crossgen2 can resolve a constrained call T.M() to a static method inherited from a generic base class (Base<string>.M). In that case it passes the constrained type's dictionary (Derived<int>) as the instantiation argument instead of the owning type's dictionary (Base<string>). As a result, R2R code reads the wrong type argument.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

interface I { static abstract string M(); }

class Base<T> : I
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static string M() => typeof(T).Name;
}

class Derived<U> : Base<string> { }

class Program
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static string Helper<T>() where T : I => T.M();

    static void Main() => Console.WriteLine(Helper<Derived<int>>());
}

Expected

String

Actual

Int32

If Derived is non-generic (class Derived : Base<string>), a Checked runtime asserts instead: numGenericArgs > 0 in ProcessDynamicDictionaryLookup (prestub.cpp).

Regression?

Reproduces with crossgen2 10.0.14, 11.0.0-rc.3, and main. It is not a recent regression.

Notes

For a non-DIM resolution, ceeInfoGetCallInfo (CorInfoImpl.ReadyToRun.cs) sets exactType = constrainedType even when the resolved method is declared on a base type. The inst param lookup then becomes TypeDictionary(Derived<int>). The VM uses GetExactDeclaringType, and ILC uses directMethod.OwningType. crossgen2 should also use the exact owning type, Base<string>.

Activity

  1. added this to the 12.0.0 milestone on Nov 18, 2026
  2. added
    area-crossgen2-coreclr
    on Nov 18, 2026
  3. added and removed
    area-crossgen2-coreclr
    on Nov 19, 2026
  4. dotnet-policy-service commented on Nov 19, 2026

    @dotnet-policy-service
    Contributor

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions