Skip to content

Trimmer removes parameterless constructor of a type used only as a new()-constrained generic attribute argument, causing TypeLoadException at GetCustomAttributes (net11 regression) #128756

Closed

Description

@simonrozsival

Description

The .NET 11 trimmer (Microsoft.NET.ILLink.Tasks) removes the public parameterless constructor of a type that is used only as the generic argument of a new()-constrained generic custom attribute, when that attribute is reached only via reflection (Type.GetCustomAttributes).

The type's shell is kept (its name is referenced from attribute metadata), but its method table is emptied. When the runtime later materializes the generic attribute and validates the new() constraint, the constraint is unsatisfiable and a TypeLoadException is thrown from inside GetCustomAttributes.

This is a regression in .NET 11: the exact same source and trim settings work correctly on .NET 10.

No IL trim warning is emitted — this is a silent hole.

Minimal reproduction

Program.cs:

using System;
using Repro;

typeof(CheckBox).GetCustomAttributes(false);
Console.WriteLine("RESULT: OK");

namespace Repro
{
    public class Handler { }
    class MyAttribute<T> : Attribute where T : new() { }
    [My<Handler>] class CheckBox { }
}

App.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>full</TrimMode>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>osx-arm64</RuntimeIdentifier>
  </PropertyGroup>
</Project>

Publish and run:

dotnet publish -c Release -r osx-arm64 -o ./bin/pub
./bin/pub/App

Expected behavior

RESULT: OK (as it does untrimmed, and as it does when trimmed on .NET 10).

Actual behavior

Unhandled exception. System.TypeLoadException: GenericArguments[0], 'Repro.Handler', on 'Repro.MyAttribute`1[T]' violates the constraint of type parameter 'T'.
   at System.ModuleHandle.ResolveType(...)
   at System.ModuleHandle.ResolveTypeHandle(...)
   at System.Reflection.RuntimeModule.ResolveType(...)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(...)
   at System.Reflection.CustomAttribute.AddCustomAttributes(...)
   at System.Reflection.CustomAttribute.GetCustomAttributes(...)
   at System.RuntimeType.GetCustomAttributes(Boolean inherit)
   at Program.<Main>$(String[] args) in Program.cs:line 4

The constraint violation is bogus: Repro.Handler does have a public parameterless constructor in source. The trimmer removed it, leaving the new() constraint unsatisfiable at runtime.

Confirmed mechanism

Inspecting the trimmed App.dll shows Repro.Handler's parameterless constructor has been removed (the type is kept as an empty shell). Two independent mitigations confirm this is the cause:

  • Removing the where T : new() constraint from MyAttribute<T> → trimmed app passes.
  • Adding [DynamicDependency(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(Handler))] from reachable code → ctor preserved, trimmed app passes.

Notably, putting [DynamicallyAccessedMembers(PublicParameterlessConstructor)] on the attribute's T type parameter does not preserve the constructor.

Regression range

Same source, same TrimMode=full:

Target Microsoft.NET.ILLink.Tasks Result
net11.0 (untrimmed) — ✅ RESULT: OK
net10.0 trimmed 10.0.5 ✅ RESULT: OK
net11.0 trimmed 11.0.0-preview.4.26230.115 ❌ TypeLoadException

Configuration

  • SDK: 11.0.100-preview.4.26230.115
  • RID: osx-arm64 (macOS, Apple Silicon)
  • PublishTrimmed=true, TrimMode=full, SelfContained=true

Other information

This pattern (a new()-constrained generic attribute resolved via reflection) is used by .NET MAUI's ElementHandlerAttribute<THandler>, where it caused CheckBox handler resolution to crash at startup in net11 Release builds — see dotnet/maui#35665. MAUI has a workaround, but the underlying trimmer behavior looks like a bug.

Activity

  1. added
    area-Tools-ILLink
    on May 29, 2026
  2. added
    untriaged
    on May 29, 2026
  3. dotnet-policy-service commented on May 29, 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.

  4. simonrozsival commented on May 29, 2026

    @simonrozsival
    MemberAuthor

    Bisected: regression introduced mid-P4 (works on P1/P2/P3 and early P4)

    Pinning Microsoft.NET.ILLink.Tasks to specific builds (same SDK, same minimal repro, CoreCLR TrimMode=full):

    ILLink.Tasks version Date stamp Result
    11.0.0-preview.1.26104.118 — ✅ PASS
    11.0.0-preview.2.26159.112 — ✅ PASS
    11.0.0-preview.3.26118.109 — ✅ PASS
    11.0.0-preview.3.26219.104 (last P3) — ✅ PASS
    11.0.0-preview.4.26181.116 (first P4) — ✅ PASS
    11.0.0-preview.4.26207.108 — ✅ PASS
    11.0.0-preview.4.26215.101 2026-04-15 ✅ PASS
    11.0.0-preview.4.26215.121 2026-04-15 ✅ last PASS
    11.0.0-preview.4.26222.142 2026-04-22 ❌ first FAIL
    11.0.0-preview.4.26224.122 2026-04-24 ❌ FAIL
    11.0.0-preview.4.26230.115 2026-04-30 ❌ FAIL

    (Date stamps decoded via Arcade's (year-2000)*1000 + month*50 + day.)

    So the regression was introduced by a src/tools/illink change merged between 2026-04-15 and 2026-04-22. VMR boundary commits (dotnet/dotnet): good ab01524bbb2ef1eea0ffaef161b3ef5686e8f256 → bad aae32bdb43e214583a15e3393e38c889b9447ec4.

    Candidate illink PRs merged in that window (for the linker team to pinpoint):

    It works on .NET 11 Preview 3 and the first ~5 weeks of Preview 4; it broke only in the 2026-04-22 ILLink build.

  5. jtschuster commented on May 29, 2026

    @jtschuster
    Member

    Thank you for the bisect info, that's super helpful!

    A couple of the candidate PRs look benign:

    #126990 Implement TrimmingDescriptorNode for ILTrim XML descriptor support (04-22)

    ILTrim is an experimental trimmer that isn't the trimmer we ship.

    #123040 Fix trimming analyzer missing diagnostics for method calls in field access expressions (04-21)

    This only touches the analyzer and shouldn't affect trimming.

    #126380 > [ILLink/ILCompiler] Fix crash when array types are used as TypeMap trim targets (04-20)

    Since the repro doesn't use the TypeMap I imagine this is unrelated.

    The rest could potentially cause issues

    #127090 Fix UnreachableBody with only ref to assembly (04-22)

    The reordering of steps could be messing with something, but it wouldn't be my first guess.

    #126519 ILLink: Don't preserve forwarder assemblies referenced by link-action root assemblies (04-16)

    This one moves around type marking order. This would be my first guess to check.

  6. removed
    untriaged
    on May 29, 2026
  7. added this to the 11.0.0 milestone on Jun 18, 2026
  8. locked and limited conversation to collaborators on Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Tools-ILLink

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions