Skip to content

Annotate TValue on ConditionalWeakTable.Remove polyfill to fix IL2091 - #616

Merged
SimonCropp merged 2 commits into
mainfrom
conditional-weak-table-remove-trim-annotation
Oct 5, 2026
Merged

SimonCropp merged 2 commits into
mainfrom
conditional-weak-table-remove-trim-annotation

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

Fixes #615

ConditionalWeakTable<TKey, TValue> declares TValue with [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] (net6.0 through net10.0). The Remove(key, out value) polyfill declared TValue without it, so trim analysis reported IL2091 for any consumer that roots its assembly, even if it never calls Remove.

Changes

  • Polyfill_ConditionalWeakTable.cs: annotate TValue to match ConditionalWeakTable. Split copies regenerated.
  • ConsumeTrimmed.csproj: add <TrimmerRootAssembly Include="$(AssemblyName)" />. The project previously only trimmed reachable code, and nothing reachable calls Remove, so the trimmer never analyzed it. Rooting the assembly makes it analyze every polyfill.
  • assemblySize.include.md: regenerated by ApiBuilderTests.

Verification

  • Before the fix, the rooted net9.0 publish of ConsumeTrimmed failed with the IL2091 from the issue, and it was the only trim warning.
  • After the fix, the rooted publish (PublishAll) is clean on net7.0 through net11.0.
  • Consume compiles on all target frameworks.
  • ConditionalWeakTableRemove passes on net48 and net9.0.

Note

CI builds ConsumeTrimmed but does not publish it, so the rooting only catches this class of bug when PublishAll is run. A publish step in build.yml would make it a standing guard; not included here.

SimonCropp and others added 2 commits October 5, 2026 18:58
ConditionalWeakTable<TKey, TValue> declares TValue with
DynamicallyAccessedMembers(PublicParameterlessConstructor). The Remove(key, out value)
polyfill did not, so trim analysis reported IL2091 for any consumer that roots its assembly.

Root the ConsumeTrimmed assembly so the trimmer analyzes every polyfill, not only the reachable ones.

Fixes #615
@SimonCropp SimonCropp added this to the 11.4.3 milestone Oct 5, 2026
@SimonCropp
SimonCropp merged commit e444042 into main Oct 5, 2026
1 check passed
@SimonCropp
SimonCropp deleted the conditional-weak-table-remove-trim-annotation branch October 5, 2026 08:02
This was referenced Oct 5, 2026
This was referenced Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConditionalWeakTable.Remove(key, out value) polyfill causes IL2091

2 participants