Fix overreporting of warnings due to RequiresUnreferencedCode#1798
Merged
vitek-karas merged 4 commits intodotnet:masterfrom Feb 3, 2021
Merged
Conversation
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
marek-safar
reviewed
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. | ||
|
|
Contributor
There was a problem hiding this comment.
Is it worth adding assert for that?
Member
Author
There was a problem hiding this comment.
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.
marek-safar
approved these changes
Feb 3, 2021
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 forRequiresUnreferencedCodeAttributeshould not kick in, as it would still produce a warning.The change:
.ctorcalls rely onReflectionMethodBodyScannerto handle all warnings caused byRequiresUnreferencedCodeAttribute. It has knowledge about intrinsic handling and can handle all non-intrisic calls as well.MarkMethodto all other cases (typically reflection or other indirect references to the method)Test changes:
ExpectedNoWarningstest attribute which validates no warnings other than those explicitly stated by the test are produced.