Fix UnreachableBody with only ref to assembly.#127090
Merged
sbomer merged 1 commit intodotnet:mainfrom Apr 22, 2026
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
sbomer
approved these changes
Apr 17, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a linker pipeline ordering issue where SweepStep could retain an assembly reference originating only from method bodies that would later be stubbed by CodeRewriterStep, leading to inconsistent output (reference retained while the referenced assembly is deleted).
Changes:
- Reorders the standard linker pipeline to run
CodeRewriterStepbeforeSweepStep. - Adds a new regression test covering the “only reference is inside an unreachable body” scenario.
- Adds a small dependency assembly used by the new test and updates the generated analyzer test list.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/tools/illink/src/linker/Linker/Driver.cs |
Moves CodeRewriterStep ahead of SweepStep in the standard pipeline to ensure sweeping observes the final rewritten bodies. |
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/OnlyReferenceInUnreachableBody.cs |
New test validating that an assembly reference originating only in a stubbed body is removed (and the referenced assembly can be removed). |
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/Dependencies/OtherAssembly2.cs |
New dependency type compiled into other.dll for the new regression test. |
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/.../UnreachableBodyTests.g.cs |
Adds the new test entry to the generated analyzer test suite. |
This was referenced Apr 18, 2026
Open
When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly. This was happening because the SweepStep was running before CodeRewriterStep. Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit type references to method bodies that were going to be stubbed. By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.
fc2a2d7 to
a9db693
Compare
Contributor
Author
|
@sbomer the PR is ready to be merged. The failures look unrelated. |
Member
|
/ba-g "CoreCLR_WasmBuildTests timeout" |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly.
This was happening because the SweepStep was running before CodeRewriterStep. Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit type references to method bodies that were going to be stubbed. By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.