Skip to content

Fix a regression to collection asserting in AssemblyChecker#125443

Merged
sbomer merged 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-seq-comparison
Mar 11, 2026
Merged

Fix a regression to collection asserting in AssemblyChecker#125443
sbomer merged 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-seq-comparison

Conversation

@mrvoorhe
Copy link
Copy Markdown
Contributor

@mrvoorhe mrvoorhe commented Mar 11, 2026

#97605 introduced a pattern that regressed test coverage and in some cases broke asserting. There were a few different regressions.

  1. The introduction of ToHashSet() results in deduplication. In some cases this was harmless, but it others it causes very real problems. VerifyBodyProperties is used by VerifyInstructions which means the test framework was deduplicating before asserting bodies were unchanged.

  2. SetEquals removed ordering requirements. This was never necessary and in some places really undermines coverage. If a method body had it's instructions scrambled the test would still happily pass.

This PR removes the ToHashSet() and SetEquals() usages from AssemblyChecker. There's another issue.

#116355 added the TypeMap test. This test introduced a dependency on the deduplication behavior. Roslyn will deduplicate assembly attributes at compile time. This meant that there was no way to get this test passing using the existing pattern of defining [KeptAttributeAttribute(typeof(TypeMapAttribute<UsedType>))] multiple times in order to tell the test framework that multiple instances of an attribute should survive. To solve this, we've upgrade the KeptAttributeAttribute assertion ability to allow for specifying the attribute parameters that you expect to survive. This allows for matching up the KeptAttributeAttribute with the exact attribute instance. This new ability is then used in TypeMap to update the assertions at the assembly level to pass with the deduplication removed.

Some new tests have been added to TestFramework.

@mrvoorhe mrvoorhe requested a review from sbomer as a code owner March 11, 2026 14:06
Copilot AI review requested due to automatic review settings March 11, 2026 14:06
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Mar 11, 2026
@dotnet-policy-service dotnet-policy-service Bot added linkable-framework Issues associated with delivering a linker friendly framework community-contribution Indicates that the PR has been added by a community member labels Mar 11, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

The introduction of `ToHashSet()` + `SetEquals` in `AssemblyChecker` caused a regression in the validation in various things.

fixes validation of stubbed methods

Expand attribute verification to allow for type checking of parameters.  This is needed to deal with assertions in the TypeMap step.
@mrvoorhe mrvoorhe force-pushed the linker-fix-seq-comparison branch from 6c203d4 to 9061e9d Compare March 11, 2026 14:09
@mrvoorhe
Copy link
Copy Markdown
Contributor Author

@sbomer Please take a look. This PR replaces #125224

Copy link
Copy Markdown
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 updates the ILLink test infrastructure to avoid masking regressions when asserting collections (particularly method body/instruction sequences) by removing ToHashSet()-based deduplication and SetEquals()-based order-insensitive comparisons in AssemblyChecker. It also extends KeptAttributeAttribute assertions so tests can match specific attribute instances by constructor arguments (needed for the TypeMap coverage), and adds new test cases to validate the updated behavior.

Changes:

  • Replace ToHashSet()/SetEquals() comparisons with sequence-based comparisons in AssemblyChecker.
  • Extend KeptAttributeAttribute expectations to optionally include constructor argument matching; update TypeMap assertions accordingly.
  • Add new test cases covering locals/body changes and parameterized KeptAttributeAttribute assertions; update generated test list.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/tools/illink/test/Mono.Linker.Tests/TestCasesRunner/AssemblyChecker.cs Removes dedup/order-insensitive comparisons; adds attribute-argument-aware matching logic.
src/tools/illink/test/Mono.Linker.Tests.Cases/TestFramework/VerifyLocalsAreChanged.xml Linker substitution file to force body stubbing for locals verification.
src/tools/illink/test/Mono.Linker.Tests.Cases/TestFramework/VerifyLocalsAreChanged.cs New test case validating locals/body sequence assertions.
src/tools/illink/test/Mono.Linker.Tests.Cases/TestFramework/VerifyKeptAttributeAttributeWorks.cs New test case validating KeptAttributeAttribute matching (including duplicates/args).
src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/TypeMap.cs Updates assembly-level KeptAttributeAttribute assertions to include constructor arguments.
src/tools/illink/test/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptAttributeAttribute.cs Adds a new constructor for parameter-aware assertions and documents intended usage.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/ILLink.RoslynAnalyzer.Tests.Generator/ILLink.RoslynAnalyzer.Tests.TestCaseGenerator/TestFrameworkTests.g.cs Generated test list updated to include the new test cases.

@sbomer sbomer enabled auto-merge (squash) March 11, 2026 20:52
@sbomer
Copy link
Copy Markdown
Member

sbomer commented Mar 11, 2026

/ba-g "timeouts"

@sbomer sbomer merged commit 83573d2 into dotnet:main Mar 11, 2026
86 of 88 checks passed
Copilot AI pushed a commit that referenced this pull request Mar 13, 2026
#97605 introduced a pattern that
regressed test coverage and in some cases broke asserting. There were a
few different regressions.

1) The introduction of `ToHashSet()` results in deduplication. In some
cases this was harmless, but it others it causes very real problems.
`VerifyBodyProperties` is used by `VerifyInstructions` which means the
test framework was deduplicating before asserting bodies were unchanged.

2) `SetEquals` removed ordering requirements. This was never necessary
and in some places really undermines coverage. If a method body had it's
instructions scrambled the test would still happily pass.

This PR removes the `ToHashSet()` and `SetEquals()` usages from
`AssemblyChecker`. There's another issue.

#116355 added the `TypeMap` test.
This test introduced a dependency on the deduplication behavior. Roslyn
will deduplicate assembly attributes at compile time. This meant that
there was no way to get this test passing using the existing pattern of
defining `[KeptAttributeAttribute(typeof(TypeMapAttribute<UsedType>))]`
multiple times in order to tell the test framework that multiple
instances of an attribute should survive. To solve this, we've upgrade
the `KeptAttributeAttribute` assertion ability to allow for specifying
the attribute parameters that you expect to survive. This allows for
matching up the `KeptAttributeAttribute` with the exact attribute
instance. This new ability is then used in `TypeMap` to update the
assertions at the assembly level to pass with the deduplication removed.

Some new tests have been added to `TestFramework`.

Co-authored-by: Adriano Carlos Verona <adriano@unity3d.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 11, 2026
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 community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants