Skip to content

MakeGenericType generates wrong warning for annotation mismatch #104911

@vitek-karas

Description

@vitek-karas

For example:

class GenericType<[DynamicallyAccessedMembers(PublicFields)] TWithFields> { }

void Test([DynamicallyAccessedMembers(PublicMethods)] Type type)
{
    typeof (GenericType).MakeGenericType (type);
}

This generates a warning like:

ILLink: Trim analysis warning IL2070: Test(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields' in call to 'System.Type.MakeGenericType(Type[])'. The parameter 'type' of method 'Test(Type)' 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.

This says that the "this" parameter for the MakeGenericType - which in this case the typeof (GenericType) as having requirements. That is not correct, the requirement comes from a generic parameter of that type. The warning should be:

IL2071: Generic argument for 'TWithFields` does not satisfy ....

The bug is at this line:
https://github.com/dotnet/linker/blob/6880454ee1a29cb9e8239cb28d352228712661ee/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs#L1900

The target context should be the genericParameter[i], not the method itself (which means "this" for that method).

Metadata

Metadata

Assignees

No one assigned

    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