-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[release/10.0] Propagate DynamicallyAccessedMembers annotations to auto-properties only #119407
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
[release/10.0] Propagate DynamicallyAccessedMembers annotations to auto-properties only #119407
Conversation
…nly (dotnet#119329) In the trimmer and ILC, we searched for backing fields with heuristics that validated a backing field had a `CompilerGeneratedAttribute`. In C# 14, the `field` keyword / semi-auto property feature means that some properties will have a compiler-generated field, but a user-written accessor. This can cause holes and unexpected behavior with the current heuristics. The semi-auto property accessors do not have `CompilerGeneratedAttribute`, so we can tighten the heuristic to only find backing fields when all accessors _and_ the found backing field have `CompilerGeneratedAttribute`. This should apply backing field propagation heuristics to properties with at least one auto accessor (e.g. `int Prop { get => field; set; }`) only (and also warns when it is unable to find the backing field for these). For all other properties, it is possible to annotate the backing field and accessors to achieve the same behavior. Additional context: dotnet#117072 (comment) and dotnet#117072 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This backport tightens the heuristics for propagating DynamicallyAccessedMembers annotations from properties to their backing fields. The change ensures annotation propagation only occurs for auto-properties (where at least one accessor has CompilerGeneratedAttribute), preventing issues with C# 14's semi-auto properties using the field keyword.
Key changes:
- Restricts backing field propagation to auto-properties only
- Adds comprehensive test coverage for various property patterns
- Updates analyzer to support C# preview features for testing
Reviewed Changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| FlowAnnotations.cs (linker) | Core logic change to check for auto-properties before propagating annotations |
| FlowAnnotations.cs (analyzer) | Updates analyzer to handle auto-property backing field annotations |
| FlowAnnotations.cs (ILC) | Parallel implementation for native AOT compiler |
| MethodDefinitionExtensions.cs | Adds helper method to check CompilerGenerated attribute |
| IPropertySymbolExtensions.cs | Adds auto-property detection for Roslyn analyzer |
| PropertyDataFlow.cs | Extensive test cases for property annotation behavior |
| FieldKeyword.cs | New test file for C# 14 field keyword scenarios |
| Test infrastructure files | Updates to support C# preview language features |
|
Tagging subscribers to this area: @dotnet/illink |
jeffschwMSFT
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approved. please get a code review. we can merge when ready
Backport of #119329 to release/10.0
In the trimmer and ILC, we searched for backing fields with heuristics that validated a backing field had a
CompilerGeneratedAttribute. In C# 14, thefieldkeyword / semi-auto property feature means that some properties will have a compiler-generated field, but a user-written accessor. This can cause holes and unexpected behavior with the current heuristics. The semi-auto property accessors do not haveCompilerGeneratedAttribute, so we can tighten the heuristic to only find backing fields when all accessors and the found backing field haveCompilerGeneratedAttribute. This should apply backing field propagation heuristics to properties with at least one auto accessor (e.g.int Prop { get => field; set; }) only (and also warns when it is unable to find the backing field for these). For all other properties, it is possible to annotate the backing field and accessors to achieve the same behavior.The best way to unify behavior is to only propagate the annotation from the property to the field when one of the accessors is an auto-property accessor (
get;orset;). Trying to find the compiler-generated backing field in all possible cases with semi-auto properties (with thefieldkeyword) would be fragileAdditional context: #117072 (comment) and #117072 (comment)
Customer Impact
Properties annotated with
[DynamicallyAccessedMembers]that use the newfieldkeyword could have extra compile time warnings that don't show up when trimming.Regression
No. This only affects new code patterns in C# 14.
Testing
Tests were added to validate warning behavior for different patterns of semi and auto properties with annotations on the property.
Risk
Low.
The tightening of the heuristic for finding backing fields only excludes properties using the new patterns of C# 14. It shouldn't affect existing code in any way.