Skip to content

Erroneous linker error when using DAM attributes + primary constructors #101861

@captainsafia

Description

@captainsafia

Description

The linker does not respect DynamicallyAccessedMembers attributes on parameters within a primary constructor.

internal sealed class TypeBasedOpenApiDocumentTransformer([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type transformerType) : IOpenApiDocumentTransformer
{
	private readonly ObjectFactory _transformerFactory = ActivatorUtilities.CreateFactory(transformerType, []);
}

Note: this issue appears to repro specifically on the linker configuration used by ASP.NET Core's LinkabilityChecker tool. A more simplified repro on a console app doesn't share the same issue.

Reproduction Steps

  1. git clone --recursive https://github.com/dotnet/aspnetcore
  2. git checkout origin/safia/linker-primconstructors-bug
  3. ./restore.sh
  4. cd src/Tools/LinkabilityChecker
  5. dotnet run
  6. Observe linker exception.

Expected behavior

No linker exception is raised.

Actual behavior

Trim analysis warning IL2077: Microsoft.AspNetCore.OpenApi.TypeBasedOpenApiDocumentTransformer.TypeBasedOpenApiDocumentTransformer(Type):
 'instanceType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 
'Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type, Type[])'. The field 
'Microsoft.AspNetCore.OpenApi.TypeBasedOpenApiDocumentTransformer.<transformerType>P' does not have matching 
annotations. The source value must declare at least the same requirements as those declared on the target location it is 
assigned to.

Regression?

No response

Known Workarounds

Explicitly defining a transformerType field with the DAM annotations resolves the issue.

Configuration

$ dotnet --info                                                                                                                                     11:38:54
.NET SDK:
 Version:           9.0.100-preview.5.24229.2
 Commit:            d301a122c4
 Workload version:  9.0.100-manifests.90bb8117
 MSBuild version:   17.11.0-preview-24222-11+9cdb3615a

Other information

No response

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

Status

High Priority

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions