Skip to content

Referencing type from ILLink.Descriptors doesn't preserve its static abstracts #128120

Description

@MichalStrehovsky

NOTE: when fixed, undo the workaround from #128020

The test now passes (it doesn't with Mono, but that's a Mono problem with marshalling DateTime in general).

However, I'm not convinced this is the right fix, I'll need to study what exactly is the reflection-like mechanism that #126911 introduced here.

So the problem reproes with e.g. this (even in .NET 10):

typeof(IX).GetMethod(nameof(IX.Call)).MakeGenericMethod(A.GetA()).Invoke(null, []);

interface IX
{
    static abstract void Method();

    public static void Call<T>() where T : IX => T.Method();
}

class A : IX
{
    public static Type GetA() => MethodBase.GetCurrentMethod().DeclaringType;

    public static void Method() => Console.WriteLine("Hello");
}

Trimming will strip the static abstracts implementation from A because it will assume nobody can get at A. However I get at it with trim unsafe code. Since this is only broken with trim unsafe code, so far so good.

However, the specific case in interop does mention the equivalent of A in ILLink.Descriptors.xml (it instructs to keep some unrelated methods on the DateMarshaler type). So if I were to add A.GetA to ILLink.Descriptors.xml, can I have a reasonable assumption that the MethodBase.GetCurrentMethod() call is now safe to suppress (it is reflection visible after all)? If so, the bug here is in ILLink, because the lines:

    <type fullname="System.StubHelpers.DateMarshaler">
      <method name="ConvertToNative" />
      <method name="ConvertToManaged" />
    </type>

didn't make it so that a DateMarshaler Type can be passed to MakeGenericMethod with something that is constrained with static abstracts that are called in the program.

Originally posted by @MichalStrehovsky in #128020 (comment)

Activity

  1. added this to the 11.0.0 milestone on May 13, 2026
  2. dotnet-policy-service commented on May 13, 2026

    @dotnet-policy-service
    Contributor

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

  3. self-assigned this
    on May 15, 2026
  4. locked and limited conversation to collaborators on Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzers

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions