Repository navigation
Preserve concrete type of .original in solution_new_original_retcode - #1332
Merged
ChrisRackauckas merged 1 commit intoApr 24, 2026
Merged
ChrisRackauckas merged 1 commit into
ChrisRackauckas merged 1 commit into
Conversation
`solution_new_original_retcode` previously rebuilt the `ODESolution` via
three sequential `@reset` / `@set` calls. Each expands into
`ConstructionBase.setproperties(sol, patch)`, which funnels through
`merge(getproperties(sol), patch)` and then re-calls `ODESolution{T,N}(...)`.
That whole call graph exceeds Julia's inference limits for non-trivial
callers (e.g. `BoundaryValueDiffEqCore.__build_solution`, which annotates
its argument as the bare `::NonlinearSolution` UnionAll). Inference gives
up and widens the rebuilt solution's return type all the way to the bare
`ODESolution` UnionAll, breaking `@inferred solve(bvp, MIRK6(...))` in
BoundaryValueDiffEq.jl (issue SciML#479).
Replace the `@reset` chain with a single direct inner-constructor call
that swaps only the three fields being updated. This keeps the rebuild
fully inferrable: the concrete `typeof(original)` / `typeof(resid)` flow
straight into the new type parameters, and the other parameters are
carried over unchanged from `sol`.
Adds a regression test in test/solution_interface.jl that reproduces the
widening with a bare `::NonlinearSolution` caller annotation.
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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
solution_new_original_retcode(src/solutions/ode_solutions.jl:679) previously rebuilt theODESolutionthrough three sequential@reset/@setcalls:Each of those expands into
ConstructionBase.setproperties(sol, patch), which funnels throughmerge(getproperties(sol), patch)and re-entersODESolution{T, N}(...). The resulting call graph exceeds Julia's inference limits for non-trivial callers, causing inference to widen the rebuilt solution's return type all the way to the bareODESolutionUnionAll.Replacing the
@resetchain with a single direct inner-constructor call that swaps only the three fields being updated restores full inference.Root cause
File:
src/solutions/ode_solutions.jl:679(functionsolution_new_original_retcode).The Accessors lowering
@reset sol.original = x->setproperties(sol, (; original = x))hitsConstructionBase.setproperties(::ODESolution, ::NamedTuple)at line 136, which doesmerge(getproperties(sol), patch)followed by anotherODESolution{T, N}(...)call. When three of these are chained, inference walks past its abstract-interpretation budget and the result widens.The widening only manifests when the caller's argument types are not fully concrete to inference at the call site --- for example
BoundaryValueDiffEqCore.__build_solution(prob, odesol, nlsol::SciMLBase.NonlinearSolution), whose bare::NonlinearSolutionannotation is what@inferred solve(bvp, MIRK6(...); dt = 0.2)feeds into. See SciML/BoundaryValueDiffEq.jl#479.Minimal reproducer (SciMLBase-only)
Before (on master):
ODESolution(bare UnionAll --- full widening).After (this PR):
And with a concrete
typeof(nlsol)at the call site:fully concrete ---
@inferred bvde_like(odesol, nlsol)passes.Design choice: (b) narrow rebuild over (a) setproperties override
The task considered two fix shapes:
ConstructionBase.setproperties(::ODESolution, ::NamedTuple)to thread concrete types better.solution_new_original_retcodeto bypass the Accessors / setproperties path entirely.This PR takes (b). Rationale:
ODESolution(slice, retcode-only, tslocation-only) and risks changing behavior for other callers.ConstructionBase.setpropertiesmethod at line 136 is kept intact for other consumers.Downstream validation
The downstream BoundaryValueDiffEq.jl test environment currently cannot be resolved against SciMLBase master (RAT/OrdinaryDiffEqTsit5 register a SciMLBase v3 vs v4 incompatibility unrelated to this PR), so
@inferred solve(bvp, MIRK6(...); dt = 0.2)could not be run against this branch directly from the BVDE worktree. The SciMLBase-only MWE above isolates the exact call shape that BVDE's__build_solutionuses (bare::NonlinearSolutionannotation) and demonstrates the widening + fix.Test plan
test/solution_interface.jl(@testset "solution_new_original_retcode type inference"):@inferred solution_new_original_retcode(odesol, nlsol, ...)for a concrete caller.Base.return_types(bvde_like, (typeof(odesol), NonlinearSolution))[1] !== ODESolution(proves the non-widening under a bare annotation).@inferred bvde_like(odesol, nlsol)for a concrete nlsol under a bare annotation.Coretest group passes locally (Julia 1.11).