From f01898ca0d845ad4d08db56c6141f8395f1d576d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Fri, 5 Jan 2024 14:02:33 +0100 Subject: [PATCH] Fix printing of `InexactError` for `Inf16` arg and similar (#52491) Resolves https://github.com/JuliaLang/julia/issues/51087 Closes https://github.com/JuliaLang/julia/pull/51163 Use `show` as pointed out in https://github.com/JuliaLang/julia/pull/51163#discussion_r1328682832. --- base/errorshow.jl | 8 +++++++- test/errorshow.jl | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/base/errorshow.jl b/base/errorshow.jl index d33c5e3ca569d..3666c7daf7080 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -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 diff --git a/test/errorshow.jl b/test/errorshow.jl index 28ca2c1bc3c5c..9fd850ff3cdb2 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -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