Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress IL2026 with attributes #5305

Closed

Conversation

martincostello
Copy link
Member

@martincostello martincostello commented Jul 23, 2024

Use attributes to suppress IL2026 so suppressions are visible to the linker.

I originally noticed this here when trying to root the assembly for the trimmer: dotnet/runtime#105034 (comment)

I couldn't find any specific justification for the four suppressions. Two came from #4410 and the rest were present when r9 was originally imported to the repo. I assume it's related to the validation source generator? I can fill in the justification as appropriate.

If these shouldn't be suppressed, then instead the properties should instead pass along some variant of the original warning on [Length] by adding [RequiresUnreferencedCode] (which is [RequiresUnreferencedCode("Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.")]).

Microsoft Reviewers: Open in CodeFlow

Use attributes to suppress IL2026 so suppressions are visible to the linker.
@MichalStrehovsky
Copy link
Member

I think this should stay as a pragma. If I understand this correctly, the warning comes from the attribute LengthAttribute itself - creating an instance of the attribute at runtime is not safe. Accessing the property annotated with it is completely fine.

Simply accessing the property won't generate warnings, since accessing a property is not something that deals with attributes, it's just an accessor method call. Reflecting on the property is a problem because it allows activating the problematic attribute. You see a new warning when reflection-rooting the whole assembly because it means "please consider the property reflection accessed". This is by design, it should warn, because the attribute is not trim safe. Using an attribute would remove that warning, and actually activating the attribute would result in a broken app without publish-time warnings; that's a bug.

@martincostello martincostello deleted the il-suppression-attributes branch July 27, 2024 07:37
@github-actions github-actions bot locked and limited conversation to collaborators Aug 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants