Skip to content

Commit eeaed6b

Browse files
Fixes
1 parent f6a89b7 commit eeaed6b

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

base/show.jl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ function make_typealiases(@nospecialize(x::Type))
903903
end
904904
end
905905

906-
function show_unionaliases(io::IO, x::Union)
906+
function show_unionaliases(io::IO, x::Union, depth::Integer)
907907
properx = makeproper(io, x)
908908
aliases, applied = make_typealiases(properx)
909909
isempty(aliases) && return false
@@ -985,7 +985,7 @@ function _show_type(io::IO, @nospecialize(x::Type), depth::Int)
985985
show_datatype(io, x, TypeVar[], depth)
986986
return
987987
elseif x isa Union
988-
if get(io, :compact, true)::Bool && show_unionaliases(io, x)
988+
if get(io, :compact, true)::Bool && show_unionaliases(io, x, depth)
989989
return
990990
end
991991
print(io, "Union")
@@ -2771,23 +2771,26 @@ function type_depth_limit(str::String, n::Int; maxdepth = nothing)
27712771
return String(take!(output))
27722772
end
27732773

2774-
function print_type_bicolor(io, @nospecialize(type); color=:normal, inner_color=:light_black, use_color::Bool=true, depth::Int)
2775-
if depth > max_type_depth(io)
2774+
function print_type_bicolor(io, type; depth::Int = 0, kwargs...)
2775+
depth > max_type_depth(io) && return nothing
2776+
str = sprint(show, type, context=io)
2777+
print_type_bicolor(io, str; depth, kwargs...)
2778+
end
2779+
2780+
function print_type_bicolor(io, str::String; color=:normal, inner_color=:light_black, use_color::Bool=true, depth::Int = 0)
2781+
depth > max_type_depth(io) && return nothing
2782+
i = findfirst('{', str)
2783+
if !use_color # fix #41928
2784+
print(io, str)
2785+
elseif i === nothing
2786+
printstyled(io, str; color=color)
27762787
else
2777-
str = sprint(show, type, context=io)
2778-
i = findfirst('{', str)
2779-
if !use_color # fix #41928
2780-
print(io, str)
2781-
elseif i === nothing
2782-
printstyled(io, str; color=color)
2788+
printstyled(io, str[1:prevind(str,i)]; color=color)
2789+
if endswith(str, "...")
2790+
printstyled(io, str[i:prevind(str,end,3)]; color=inner_color)
2791+
printstyled(io, "..."; color=color)
27832792
else
2784-
printstyled(io, str[1:prevind(str,i)]; color=color)
2785-
if endswith(str, "...")
2786-
printstyled(io, str[i:prevind(str,end,3)]; color=inner_color)
2787-
printstyled(io, "..."; color=color)
2788-
else
2789-
printstyled(io, str[i:end]; color=inner_color)
2790-
end
2793+
printstyled(io, str[i:end]; color=inner_color)
27912794
end
27922795
end
27932796
end
@@ -2818,7 +2821,7 @@ function ismodulecall(ex::Expr)
28182821
isa(resolvebinding(ex.args[2]), Module)
28192822
end
28202823

2821-
function show(io::IO, tv::TypeVar)
2824+
function show(io::IO, tv::TypeVar, depth::Int = 0)
28222825
# If we are in the `unionall_env`, the type-variable is bound
28232826
# and the type constraints are already printed.
28242827
# We don't need to print it again.
@@ -2828,7 +2831,7 @@ function show(io::IO, tv::TypeVar)
28282831
function show_bound(io::IO, @nospecialize(b))
28292832
parens = isa(b,UnionAll) && !print_without_params(b)
28302833
parens && print(io, "(")
2831-
show(io, b)
2834+
show(io, b, depth)
28322835
parens && print(io, ")")
28332836
end
28342837
lb, ub = tv.lb, tv.ub
@@ -2852,14 +2855,14 @@ function show(io::IO, tv::TypeVar)
28522855
nothing
28532856
end
28542857

2855-
function show(io::IO, vm::Core.TypeofVararg)
2858+
function show(io::IO, vm::Core.TypeofVararg, depth::Int = 0)
28562859
print(io, "Vararg")
28572860
if isdefined(vm, :T)
28582861
print(io, "{")
2859-
show(io, vm.T)
2862+
show(io, vm.T, depth + 1)
28602863
if isdefined(vm, :N)
28612864
print(io, ", ")
2862-
show(io, vm.N)
2865+
show(io, vm.N, depth)
28632866
end
28642867
print(io, "}")
28652868
end

test/stacktraces.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ end
270270
@show sprint(Base.show, typ, context = (:max_type_depth_limit => 6))
271271
end
272272

273+
Base.@kwdef struct F55952{A,B}
274+
num::Int = 1
275+
end
276+
277+
@testset "Depth-limited type printing performance for highly nested types" begin
278+
nest_val(na, nb, ::Val{1}) = F55952{na, nb}()
279+
nest_val(na, nb, ::Val{n}) where {n} = nest_val(F55952{na, nb}, F55952{na, nb}, Val(n-1))
280+
nest_val(na, nb, n::Int) = nest_val(na, nb, Val(n))
281+
nest_val(n) = nest_val(1, 1, n)
282+
typ = typeof(nest_val(23))
283+
# be careful with changing to a larger number
284+
# ~10 seconds before #55952 is fixed:
285+
@test 1 > @elapsed sprint(Base.show, typ, context = (:max_type_depth_limit => 2))
286+
end
287+
273288
@testset "Base.StackTraces docstrings" begin
274289
@test isempty(Docs.undocumented_names(StackTraces))
275290
end

0 commit comments

Comments
 (0)