Skip to content

Conversation

@jtschuster
Copy link
Member

@jtschuster jtschuster commented Sep 3, 2025

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: #117072 (comment) and #117072 (comment)

@jtschuster jtschuster requested review from a team and Copilot September 3, 2025 18:51
@github-actions github-actions bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Sep 3, 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 PR refines the heuristics for propagating DynamicallyAccessedMembers annotations to backing fields in the trimmer and ILC tools. The change ensures that only fully auto-generated properties (where all accessors and backing fields have CompilerGeneratedAttribute) receive automatic annotation propagation, addressing compatibility issues with C# 14's field keyword and semi-auto properties.

Key changes:

  • Tightened property backing field detection heuristics to check CompilerGeneratedAttribute on all accessors
  • Added comprehensive test coverage for the field keyword functionality
  • Updated test infrastructure to use C# Preview language version to support new features

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
PropertyDataFlow.cs Added CompilerGeneratedAttribute to test accessors and new test class for unrecognized field scenarios
FieldKeyword.cs New comprehensive test file covering field keyword functionality with various property patterns
FlowAnnotations.cs (linker) Refactored backing field detection logic to use IsFullyAutoProperty check and improved error handling
FlowAnnotations.cs (ILC) Parallel implementation of the refined heuristics for ILC compiler
MethodDefinitionExtensions.cs Added IsCompilerGenerated extension method for checking compiler-generated attributes
Test infrastructure files Updated to use C# Preview language version for field keyword support

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Sep 3, 2025
Simplifies checks for if we need to warn because we were unable to find a backing field.
Adds a test case for when there is a single auto-prop accessor and it could not find the backing field.
Copy link
Member

@sbomer sbomer left a comment

Choose a reason for hiding this comment

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

LGTM, thanks a lot!

@jtschuster jtschuster requested a review from agocke September 4, 2025 23:35
@jtschuster jtschuster merged commit 541ac3e into dotnet:main Sep 5, 2025
117 checks passed
@jtschuster
Copy link
Member Author

/backport to release/10.0

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

@jtschuster backporting to "release/10.0" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Add more issue links for ExpectedWarnings that don't match
Using index info to reconstruct a base tree...
M	src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExceptionalDataFlow.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExponentialDataFlow.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsFromXML.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInAssembly.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInCompilerGeneratedCode.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypes.cs
M	src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypesUsingTarget.cs
Falling back to patching base and 3-way merge...
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/CompilerGeneratedCodeDataflow.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExceptionalDataFlow.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExceptionalDataFlow.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExponentialDataFlow.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/ExponentialDataFlow.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsFromXML.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsFromXML.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInAssembly.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInAssembly.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInCompilerGeneratedCode.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInCompilerGeneratedCode.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypes.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypes.cs
Auto-merging src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypesUsingTarget.cs
CONFLICT (content): Merge conflict in src/tools/illink/test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/DetectRedundantSuppressionsInMembersAndTypesUsingTarget.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Add more issue links for ExpectedWarnings that don't match
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

jtschuster added a commit to jtschuster/runtime that referenced this pull request Sep 5, 2025
…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)
jeffschwMSFT pushed a commit that referenced this pull request Sep 11, 2025
…nly (#119329) (#119407)

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: #117072 (comment) and #117072 (comment)
jtschuster added a commit that referenced this pull request Sep 22, 2025
Fixes #119780

We added a warning when we cannot find the backing field of an auto property in #119329 which caused some issues in illink->ilc scenarios when an auto property was stubbed out. Instead, we should just not propagate the DAM annotation.
@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzer doesn't warn about reflection access to generated backing fields

3 participants