diff --git a/base/arrayshow.jl b/base/arrayshow.jl index f792d26d8e9b5..554832bdc47de 100644 --- a/base/arrayshow.jl +++ b/base/arrayshow.jl @@ -442,7 +442,9 @@ function _show_nonempty(io::IO, @nospecialize(X::AbstractMatrix), prefix::String print(io, undef_ref_str) else el = X[i,j] - show(io, el) + if !show_circular(io, el) + show(io, el) + end end end if last(cr) == last(indc) @@ -488,9 +490,11 @@ function show(io::IO, X::AbstractArray) if !implicit io = IOContext(io, :typeinfo => eltype(X)) end - isempty(X) ? - _show_empty(io, X) : - _show_nonempty(io, X, prefix) + if isempty(X) + return _show_empty(io, X) + end + recur_io = IOContext(io, :SHOWN_SET => X) + _show_nonempty(recur_io, X, prefix) end ### 0-dimensional arrays (#31481) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index ad821855e573a..f4528ad12d4b6 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -2310,3 +2310,17 @@ end v2 = view(A, Base.IdentityUnitRange(1:length(A))) @test sum(x for x in v2) == sum(A) end + +@testset "self referential" begin + v = Any[1,2,3] + m = Any[1 2 3] + + v[1] = v + m[1] = m + + io = IOBuffer() + show(io, v) + @test String(take!(io)) == "Any[Any[#= circular reference @-1 =#], 2, 3]" + show(io, m) + @test String(take!(io)) == "Any[#= circular reference @-1 =# 2 3]" +end