Skip to content

make types safe to print when verbose stacktraces are on - #419

Closed
KristofferC wants to merge 4 commits into
SciML:masterfrom
KristofferC:kc/invoke_types
Closed

KristofferC wants to merge 4 commits into
SciML:masterfrom
KristofferC:kc/invoke_types

Conversation

@KristofferC

@KristofferC KristofferC commented Mar 15, 2023 •

Copy link
Copy Markdown

The first commit invokes the show method for general Types in order for it to be safe. So at least people that hit SciML/TruncatedStacktraces.jl#18 can now disable it and get working code:

julia> using SnoopCompileCore; invalidations = @snoopr(using SciMLBase); using SnoopCompile


julia> trees = invalidation_trees(invalidations)
3-element Vector{SnoopCompile.MethodInvalidations}:
Error showing value of type Vector{SnoopCompile.MethodInvalidations}:
ERROR: UndefVarError: `T` not defined
Stacktrace:
  [1] show(io::IOContext{IOBuffer}, t::
SYSTEM (REPL): showing an error caused an error
ERROR: UndefVarError: `T` not defined
Stacktrace:
  [1] show(io::IOContext{IOBuffer}, t::
SYSTEM (REPL): caught exception of type UndefVarError while trying to handle a nested exception; giving up

julia> SciMLBase.TruncatedStacktraces.VERBOSE[] = true
true

julia> trees = invalidation_trees(invalidations)
3-element Vector{SnoopCompile.MethodInvalidations}:
 inserting print_matrix_row(io::IO, X::Union{FillArrays.AbstractFill{<:Any, 1}, FillArrays.AbstractFill{<:Any, 2}, FillArrays.RectDiagonal, LinearAlgebra.AbstractTriangular{<:Any, <:FillArrays.AbstractFill{<:Any, 2}}, LinearAlgebra.Diagonal{<:Any, <:FillArrays.AbstractFill{<:Any, 1}}}, A::Vector, i::Integer, cols::AbstractVector, sep::AbstractString, idxlast::Integer) @ FillArrays ~/.julia/packages/FillArrays/xqile/src/FillArrays.jl:688 invalidated:
   backedges: 1: superseding print_matrix_row(io::IO, X::AbstractVecOrMat, A::Vector, i::Integer, cols::AbstractVector, sep::AbstractString, idxlast::Integer) @ Base arrayshow.jl:97 with MethodInstance for Base.print_matrix_row(::IOContext{Base.TTY}, ::AbstractVecOrMat, ::Vector{Tuple{Int64, Int64}}, ::Int64, ::Vector{Int64}, ::String, ::Int64) (13 children)
   8 mt_cache

 inserting broadcasted(::Base.Broadcast.DefaultArrayStyle{N}, op, r::FillArrays.AbstractFill{T, N}) where {T, N} @ FillArrays ~/.julia/packages/FillArrays/xqile/src/fillbroadcast.jl:78 invalidated:
   mt_backedges: 1: signature broadcasted(::S, f, args...) where S<:BroadcastStyle @ Base.Broadcast broadcast.jl:1319 (formerly broadcasted(::S, f, args...) where S<:BroadcastStyle @ Base.Broadcast broadcast.jl:1319) triggered MethodInstance for Base.Broadcast.broadcasted(::typeof(esc), ::Any) (20 children)
   9 mt_cache

 inserting show(io::IO, t::Type{<:SciMLBase.IntegralSolution{T, N}}) where {T, N} @ SciMLBase ~/JuliaPkgs/SciMLBase.jl/src/solutions/basic_solutions.jl:76 invalidated:
   backedges: 1: superseding show(io::IO, x::Type) @ Base show.jl:950 with MethodInstance for show(::IOContext{IOBuffer}, ::Type) (107 children)
              2: superseding show(io::IO, x::Type) @ Base show.jl:950 with MethodInstance for show(::IOBuffer, ::Type) (3104 children)
   4 mt_cache

The second commit was made by "pattern recognition". I am not sure, but it looks like a typo that those show methods were defined on the instance of the struct and not the type. But I might be wrong.

@ChrisRackauckas

Copy link
Copy Markdown
Member

@avik-pal can we incorporate this into the macro?

@codecov

codecov Bot commented Mar 15, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #419 (4668470) into master (2bd5ea4) will decrease coverage by 0.60%.
The diff coverage is 2.12%.

❗ Current head 4668470 differs from pull request most recent head c15b4bf. Consider uploading reports for the commit c15b4bf to get more accurate results

@@            Coverage Diff             @@
##           master     #419      +/-   ##
==========================================
- Coverage   53.85%   53.26%   -0.60%     
==========================================
  Files          46       46              
  Lines        3589     3588       -1     
==========================================
- Hits         1933     1911      -22     
- Misses       1656     1677      +21     
Impacted Files Coverage Δ
src/callbacks.jl 74.46% <0.00%> (+4.25%) ⬆️
src/problems/analytical_problems.jl 0.00% <0.00%> (ø)
src/problems/basic_problems.jl 61.19% <0.00%> (ø)
src/problems/bvp_problems.jl 64.28% <0.00%> (ø)
src/problems/dae_problems.jl 76.47% <0.00%> (ø)
src/problems/dde_problems.jl 31.81% <0.00%> (ø)
src/problems/discrete_problems.jl 66.66% <0.00%> (ø)
src/problems/implicit_discrete_problems.jl 0.00% <0.00%> (ø)
src/problems/rode_problems.jl 69.23% <0.00%> (ø)
src/problems/sdde_problems.jl 0.00% <0.00%> (ø)
... and 10 more

... and 9 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@KristofferC

Copy link
Copy Markdown
Author
  • Why isn't this package using the macro?
  • Is the " I am not sure, but it looks like a typo that those show methods were defined on the instance of the struct and not the type." comment true

@KristofferC

Copy link
Copy Markdown
Author

Also, in cases like

print(io, "AnalyticalProblem{$isinplace,$uType,$tType,…}")

one should run @isdefined isinplace && @isdefined uType && @isdefined tType before running that code. Note that this is broken on 1.9 though: JuliaLang/julia#49027

@ChrisRackauckas

Copy link
Copy Markdown
Member

Thanks. Replaced by #420, where now everything uses the macro and the macro incorporates your improvements from here.

ChrisRackauckas added a commit that referenced this pull request Aug 26, 2026
…1550)

* Restore the SciMLOperators operator interface on `using SciMLBase`

#1472 dropped `@reexport using SciMLOperators` from `src/SciMLBase.jl`. Reexport
exports `filter(x -> Base.isexported(m, x), names(m; all = true, imported = true))`,
so that line had been putting SciMLOperators' 33 exported names plus the
`SciMLOperators` module binding into scope for every `using SciMLBase`. Removing it
silently deleted all 34 from SciMLBase's public surface: nothing in SciMLBase's own
tests imports them, so CI stayed green while `using SciMLBase; MatrixOperator(A)`
started failing with `UndefVarError` across the ecosystem.

SciMLBase is the root of the SciML dependency tree, so the blast radius is every
package and script that reached an operator through it. `OptimizationBase` records
the same surface from the other side: its comment notes that blanket-reexporting
SciMLBase "put 256 names into every downstream namespace -- `DynamicalSDEFunction`,
`BlockDiagonalOperator`, `AddVector` and the rest of SciMLOperators".

Restore the surface as an explicit `export` list rather than the blanket reexport,
following the Sundials.jl#553 pattern. The list is the operator interface SciMLBase's
own documentation is written in terms of: `SciMLFunctions.md` documents
`jac_prototype` as "an `AbstractSciMLOperator`" whose Jacobian update goes through
`update_coefficients!`/`update_coefficients`, and `SplitFunction`, `SplitODEProblem`
and `IncrementingODEProblem` are all specified as operator-valued. That means the
premade operator types, the operator traits, and the update/cache verbs -- 32 names.

Deliberately excluded from the 34: `WOperator` and `StaticWOperator`. They are the
stiff-solver W = M - gamma*J machinery, constructed by OrdinaryDiffEq and consumed by
LinearSolve, both of which depend on SciMLOperators directly. A `using SciMLBase` user
never builds one. `jacobian_stale`, `mark_jacobian_updated!` and
`mark_jacobian_current!` are not in the list either: SciMLOperators only started
exporting them in #419, after #1472, so they were never part of this surface.

`AbstractSciMLOperator` is a common assumption in reports about this breakage, but it
was never reexported here: SciMLOperators has never exported it, and its `@public`
declaration landed in #418, after #1472 -- and Reexport filters on `Base.isexported`
regardless. It stays reachable the documented way, qualified as
`SciMLOperators.AbstractSciMLOperator`, which is why the `SciMLOperators` binding
itself is back in the export list.

`const isconstant = SciMLOperators.isconstant` becomes an ordinary
`using SciMLOperators: isconstant`, matching what `solve!` already does with
CommonSolve. The alias made SciMLBase the binding owner, which would have failed
`run_api_docs`' docstring check once the name was exported; imported, the docstring
resolves to SciMLOperators.

QA declares the list through `reexports_allow`, and a new "Reexport surface" testset
asserts every approved name is both in `names(SciMLBase)` and actually in scope from
a bare `using SciMLBase`, evaluated in a dedicated module so the extension trigger
packages `test/qa/qa.jl` loads cannot mask a missing export. The allow-list cannot
drift from the exports in either direction.

Restoring names that used to be exported is not breaking; no version bump here.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01Rmh6B9eoW3N8oCbuuVPVxJ

* docs: render the reexported API

A restored export that is not documented is only half the fix: nothing tells the next
reader, or the next QA sweep, that the name is deliberately part of SciMLBase's surface
or which package actually owns it.

Add `docs/src/interfaces/Reexported_API.md`, wired into `docs/pages.jl`, listing every
name `using SciMLBase` provides that SciMLBase does not define. The 32 SciMLOperators
names are grouped by role -- the `SciMLOperators` module binding, operator types
(trivial / matrix-backed / scalar / matrix-free / structured and lazy), and the operator
interface and traits (updating / caching and materialization / structural traits /
supported-operation traits / lazy algebra) -- each stated as owned and documented by
SciMLOperators, with links to its Premade Operators and interface pages. The CommonSolve
verbs already exported here get the same treatment.

The page closes with the boundary: anything else from SciMLOperators must be imported
from SciMLOperators directly. Two exclusions are spelled out rather than left to look
like oversights. `AbstractSciMLOperator` and the other abstract and lazy-algebra types
are public but deliberately not exported by SciMLOperators, so they are not exported
here either and are used qualified -- which is what the reexported `SciMLOperators`
binding is for. `WOperator`, `StaticWOperator`, `jacobian_stale`,
`mark_jacobian_updated!` and `mark_jacobian_current!` are stiff-solver W-matrix
machinery for packages that depend on SciMLOperators directly.

`SciMLFunctions.md` already describes `jac_prototype` in terms of `AbstractSciMLOperator`
and `update_coefficients!`, so it now points at the new page for the exact list.

The list now lives in three places -- the page, the `export` block in
`src/SciMLBase.jl`, and `_scimloperators_reexports` in `test/qa/qa.jl`. All three are
identical, the two code copies carry cross-references to the other two, and the
"Reexport surface" testset holds the code copies to the actual export surface.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01Rmh6B9eoW3N8oCbuuVPVxJ

* compat: raise the SciMLOperators floor to 1.19

Downgrade CI resolved SciMLOperators to the declared floor, 1.3.0, where the new
`using SciMLOperators: ...` line fails at precompile time:

    ERROR: LoadError: UndefVarError: `BlockDiagonalOperator` not defined
    ERROR: LoadError: Failed to precompile SciMLBase

That is the honest consequence of the restoration: the reexported names are newer than
the floor SciMLBase carried, and until now nothing in SciMLBase referenced them, so the
floor was never tested against them. `BlockDiagonalOperator` is only the first name the
loader reaches.

Walking every registered 1.x tag of SciMLOperators and extracting its `export` blocks
gives the introduction point of each of the 32 restored names:

  - 1.0.0  -- the other 27 names
  - 1.13.0 -- `has_concretization`
  - 1.19.0 -- `BlockDiagonalOperator`, `TensorSumOperator`, `kronsum`

so 1.19.0 is the first registered version that exports all of them, and every release
from 1.19.0 through 1.28.0 (and master's 1.29.0) still does. 1.19.0 is not yanked, and
its registered git-tree-sha1 matches the `v1.19.0` tag. 1.18.0 is missing three of the
names, so this floor is minimal rather than merely sufficient.

Verified by resolving SciMLOperators to exactly 1.19.0 against this branch: SciMLBase
precompiles, `names(SciMLBase)` is 498, and all 32 names are in scope from a bare
`using SciMLBase`. Julia floors match (both 1.10), and SciMLOperators 1.19.0's own
bounds are already satisfied by SciMLBase's -- `ArrayInterface` 7.19 exactly, `Adapt`,
`DocStringExtensions` and `StaticArraysCore` above -- except `Accessors`, where
SciMLOperators 1.19.0 wants 0.1.42 and SciMLBase declares 0.1.39. That needs no change
here: the downgrade action resolves a jointly minimal version set rather than pinning
each direct dependency, so it lifts Accessors to satisfy the transitive bound, and
SciMLBase's own 0.1.39 floor remains accurate for its own use.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_01Rmh6B9eoW3N8oCbuuVPVxJ

* docs: fix the CommonSolve owner link (#1555)

AI-Agent: OpenAI Codex
Codex-Session-ID: 01a03a11-47c2-77e0-858b-167004f0b79c


Claude-Session: https://claude.ai/code/session_01Rmh6B9eoW3N8oCbuuVPVxJ

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Co-authored-by: Chris Rackauckas - Beep Boop Edition <[email protected]>
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.

2 participants