SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

Broadcasting causes `StackOverflowError` in combination with Tuple

Open abraemer opened this issue 1 year ago • 0 comments

By chance I found a strange corner case with regard to broadcasting: MWE:

using SparseArrays
v1 = spzeros(5); v2 = spzeros(5);
v1 .= .*(v1, (1,))
v1 .= .*(v2, (1,)) # works
v1 .= .*(v1, v2, Ref(1)) # works
v1 .= .*(v1, v2, (1,)) # ERROR: StackOverflowError:
v1 .= .*(v1, v2, (1,2,3,4,5)) # ERROR: StackOverflowError:

Note that the specific function, here *, is unimportant. This issue occurred with a package originally. Somehow if the function has more than 2 arguments and one of them is a tuple, an infinite recursion happens. Interestingly, a Ref works in-place of the single-element tuple.

Stacktrace:

ERROR: StackOverflowError:
Stacktrace:
      [1] materialize!(dest::SparseVector{…}, bc::Base.Broadcast.Broadcasted{…})
        @ Base.Broadcast .\broadcast.jl:910
      [2] broadcast!
        @ .\broadcast.jl:880 [inlined]
      [3] copyto!
        @ C:\Users\bradr\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\SparseArrays\src\higherorderfns.jl:1175 [inlined]
      [4] materialize!
        @ .\broadcast.jl:914 [inlined]
      [5] materialize!(dest::SparseVector{…}, bc::Base.Broadcast.Broadcasted{…})
        @ Base.Broadcast .\broadcast.jl:911
--- the last 4 lines are repeated 29019 more times ---
 [116082] broadcast!
        @ .\broadcast.jl:880 [inlined]
 [116083] copyto!
        @ C:\Users\bradr\.julia\juliaup\julia-1.10.3+0.x64.w64.mingw32\share\julia\stdlib\v1.10\SparseArrays\src\higherorderfns.jl:1175 [inlined]
 [116084] materialize!
        @ .\broadcast.jl:914 [inlined]
Some type information was truncated. Use `show(err)` to see complete types.

I observed this on 1.11.1, 1.10.5, 1.9 and 1.6.

ref https://github.com/Jutho/VectorInterface.jl/issues/19

abraemer avatar Oct 21 '24 18:10 abraemer