Skip to content

ILC: can't resolve methods in generic types with generic type arguments as a parameter type in xml descriptor #128121

@rolfbjarne

Description

@rolfbjarne

Description

If an xml linker description tries to root a method from a generic type, and that method uses some of the generic type arguments in its signature, then ILC shows a warning during the build.

Reproduction Steps

C# code:

var obj = new MyGenericClass<string> ();
obj.GenericMethod ("hello");

public class MyGenericClass<T> {
	public void GenericMethod (T arg) {
	}
}

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <TrimmerRootDescriptor Include="Roots.xml" />
  </ItemGroup>
</Project>

Roots.xml:

<linker>
  <assembly fullname="xml-generic-method">
    <type fullname="MyGenericClass`1">
      <!-- This signature uses the generic parameter name 'T', which ILC can't resolve -->
      <method signature="System.Void GenericMethod(T)" />
    </type>
  </assembly>
</linker>

Expected behavior

No warning, like building with ILLink does:

$ dotnet publish /p:PublishTrimmed=true
Restore complete (0.3s)
  xml-generic-method net10.0 osx-arm64 succeeded (4.2s) → bin/Release/net10.0/osx-arm64/publish/

Build succeeded in 4.7s

Actual behavior

Building with NativeAOT yields a warning:

$ dotnet publish /p:PublishAot=true
Restore complete (0.4s)
  xml-generic-method net10.0 osx-arm64 succeeded with 1 warning(s) (1.5s) → bin/Release/net10.0/osx-arm64/publish/
    /Users/rolf/test/dotnet/xml-generic-method/Roots.xml(5,8): warning IL2009: Could not find method 'System.Void GenericMethod(T)' on type 'MyGenericClass`1'.

Build succeeded with 1 warning(s) in 2.0s

Regression?

I don't think so.

Known Workarounds

Using the method name instead of the signature in the xml works:

<linker>
  <assembly fullname="xml-generic-method">
    <type fullname="MyGenericClass`1">
      <method name="GenericMethod" />
    </type>
  </assembly>
</linker>

but is less surgical and may root unrelated methods as well.

Configuration

$ dotnet --info
$ dotnet --info
.NET SDK:
 Version:           10.0.203
 Commit:            c23858a6d8
 Workload version:  10.0.200-manifests.333bc819
 MSBuild version:   18.3.3+c23858a6d

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  26.4
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/10.0.203/

.NET workloads installed:
 [macos]
   Installation Source: SDK 10.0.200
   Manifest Version:    26.4.10256/10.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/10.0.100/microsoft.net.sdk.macos/26.4.10256/WorkloadManifest.json
   Install Type:        FileBased

 [ios]
   Installation Source: SDK 10.0.200
   Manifest Version:    26.4.10256/10.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/10.0.100/microsoft.net.sdk.ios/26.4.10256/WorkloadManifest.json
   Install Type:        FileBased

 [maui]
   Installation Source: SDK 10.0.200
   Manifest Version:    10.0.20/10.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/10.0.100/microsoft.net.sdk.maui/10.0.20/WorkloadManifest.json
   Install Type:        FileBased

Configured to use loose manifests when installing new manifests.

Host:
  Version:      10.0.7
  Architecture: arm64
  Commit:       b16286c228

.NET SDKs installed:
  8.0.416 [/usr/local/share/dotnet/sdk]
  9.0.308 [/usr/local/share/dotnet/sdk]
  9.0.311 [/usr/local/share/dotnet/sdk]
  9.0.312 [/usr/local/share/dotnet/sdk]
  9.0.313 [/usr/local/share/dotnet/sdk]
  10.0.100 [/usr/local/share/dotnet/sdk]
  10.0.101 [/usr/local/share/dotnet/sdk]
  10.0.103 [/usr/local/share/dotnet/sdk]
  10.0.201 [/usr/local/share/dotnet/sdk]
  10.0.203 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.22 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.14 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.15 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.22 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.15 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  x64   [/usr/local/share/dotnet/x64]
    registered at [/etc/dotnet/install_location_x64]

Environment variables:
  DOTNET_CLI_TELEMETRY_OPTOUT              [1]

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions