Skip to content

Commit 6ff4fc8

Browse files
Lilith HafnerLilith Hafner
authored andcommitted
also handle floating point numbers (#26847)
1 parent 1c482e8 commit 6ff4fc8

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

base/ryu/Ryu.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ end
111111
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
112112
compact = get(io, :compact, false)::Bool
113113
buf = Base.StringVector(neededdigits(T))
114-
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
114+
typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) != typeof(x)
115115
pos = writeshortest(buf, 1, x, false, false, true, -1,
116116
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
117117
write(io, resize!(buf, pos - 1))

base/show.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,14 +1140,12 @@ function show(io::IO, tn::Core.TypeName)
11401140
print(io, ")")
11411141
end
11421142

1143+
nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
1144+
show(io::IO, b::Bool) = print(io, nonnothing_nonmissing_typeinfo(io) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
11431145
show(io::IO, ::Nothing) = print(io, "nothing")
11441146
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
11451147
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
11461148
print(io::IO, n::Unsigned) = print(io, string(n))
1147-
function show(io::IO, b::Bool)
1148-
typeinfo = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
1149-
print(io, typeinfo === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
1150-
end
11511149

11521150
show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))")
11531151

test/show.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,9 @@ end
21232123
@test_repr "Union{Bool, Nothing}[1, 0, nothing]"
21242124
end
21252125

2126+
# issue #26847
2127+
@test_repr "Union{Missing, Float32}[1.0]"
2128+
21262129
# issue #30505
21272130
@test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]"
21282131

0 commit comments

Comments
 (0)