Skip to content

Invalid trim warning suppression in BindConverter #57016

Open
@MichalStrehovsky

Description

@MichalStrehovsky

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

ParserDelegateCache.Get method body suppresses trim analysis warnings with the following justification:

[UnconditionalSuppressMessage(
"ReflectionAnalysis",
"IL2060:MakeGenericMethod",
Justification = "The referenced methods don't have any DynamicallyAccessedMembers annotations. See https://github.com/mono/linker/issues/1727")]
[UnconditionalSuppressMessage(
"ReflectionAnalysis",
"IL2075:MakeGenericMethod",
Justification = "The referenced methods don't have any DynamicallyAccessedMembers annotations. See https://github.com/mono/linker/issues/1727")]
public static BindParser<T> Get<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()

This is a lie. At minimum, the following code:

else if (typeof(T).IsArray)
{
var method = _makeArrayTypeConverter ??= typeof(ParserDelegateCache).GetMethod(nameof(MakeArrayTypeConverter), BindingFlags.NonPublic | BindingFlags.Static)!;
var elementType = typeof(T).GetElementType()!;
parser = (Delegate)method.MakeGenericMethod(elementType).Invoke(null, null)!;
}

is doing MakeGenericMethod on a method that is annotated. I don't see anything obvious that would ensure this invariant holds:

private static BindParser<T[]?> MakeArrayTypeConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()

Trim warning suppressions:

  • Should be reviewed with the same level of suspicion as unsafe. Assume it's a bug unless proven otherwise.
  • Should never be used on a 130 line method that does tons of reflection. Extract the suppressed block to a small helper method and suppress there.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-trimming

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions