Skip to content

Commit bb3f280

Browse files
authored
fix show of self referential arrays (#58863)
see the tests for some MWE this fixes closes #47280
1 parent 1ee2f8a commit bb3f280

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

base/arrayshow.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,12 @@ function show(io::IO, X::AbstractArray)
488488
if !implicit
489489
io = IOContext(io, :typeinfo => eltype(X))
490490
end
491-
isempty(X) ?
492-
_show_empty(io, X) :
493-
_show_nonempty(io, X, prefix)
491+
if isempty(X)
492+
return _show_empty(io, X)
493+
end
494+
show_circular(io, X) && return
495+
recur_io = IOContext(io, :SHOWN_SET => X)
496+
_show_nonempty(recur_io, X, prefix)
494497
end
495498

496499
### 0-dimensional arrays (#31481)

test/abstractarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,3 +2316,21 @@ end
23162316
v2 = view(A, Base.IdentityUnitRange(1:length(A)))
23172317
@test sum(x for x in v2) == sum(A)
23182318
end
2319+
2320+
@testset "self referential" begin
2321+
v = Any[1,2,3]
2322+
v[1] = v
2323+
io = IOBuffer()
2324+
show(io, v)
2325+
@test String(take!(io)) == "Any[Any[#= circular reference @-1 =#], 2, 3]"
2326+
2327+
m1 = Any[1 2; 3 4]
2328+
m1[1] = m1
2329+
show(io, m1)
2330+
@test String(take!(io)) == "Any[#= circular reference @-1 =# 2; 3 4]"
2331+
2332+
m2 = Any[1; 2;; 3; 4;;; 5; 6;; 7; 8]
2333+
m2[1] = m2
2334+
show(io, m2)
2335+
@test String(take!(io)) == "Any[#= circular reference @-1 =# 3; 2 4;;; 5 7; 6 8]"
2336+
end

0 commit comments

Comments
 (0)