Skip to content

IL2067 warning mentions wrong property, makes no sense #121629

@jonpryor

Description

@jonpryor

Description

I am observing an IL2067 warning which makes no sense to me, in that it mentions 'DynamicallyAccessedMemberTypes on a record parameter which doesn't have [DynamicallyAccessedMembers].

Reproduction Steps

Consider the following code:

using System.Diagnostics.CodeAnalysis;

record ViewMap(
    Func<Type?>? ViewSelector = null,
    [property: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
    [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
    Type? ViewModel = null,
    object? Data = null
);

record MessageDialogViewMap(
    [param:   DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicConstructors)]
    Type? ViewModel = default,
    object? Data = default
): ViewMap(
    ViewModel: ViewModel,
    Data: Data
);

Note that ViewMap.ViewModel has [DynamicallyAccessedMembers], while ViewModel.Data does not.

Build the project:

dotnet build -bl -p:IsTrimmable=true

It warns:

MyRecord.cs(15,4): warning IL2067: 'ViewModel' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'ViewMap.ViewMap(Func<Type>, Type, Object)'. The parameter 'Data' of method 'MessageDialogViewMap.MessageDialogViewMap(Type, Object)' 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.

Note The parameter 'Data' … does not have matching annotations. Data doesn't have any annotations! There's nothing to match!

Expected behavior

No warnings.

Actual behavior

An IL2067 warning (see Reproduction Steps).

Regression?

No response

Known Workarounds

Provide all the parameters to the base constructor invocation. This compiles without warning:

record MessageDialogViewMap(
    [param:   DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicConstructors)]
    Type? ViewModel = default,
    object? Data = default
): ViewMap(
    ViewSelector: null,
    ViewModel: ViewModel,
    Data: Data
);

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions