fix: prevent toexpr from rewriting Const values via st.rewrites#870
Merged
AayushSabharwal merged 1 commit intoJuliaSymbolics:masterfrom Feb 28, 2026
Conversation
In v4.18.4, the refactored `toexpr(O::BasicSymbolic{T}, st)` function
started checking `st.rewrites` for all BasicSymbolic types including
Const values. The old code had a guard `(issym(O) || iscall(O))` in
`substitute_name` that prevented constants from being rewritten.
This caused a bug where hash-consed constant values (e.g., Const(0))
that appeared both in an expression body AND in function argument
arrays (via DestructuredArgs with create_bindings=false) would be
incorrectly aliased. The constant 0 in the expression would be
replaced with a reference to the argument position (e.g., arg2[12]),
producing wrong results at runtime when that argument had non-zero
values.
This manifested as incorrect jacobian computations in
DifferentiationInterface.jl when using Symbolics.jl's build_function
with cache contexts (where cache variables are erased to zero before
building the function).
Fixes JuliaSymbolics/Symbolics.jl#1811
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Contributor
Benchmark Results (Julia vlts)Time benchmarks
Memory benchmarks
|
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
AayushSabharwal
approved these changes
Feb 28, 2026
c370e77
into
JuliaSymbolics:master
14 of 21 checks passed
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.
Summary
toexpr(O::BasicSymbolic{T}, st)incorrectly rewritesConstvalues throughst.rewrites, causing hash-consed constants to be aliased with function argument references(issym(O) || iscall(O))insubstitute_namethat prevented constants from being rewritten; the v4.18.4 refactor dropped this guardRoot Cause
In v4.18.4, PR #862 refactored
toexprfor performance. The newtoexpr(O::BasicSymbolic{T}, st)checkshaskey(st.rewrites, O)for allBasicSymbolictypes, includingConst. The old code routed throughsubstitute_namewhich had a guard:When
build_functionis called with argument arrays containing constant values (e.g., zeros from erased cache variables in DifferentiationInterface.jl),handle_let_pair!adds these constants tost.rewrites:Due to hash consing, all
Const(0)values are the same object. So whentoexprencounters a literal0in the expression body (e.g., off-diagonal entries of a sparse Jacobian), it finds the rewrite and replaces it withˍ₋arg2[12]. At runtime,arg2contains actual (non-zero) data, producing incorrect results.Impact
This broke DifferentiationInterface.jl's Symbolics backend when computing Jacobians with cache contexts:
Test plan
test/code.jlthat reproduces the exact scenario🤖 Generated with Claude Code