Skip to content

Commit

Permalink
Fix printing of InexactError for Inf16 arg and similar (#52491)
Browse files Browse the repository at this point in the history
Resolves #51087
Closes #51163

Use `show` as pointed out in
#51163 (comment).
  • Loading branch information
lgoettgens authored Jan 5, 2024
1 parent f68d7f8 commit f01898c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ function showerror(io::IO, ex::InexactError)
print(io, "InexactError: ", ex.func, '(')
T = first(ex.args)
nameof(T) === ex.func || print(io, T, ", ")
join(io, ex.args[2:end], ", ")
# `join` calls `string` on its arguments, which shadows the size of e.g. Inf16
# as `string(Inf16) == "Inf"` instead of "Inf16". Thus we cannot use `join` here.
for arg in ex.args[2:end-1]
show(io, arg)
print(io, ", ")
end
show(io, ex.args[end])
print(io, ")")
Experimental.show_error_hints(io, ex)
end
Expand Down
14 changes: 14 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1090,3 +1090,17 @@ let e = @test_throws MethodError convert(TypeCompareError{Float64,1}, TypeCompar
@test occursin("TypeCompareError{Float64,1}", str)
@test !occursin("TypeCompareError{Float64{},2}", str) # No {...} for types without params
end

@testset "InexactError for Inf16 should print '16' (#51087)" begin
@test sprint(showerror, InexactError(:UInt128, UInt128, Inf16)) == "InexactError: UInt128(Inf16)"

for IntType in [Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128]
IntStr = string(IntType)
for InfVal in Any[Inf, Inf16, Inf32, Inf64]
InfStr = repr(InfVal)
e = @test_throws InexactError IntType(InfVal)
str = sprint(Base.showerror, e.value)
@test occursin("InexactError: $IntStr($InfStr)", str)
end
end
end

0 comments on commit f01898c

Please sign in to comment.