Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,13 +767,13 @@ let bigints = Union{Int, UInt, Int64, UInt64, Int128, UInt128}
# therefore still be valid (if the result is representable at all)
# n.b. !(s isa T)
if s isa Unsigned || -1 <= s <= 1 || s == -s
a = div(diff, s)
a = div(diff, s) % T
elseif s < 0
a = div(unsigned(-diff), -s) % typeof(diff)
a = div(unsigned(-diff), -s) % T
else
a = div(unsigned(diff), s) % typeof(diff)
a = div(unsigned(diff), s) % T
end
return Integer(a) + oneunit(a)
return a + oneunit(T)
end
function checked_length(r::OrdinalRange{T}) where T<:bigints
s = step(r)
Expand All @@ -791,7 +791,7 @@ let bigints = Union{Int, UInt, Int64, UInt64, Int128, UInt128}
else
a = div(checked_sub(start, stop), -s)
end
return checked_add(a, oneunit(a))
return checked_add(convert(T, a), oneunit(T))
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,11 @@ end
@test typeof(step(r)) === Int8
end

@testset "length(StepRange()) type stability" begin
typeof(length(StepRange(1,Int128(1),1))) == typeof(length(StepRange(1,Int128(1),0)))
typeof(checked_length(StepRange(1,Int128(1),1))) == typeof(checked_length(StepRange(1,Int128(1),0)))
end

@testset "LinRange eltype for element types that wrap integers" begin
struct RealWrapper{T <: Real} <: Real
x :: T
Expand Down