Skip to content

[dotnet-linker] Don't unconditionally preserve smart enum conversions in trimmed assemblies. - #26486

Merged
rolfbjarne merged 3 commits into
mainfrom
dev/rolf/preserve-smart-enum-conversions-trimmed-assemblies
Aug 28, 2026
Merged

rolfbjarne merged 3 commits into
mainfrom
dev/rolf/preserve-smart-enum-conversions-trimmed-assemblies

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

PreserveSmartEnumConversionsStep has two ways of preserving the smart enum conversion methods:

  • Inject a [DynamicDependency] attribute on the method that references the smart enum. This is conditional: the conversion methods only survive if the referencing method survives.
  • Emit an ILLink root descriptor file. This preserves the conversion methods unconditionally, and is required when we can't modify the assembly (hot reload).

Which one to use was decided by the global HotReloadCompatibleBuild option, which defaults to true for all debug builds. That meant that every smart enum referenced anywhere in the platform assembly was preserved unconditionally, which in turn kept a lot of code alive that should have been trimmed away (15+ extra namespaces surviving in the platform assembly, which made us link with the corresponding frameworks).

Assemblies with AssemblyAction.Link are being trimmed, so they're not hot reloadable and can safely be modified - this is the same condition we already use in RemoveUserResourcesSubStep, ListExportedSymbols and InlineDlfcnMethodsStep.

Verified by inspecting a Full-linked Microsoft.iOS.dll: the number of surviving namespaces went from 19 down to 6.

πŸ€– Pull request created by Copilot

… in trimmed assemblies.

`PreserveSmartEnumConversionsStep` has two ways of preserving the smart enum
conversion methods:

* Inject a `[DynamicDependency]` attribute on the method that references the
  smart enum. This is conditional: the conversion methods only survive if the
  referencing method survives.
* Emit an ILLink root descriptor file. This preserves the conversion methods
  unconditionally, and is required when we can't modify the assembly (hot
  reload).

Which one to use was decided by the global `HotReloadCompatibleBuild` option,
which defaults to true for all debug builds. That meant that every smart enum
referenced anywhere in the platform assembly was preserved unconditionally,
which in turn kept a lot of code alive that should have been trimmed away (15+
extra namespaces surviving in the platform assembly, which made us link with
the corresponding frameworks).

Assemblies with `AssemblyAction.Link` are being trimmed, so they're not hot
reloadable and can safely be modified - this is the same condition we already
use in `RemoveUserResourcesSubStep`, `ListExportedSymbols` and
`InlineDlfcnMethodsStep`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ef04d7c1-e5e3-4244-ac92-f7341ddac90e
@rolfbjarne
rolfbjarne requested a review from dalexsoto as a code owner August 28, 2026 04:49
Copilot AI lite review requested due to automatic review settings August 28, 2026 04:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adjusts PreserveSmartEnumConversionsStep so smart-enum conversion methods are only preserved unconditionally (via an ILLink root-descriptor XML) when the referencing assembly can’t be modified due to Hot Reload constraints, instead of using a global Hot Reload setting that caused over-preservation in trimmed platform assemblies.

Changes:

  • Make the XML-descriptor decision depend on the referencing assembly’s AssemblyAction (avoid XML preservation for AssemblyAction.Link assemblies).
  • Move the XML-preservation decision into the per-condition loop so mixed scenarios (some assemblies modifiable, some not) are handled correctly.
  • Emit the XML descriptor only when it has collected entries (no longer gated by a global flag at end-of-step).

πŸ’‘ Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne and others added 2 commits August 28, 2026 10:12
…embly behavior.

The Hot Reload code path in `PreserveSmartEnumConversionsStep` now only kicks in
for assemblies that aren't trimmed, so update `HotReloadCompatibleBuildTest` to
build the test assembly as a non-trimmed (copy) assembly.

Also add `HotReloadCompatibleBuildTrimmedAssemblyTest`, which verifies that
[DynamicDependency] attributes are injected into trimmed assemblies even when
$(HotReloadCompatibleBuild) is enabled.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Linked assemblies use conditional smart enum dependencies even in hot-reload-compatible prepare builds, so unused platform smart enums should not survive linking.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rolfbjarne
rolfbjarne enabled auto-merge (squash) August 28, 2026 08:43
@rolfbjarne rolfbjarne added the ready-to-review This PR is ready to review/merge. label Aug 28, 2026
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

βœ… API diff for current PR / commit

NET (empty diffs)

βœ… API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 83e09a46b37a26fb11533038f6c1188c3daf5a82 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

πŸš€ [CI Build #83e09a4] Test results πŸš€

Test results

βœ… All tests passed on VSTS: test results.

πŸŽ‰ All 205 tests passed πŸŽ‰

Tests counts

βœ… assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
βœ… cecil: All 1 tests passed. Html Report (VSDrops) Download
βœ… dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
βœ… dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
βœ… dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
βœ… dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
βœ… dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
βœ… framework: All 2 tests passed. Html Report (VSDrops) Download
βœ… fsharp: All 4 tests passed. Html Report (VSDrops) Download
βœ… generator: All 5 tests passed. Html Report (VSDrops) Download
βœ… interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
βœ… introspection: All 4 tests passed. Html Report (VSDrops) Download
βœ… linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
βœ… linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
βœ… linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
βœ… linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
βœ… monotouch (iOS): All 19 tests passed. Html Report (VSDrops) Download
βœ… monotouch (MacCatalyst): All 18 tests passed. Html Report (VSDrops) Download
βœ… monotouch (macOS): All 21 tests passed. Html Report (VSDrops) Download
βœ… monotouch (tvOS): All 19 tests passed. Html Report (VSDrops) Download
βœ… msbuild: All 2 tests passed. Html Report (VSDrops) Download
βœ… sharpie: All 1 tests passed. Html Report (VSDrops) Download
βœ… windows: All 3 tests passed. Html Report (VSDrops) Download
βœ… xcframework: All 4 tests passed. Html Report (VSDrops) Download
βœ… xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

βœ… Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
βœ… Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
βœ… Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
βœ… Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
βœ… Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

βœ… Linux build succeeded

Pipeline on Agent
Hash: 83e09a46b37a26fb11533038f6c1188c3daf5a82 [PR build]

@rolfbjarne

Copy link
Copy Markdown
Member Author

@dalexsoto stamp

@rolfbjarne
rolfbjarne merged commit fab2bbc into main Aug 28, 2026
56 checks passed
@rolfbjarne
rolfbjarne deleted the dev/rolf/preserve-smart-enum-conversions-trimmed-assemblies branch August 28, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot ready-to-review This PR is ready to review/merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants