Skip to content

Conversation

@jtschuster
Copy link
Member

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, 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.

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; or set;). Trying to find the compiler-generated backing field in all possible cases with semi-auto properties (with the field keyword) would be fragile

Additional context: #117072 (comment) and #117072 (comment)

Customer Impact

Properties annotated with [DynamicallyAccessedMembers] that use the new field keyword could have extra compile time warnings that don't show up when trimming.

void ValidateValue(
  [DynamicallyAccessedMembers(PublicMethods)]
  Type param)
{ }

[DynamicallyAccessedMembers(PublicMethod)]
Type MyType {
  get
  {
    return field; // "Field does not have PublicFields annotation" warning in analyzer, not in trimmer/ilc
  }
  set
  {
    field = value;
    ValidateValue(field); // "Field does not have PublicFields annotation" warning in analyzer, not in trimmer/ilc
  }
}

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.

…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)
@jtschuster jtschuster self-assigned this Sep 5, 2025
Copilot AI review requested due to automatic review settings September 5, 2025 19:22
@github-actions github-actions bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Sep 5, 2025
Copy link
Contributor

Copilot AI left a 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

@dotnet-policy-service dotnet-policy-service bot added linkable-framework Issues associated with delivering a linker friendly framework labels Sep 5, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/illink
See info in area-owners.md if you want to be subscribed.

@jtschuster jtschuster added the Servicing-consider Issue for next servicing release review label Sep 5, 2025
@jtschuster jtschuster changed the title Propagate DynamicallyAccessedMembers annotations to auto-properties only [release/10.0] Propagate DynamicallyAccessedMembers annotations to auto-properties only Sep 5, 2025
Copy link
Member

@jeffschwMSFT jeffschwMSFT left a 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

@jeffschwMSFT jeffschwMSFT added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Sep 8, 2025
@jeffschwMSFT jeffschwMSFT added this to the 10.0.0 milestone Sep 8, 2025
@jeffschwMSFT jeffschwMSFT merged commit a26121b into dotnet:release/10.0 Sep 11, 2025
124 of 129 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers linkable-framework Issues associated with delivering a linker friendly framework Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants