[release/10.0] Propagate DynamicallyAccessedMembers annotations to auto-properties only#119407
Merged
jeffschwMSFT merged 1 commit intodotnet:release/10.0from Sep 11, 2025
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)
Contributor
There was a problem hiding this comment.
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 |
Contributor
|
Tagging subscribers to this area: @dotnet/illink |
jeffschwMSFT
approved these changes
Sep 8, 2025
Member
jeffschwMSFT
left a comment
There was a problem hiding this comment.
approved. please get a code review. we can merge when ready
sbomer
approved these changes
Sep 8, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
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.