Skip to content

Commit ba0939f

Browse files
committed
Make sure has_offset_axes(::StepRange) could be const folded.
1 parent 2e6aa41 commit ba0939f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

base/range.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ step_hp(r::AbstractRange) = step(r)
687687

688688
axes(r::AbstractRange) = (oneto(length(r)),)
689689

690+
# Needed to ensure `has_offset_axes` can constant-fold.
691+
has_offset_axes(::StepRange) = false
692+
# TODO: Extend this after we land #45236.
693+
let baseints = Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}
694+
global firstindex
695+
firstindex(::StepRange{T,T}) where {T<:baseints} = sizeof(T) < sizeof(Int) ? 1 : one(T)
696+
end
697+
690698
# n.b. checked_length for these is defined iff checked_add and checked_sub are
691699
# defined between the relevant types
692700
function checked_length(r::OrdinalRange{T}) where T

test/ranges.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,3 +2342,17 @@ end
23422342
@test isempty(range(typemax(Int), length=0, step=UInt(2)))
23432343

23442344
@test length(range(1, length=typemax(Int128))) === typemax(Int128)
2345+
2346+
@testset "firstindex(::StepRange{T,T})" begin
2347+
test_firstindex(x) = firstindex(x) === first(Base.axes1(x))
2348+
for bit in (8,16,32,64,128)
2349+
uint = Symbol(:UInt, bit)
2350+
int = Symbol(:Int, bit)
2351+
@eval UR = StepRange{$uint,$uint}
2352+
@eval IR = StepRange{$int,$int}
2353+
@test test_firstindex(UR(1,1,1))
2354+
@test test_firstindex(UR(1,1,0))
2355+
@test test_firstindex(IR(1,1,1))
2356+
@test test_firstindex(IR(1,1,0))
2357+
end
2358+
end

0 commit comments

Comments
 (0)