Skip to content

Revert variance changes for trimming attributes validation#123577

Closed
Copilot wants to merge 8 commits intomainfrom
copilot/fix-trimming-attributes-variance
Closed

Revert variance changes for trimming attributes validation#123577
Copilot wants to merge 8 commits intomainfrom
copilot/fix-trimming-attributes-variance

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

Description

Reverts attempted variance handling for DynamicallyAccessedMembers and Requires* attributes in override/interface implementation scenarios.

The variance approach was incorrect:

  • Requires attributes*: Removing from interface implementations would allow callers to cast to the interface and bypass warnings while executing the same code
  • DynamicallyAccessedMembers: Exact matching ensures consistent behavior regardless of access path (base, derived, or interface)

Changes

  • Restored original exact-match validation for FlowAnnotations.ValidateMethodAnnotationsAreSame in ILLink and NativeAOT
  • Restored original DynamicallyAccessedMembersAnalyzer.VerifyDamOnMethodsMatch in Roslyn Analyzer
  • Restored original test expectations in VirtualMethodHierarchyDataflowAnnotationValidation and RequiresAttributeMismatch

Test Results

  • All 81 DataFlow tests: PASSED
  • All 23 RequiresCapability tests: PASSED
Original prompt

This section details on the original issue you should resolve

<issue_title>Trimming attributes don't fully handle variance</issue_title>
<issue_description>Most of the trimming analysis is too conservative in how it handles attributes in override/interface situations.

In overriding, hiding, and interface implementation (OHI):

  • DynamicallyAccessedMembers should allow adding DAMT items in member outputs and removing DAMT items on inputs, but not vice versa.
  • Removing Requires* from an OHI member should be allowed, but not adding</issue_description>

Comments on the Issue (you are @copilot in this section)

@agocke That would be unsafe. You could always cast down to the interface and remove the warning. This is follows from the general variance rules. You can loosen input requirements in subtypes, but you can’t strengthen. For example, you could change a parameter type from string to object, but not vice versa (note, c# only allows this for interfaces)

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 24, 2026
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jan 24, 2026
Copilot AI and others added 2 commits January 24, 2026 19:02
…ariance

Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix trimming attributes to handle variance correctly Implement variance-aware validation for trimming attributes in OHI scenarios Jan 24, 2026
Copilot AI requested a review from agocke January 24, 2026 19:15
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI requested a review from agocke January 24, 2026 19:28
@jkotas jkotas added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 24, 2026
@dotnet-policy-service
Copy link
Contributor

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

@agocke
Copy link
Member

agocke commented Jan 25, 2026

@copilot

 Failed RequiresCapability.RequiresAttributeMismatch [2 s]
  Error Message:
   Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ImplementationClassWithoutRequires.Method(): and message containing 'ImplementationClassWithoutRequires.Method()' 'IBaseWithRequires.Method()', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ImplementationClassWithoutRequires.PropertyAnnotationInAccesor.get: and message containing 'ImplementationClassWithoutRequires.PropertyAnnotationInAccesor.get' 'IBaseWithRequires.PropertyAnnotationInAccesor.get', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ImplementationClassWithoutRequires.PropertyAnnotationInPropertyAndAccessor.set: and message containing 'ImplementationClassWithoutRequires.PropertyAnnotationInPropertyAndAccessor.set' 'IBaseWithRequires.PropertyAnnotationInPropertyAndAccessor.set', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ExplicitImplementationClassWithoutRequires.Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.IBaseWithRequires.Method(): and message containing 'IBaseWithRequires.Method()' 'ExplicitImplementationClassWithoutRequires.Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.IBaseWithRequires.Method()', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ExplicitImplementationClassWithoutRequires..Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.IBaseWithRequires.get_PropertyAnnotationInAccesor.get: and message containing 'PropertyAnnotationInAccesor.get' 'IBaseWithRequires.PropertyAnnotationInAccesor.get', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ExplicitImplementationClassWithoutRequires..Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.IBaseWithRequires.set_PropertyAnnotationInPropertyAndAccessor.set: and message containing 'PropertyAnnotationInPropertyAndAccessor.set' 'IBaseWithRequires.PropertyAnnotationInPropertyAndAccessor.set', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ImplementationClassWithoutRequiresInSource.Method(): and message containing 'ImplementationClassWithoutRequiresInSource.Method()' 'IBaseWithRequiresInReference.Method()', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.ImplementationClassWithoutRequiresInSource.PropertyAnnotationInAccesor.get: and message containing 'ImplementationClassWithoutRequiresInSource.PropertyAnnotationInAccesor.get' 'IBaseWithRequiresInReference.PropertyAnnotationInAccesor.get', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.StaticInterfaceMethods.ImplIRequiresMismatching.VirtualMethod(): and message containing 'ImplIRequiresMismatching.VirtualMethod' 'IRequires.VirtualMethod', but no such message was found.
Expected to find warning: warning IL2046: Mono.Linker.Tests.Cases.RequiresCapability.RequiresAttributeMismatch.StaticInterfaceMethods.ImplIRequiresMismatching.AbstractMethod(): and message containing 'ImplIRequiresMismatching.AbstractMethod' 'IRequires.AbstractMethod', but no such message was found.
Unmatched Messages:

  Stack Trace:
     at Mono.Linker.Tests.TestCasesRunner.ResultChecker.VerifyLoggedMessages(AssemblyDefinition original, TrimmingTestLogger logger, Boolean checkRemainingErrors) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs:line 1196
   at Mono.Linker.Tests.TestCasesRunner.ResultChecker.Check(TrimmedTestCaseResult linkResult) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs:line 131
   at Mono.Linker.Tests.TestCases.All.RequiresCapabilityTests(TestCase testCase) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCases/TestSuites.cs:line 196
   at InvokeStub_All.RequiresCapabilityTests(Object, Span`1)

1)    at Mono.Linker.Tests.TestCasesRunner.ResultChecker.VerifyLoggedMessages(AssemblyDefinition original, TrimmingTestLogger logger, Boolean checkRemainingErrors) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs:line 1196
   at Mono.Linker.Tests.TestCasesRunner.ResultChecker.Check(TrimmedTestCaseResult linkResult) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs:line 131
   at Mono.Linker.Tests.TestCases.All.RequiresCapabilityTests(TestCase testCase) in /_/src/tools/illink/test/Mono.Linker.Tests/TestCases/TestSuites.cs:line 196
   at InvokeStub_All.RequiresCapabilityTests(Object, Span`1)

Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI changed the title Implement variance-aware validation for trimming attributes in OHI scenarios Revert variance changes for trimming attributes validation Feb 16, 2026
@dotnet-policy-service
Copy link
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Trimming attributes don't fully handle variance

3 participants