dotnet / dotnet/linker

Instead of generating IL2045 use the lazy marking of attributes and preserve otherwise referenced attributes

Open
#1,378 5 comments 0 reactions 0 assignees View on GitHub
area-Linker: Diagnostic
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

Currently if an attribute is marked for removal of its instances via the `RemoveAttributeInstances` internal annotation and then the attribute type is directly referenced from the code (for example via `typeof(MyAttribute)`), the linker will generate warning IL2045 saying that this is potentially problematic as the attribute instances are being removed.

To mitigate the problem the developer can either:
* Suppress the warning via `UnconditionalSuppressMessage` attribute, assuming the code referencing the attribute type will continue working as expected (or if the change in behavior is acceptable).
* Force-include the attribute instances by marking the attribute type as preserved in a link descriptor XML file (anywhere in the app)

We could effectively remove the need for IL2045 by implementing attribute removal via the functionality already present in `MarkStep` for `--used-attribs-only`:
* If we see an attribute that is of a "removable type", don't mark it, just add it into the lazily marked list.
* If the attribute type ended up getting marked (e.g. because there was a `typeof`), mark the attributes as well (this is what the `--used-attribs-only` does too, but the twist would be that we don't let it happen to random attributes, only to those that are in the list).

See https://github.com/dotnet/runtime/issues/39707#issuecomment-662314499 for the discussion leading to this proposal.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.