From db0b126d160a5cdeeefb80512ff7f506c698200d Mon Sep 17 00:00:00 2001 From: Samuel Badr Date: Fri, 5 May 2023 02:16:10 +0200 Subject: [PATCH] Allow showing of StepRangLen{T} with generic T (#49516) (cherry picked from commit ee958432e043d82975ddc02fe670f56d49b25c57) --- base/range.jl | 2 +- test/ranges.jl | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/base/range.jl b/base/range.jl index 9d12ae1001784f..a2803c2cf6a560 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1093,7 +1093,7 @@ show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ' show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r))) show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")") function show(io::IO, r::StepRangeLen) - if step(r) != 0 + if !iszero(step(r)) print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r))) else # ugly temporary printing, to avoid 0:0:0 etc. diff --git a/test/ranges.jl b/test/ranges.jl index c4cd4664f5f957..0439a626dcac5f 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -2392,3 +2392,16 @@ end @test test_firstindex(StepRange{Union{Int64,Int128},Int}(Int64(1), 1, Int128(1))) @test test_firstindex(StepRange{Union{Int64,Int128},Int}(Int64(1), 1, Int128(0))) end + +@testset "PR 49516" begin + struct PR49516 <: Signed + n::Int + end + PR49516(f::PR49516) = f + Base.:*(x::Integer, f::PR49516) = PR49516(*(x, f.n)) + Base.:+(f1::PR49516, f2::PR49516) = PR49516(+(f1.n, f2.n)) + Base.show(io::IO, f::PR49516) = print(io, "PR49516(", f.n, ")") + + srl = StepRangeLen(PR49516(1), PR49516(2), 10) + @test sprint(show, srl) == "PR49516(1):PR49516(2):PR49516(19)" +end