Fix calc() computation in trimmed / NativeAOT applications - #236
Merged
FlorianRappl merged 1 commit intoSep 4, 2026
Merged
Conversation
sebastienros
force-pushed
the
fix/aot-calc-metric-values
branch
from
September 3, 2026 23:37
d367563 to
0e8f9e1
Compare
The calc add/sub/mul/div expressions create the resulting metric value via Activator.CreateInstance(x.GetType(), result). The trimmer cannot see that call target, so the single-Double constructors of the built-in metric values were removed, making every calc() computation throw MissingMethodException in NativeAOT (and trimmed) applications. The reflection call now lives in a single helper that roots the public constructors of all built-in metric values via DynamicDependency, so the behavior matches the JIT one. ICssMetricValue is public, hence external implementations stay supported - they just have to preserve their own constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a2612f6f-7d43-40de-a31f-96a2600e6f81
sebastienros
force-pushed
the
fix/aot-calc-metric-values
branch
from
September 3, 2026 23:43
0e8f9e1 to
3680c6a
Compare
FlorianRappl
approved these changes
Sep 4, 2026
FlorianRappl
left a comment
Contributor
There was a problem hiding this comment.
Great catch - thanks!
This was referenced Sep 7, 2026
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.
Problem
Computing any
calc()that combines two metric values throws in NativeAOT (and trimmed) apps:Repro (NativeAOT, net10.0):
<style>p { width: calc(100px + 20px) }</style>+GetComputedStylereturns120pxon JIT but throws under AOT.The four calc expressions build their result with
Activator.CreateInstance(x.GetType(), result). The trimmer cannot follow that, so it removes theCssXyzValue(Double)constructors that nothing else calls. Publishing an app with these packages surfaced this as 4IL2072trim warnings — the only trim warnings coming out of AngleSharp.Css (AngleSharp, AngleSharp.Xml and AngleSharp.XPath produce none).Change
ICssMetricValue.WithValue(Double)that roots the public constructors of the eight built-in metric values via[DynamicDependency], plus a documented[UnconditionalSuppressMessage]forIL2072.ICssMetricValueis public, so the dynamic path is kept for external implementations — those simply have to preserve their own single-Doubleconstructor (the justification text says so).IsAotCompatiblein this bug-fix PR. That property should be considered in a later release because it also opts the assembly into trimming; enabling it now could cause unexpected trimming in applications referencing AngleSharp.Css.calc()addition/subtraction for lengths and times, and aWithValueround-trip for all eight metric types.Verification
dotnet test src/AngleSharp.Css.Tests/AngleSharp.Css.Tests.csproj→ 2084 passed.dotnet build src/AngleSharp.Css.sln -c Release→ 0 warnings.calc(100px + 20px)/calc(100px - 20px)now compute120px/80pxjust like on JIT.Note:
calc(2 * 8px)andcalc(30px / 2)still fail withInvalidOperationException: Unsupported unit cannot be converted.— that is a pre-existing issue that reproduces identically on JIT and is out of scope here.