diff --git a/base/show.jl b/base/show.jl index fae2675fb4b2a..fd10aa07556dd 100644 --- a/base/show.jl +++ b/base/show.jl @@ -993,10 +993,17 @@ function show_datatype(io::IO, @nospecialize(x::DataType), wheres::Vector=TypeVa # Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T} if istuple if n > 3 && all(@nospecialize(i) -> (parameters[1] === i), parameters) - print(io, "NTuple{", n, ", ", parameters[1], "}") + print(io, "NTuple{", n, ", ") + show(io, parameters[1]) + print(io, "}") else print(io, "Tuple{") - join(io, parameters, ", ") + # join(io, params, ", ") params but `show` it + first = true + for param in parameters + first ? (first = false) : print(io, ", ") + show(io, param) + end print(io, "}") end else diff --git a/test/show.jl b/test/show.jl index f6beef9bd019c..b5056952de532 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1347,6 +1347,20 @@ test_repr("(:).a") @test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4, Float64}" @test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32, Float32, Float32}" +@testset "issue #42931" begin + @test repr(NTuple{4, :A}) == "NTuple{4, :A}" + @test repr(NTuple{3, :A}) == "Tuple{:A, :A, :A}" + @test repr(NTuple{2, :A}) == "Tuple{:A, :A}" + @test repr(NTuple{1, :A}) == "Tuple{:A}" + @test repr(NTuple{0, :A}) == "Tuple{}" + + @test repr(Tuple{:A, :A, :A, :B}) == "Tuple{:A, :A, :A, :B}" + @test repr(Tuple{:A, :A, :A, :A}) == "NTuple{4, :A}" + @test repr(Tuple{:A, :A, :A}) == "Tuple{:A, :A, :A}" + @test repr(Tuple{:A}) == "Tuple{:A}" + @test repr(Tuple{}) == "Tuple{}" +end + # Test that REPL/mime display of invalid UTF-8 data doesn't throw an exception: @test isa(repr("text/plain", String(UInt8[0x00:0xff;])), String)