Skip to content

Fix overreporting of warnings due to RequiresUnreferencedCode#1798

Merged
vitek-karas merged 4 commits intodotnet:masterfrom
vitek-karas:FixRequiresUnreferencedCodeDetection
Feb 3, 2021
Merged

Fix overreporting of warnings due to RequiresUnreferencedCode#1798
vitek-karas merged 4 commits intodotnet:masterfrom
vitek-karas:FixRequiresUnreferencedCodeDetection

Conversation

@vitek-karas
Copy link
Member

Linker has intrinsic handling for some methods which are annotated with RequiresUnreferencedCodeAttribute. The intrinsic handling will sometimes recognize the pattern and avoid generating a warning, at other times it will generate a better more specific warning. In cases where this happens the generic handler for RequiresUnreferencedCodeAttribute should not kick in, as it would still produce a warning.

The change:

  • For direct calls, virtual calls and .ctor calls rely on ReflectionMethodBodyScanner to handle all warnings caused by RequiresUnreferencedCodeAttribute. It has knowledge about intrinsic handling and can handle all non-intrisic calls as well.
  • Limit the generic handler in MarkMethod to all other cases (typically reflection or other indirect references to the method)

Test changes:

  • We already have test cases which trigger these conditions, but we don't have a way to validate that extra warnings were not produced
  • Added ExpectedNoWarnings test attribute which validates no warnings other than those explicitly stated by the test are produced.
  • Used it on two test cases which trigger the interesting condition - and fixed the tests to correctly expect all warnings

Linker has intrinc handling for some methods which are annotated with RequiresUnreferencedCodeAttribute. The intrinsic handling will sometimes recognize the pattern and avoid generating a warning, at other times it will generate a better more specific warning. In cases where this happens the generic handler for RequiresUnreferencedCode should not kick in, as it would still produce a warning.

The change:
* For direct calls, virtual calls and .ctor calls rely on ReflectionMethodBodyScanner to handle all warnings caused by Re
@vitek-karas vitek-karas added this to the .NET 6.0 milestone Feb 3, 2021
@vitek-karas vitek-karas requested a review from mateoatr February 3, 2021 12:13
@vitek-karas vitek-karas self-assigned this Feb 3, 2021
// of the methods annotated with the attribute (for example Type.GetType)
// and it know when it's OK and when it needs a warning. In this place we don't know
// and would have to warn every time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding assert for that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very hard to "verify" this - the code will get here for DirectCall (and the others), actually before it gets to ReflectionMethodBodyScanner. The only sensible check would be in ProcessMethodBody that if we see a method call to annotated method, we enable ReflectionMethodBodyScanner - but that is already there, since that's the place which enables the data flow processing.

This code here runs during MarkMethod, the data flow analysis runs during ProcessMethod, so much later on.

I do agree that this relationship is a bit fragile, but I can't think of a better way to handle it right now.

Copy link
Contributor

@mateoatr mateoatr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vitek-karas vitek-karas merged commit faf9cc0 into dotnet:master Feb 3, 2021
@vitek-karas vitek-karas deleted the FixRequiresUnreferencedCodeDetection branch March 13, 2021 21:47
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…/linker#1798)

Linker has intrinsic handling for some methods which are annotated with `RequiresUnreferencedCodeAttribute`. The intrinsic handling will sometimes recognize the pattern and avoid generating a warning, at other times it will generate a better more specific warning. In cases where this happens the generic handler for `RequiresUnreferencedCodeAttribute` should not kick in, as it would still produce a warning.

The change:
* For direct calls, virtual calls and `.ctor` calls rely on `ReflectionMethodBodyScanner` to handle all warnings caused by `RequiresUnreferencedCodeAttribute`. It has knowledge about intrinsic handling and can handle all non-intrisic calls as well.
* Limit the generic handler in `MarkMethod` to all other cases (typically reflection or other indirect references to the method)

Test changes:
* We already have test cases which trigger these conditions, but we don't have a way to validate that extra warnings were not produced
* Added `ExpectedNoWarnings` test attribute which validates no warnings other than those explicitly stated by the test are produced.
* Used it on two test cases which trigger the interesting condition - and fixed the tests to correctly expect all warnings

Commit migrated from dotnet/linker@faf9cc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants