Skip to content

Commit 9091c16

Browse files
committed
Refactor show_default and reuse for printing
See issue JuliaLang#36263
1 parent 45345da commit 9091c16

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

base/indices.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ unsafe_length(S::Slice) = unsafe_length(S.indices)
358358
getindex(S::Slice, i::Int) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
359359
getindex(S::Slice, i::AbstractUnitRange{<:Integer}) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
360360
getindex(S::Slice, i::StepRange{<:Integer}) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
361-
show(io::IO, r::Slice) = print(io, "Base.Slice(", r.indices, ")")
361+
show(io::IO, r::Slice) = show_default(io, r; f_type = t -> t.name)
362362
iterate(S::Slice, s...) = iterate(S.indices, s...)
363363

364364

@@ -389,7 +389,7 @@ unsafe_length(S::IdentityUnitRange) = unsafe_length(S.indices)
389389
getindex(S::IdentityUnitRange, i::Int) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
390390
getindex(S::IdentityUnitRange, i::AbstractUnitRange{<:Integer}) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
391391
getindex(S::IdentityUnitRange, i::StepRange{<:Integer}) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
392-
show(io::IO, r::IdentityUnitRange) = print(io, "Base.IdentityUnitRange(", r.indices, ")")
392+
show(io::IO, r::IdentityUnitRange) = show_default(io, r; f_type = t -> t.name)
393393
iterate(S::IdentityUnitRange, s...) = iterate(S.indices, s...)
394394

395395
"""

base/range.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ end
739739

740740
show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))
741741
show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))
742-
show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")
742+
show(io::IO, r::OneTo) = show_default(io, r; f_type = t -> t.name)
743743

744744
function ==(r::T, s::T) where {T<:AbstractRange}
745745
isempty(r) && return isempty(s)

base/show.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,18 @@ show(io::IO, @nospecialize(x)) = show_default(io, x)
388388
show(x) = show(stdout::IO, x)
389389

390390
# avoid inferring show_default on the type of `x`
391-
show_default(io::IO, @nospecialize(x)) = _show_default(io, inferencebarrier(x))
391+
show_default(io::IO, @nospecialize(x); f_type=identity) = _show_default(io, inferencebarrier(x), inferencebarrier(f_type))
392392

393-
function _show_default(io::IO, @nospecialize(x))
393+
function _show_default(io::IO, @nospecialize(x), @nospecialize(f_type))
394394
t = typeof(x)
395-
show(io, inferencebarrier(t))
395+
show(io, inferencebarrier(f_type(t)))
396396
print(io, '(')
397+
_show_default_body(io, x)
398+
print(io, ')')
399+
end
400+
401+
function _show_default_body(io::IO, @nospecialize(x))
402+
t = typeof(x)
397403
nf = nfields(x)
398404
nb = sizeof(x)
399405
if nf != 0 || nb == 0
@@ -422,7 +428,6 @@ function _show_default(io::IO, @nospecialize(x))
422428
end
423429
end
424430
end
425-
print(io,')')
426431
end
427432

428433
# Check if a particular symbol is exported from a standard library module

0 commit comments

Comments
 (0)