Skip to content

Structs cannot be annotated as RequiresUnreferencedCode #90115

@m-redding

Description

@m-redding

Description

Structs cannot be annotated as RequiresUnreferencedCode. I'm trying to make a library AOT compatible, and we have multiple structs that are not compatible with trimming. In most cases, we can just annotate all incompatible methods with RequiresUnreferencedCode. However, we have 2 cases where we have an overload for ToString which is incompatible with trimming. Since we cannot annotate the method or struct the warning will never be resolved.

Reproduction Steps

internal struct Sample
{
        public override string ToString()
        {
                // Reflection-based serialization code not compatible with trimming
        }
}

Expected behavior

Ability to annotate structs as RequiresUnreferencedCode

Actual behavior

Adding RequiresUnreferencedCode attribute to a struct results in CS0592

Regression?

No response

Known Workarounds

For the ToString() overload case, none. For the general case, every method must be annotated.

Configuration

No response

Other information

No response

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