Skip to content

Commit 74e34ec

Browse files
vtjnashLilithHafner
authored andcommitted
fix ranges PR conflict (JuliaLang#43790)
We cannot safely use reverse, so we do not anymore. That is now causing conflict between JuliaLang#43059 and JuliaLang#29842. And this is likely clearer anyways. Closes JuliaLang#43788
1 parent 3815ce2 commit 74e34ec

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

base/range.jl

+10-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ range_length(len::Integer) = OneTo(len)
167167
range_stop(stop) = range_start_stop(oftype(stop, 1), stop)
168168
range_stop(stop::Integer) = range_length(stop)
169169

170-
range_step_stop_length(step, stop, length) = reverse(range_start_step_length(stop, -step, length))
170+
function range_step_stop_length(step, a, len::Integer)
171+
start = a - step * (len - oneunit(len))
172+
if start isa Signed
173+
# overflow in recomputing length from stop is okay
174+
return StepRange{typeof(start),typeof(step)}(start, step, convert(typeof(start), a))
175+
end
176+
return StepRangeLen{typeof(start),typeof(start),typeof(step)}(start, step, len)
177+
end
171178

172179
# Stop and length as the only argument
173180
function range_stop_length(a, len::Integer)
@@ -177,7 +184,7 @@ function range_stop_length(a, len::Integer)
177184
# overflow in recomputing length from stop is okay
178185
return UnitRange(start, oftype(start, a))
179186
end
180-
return range_step_stop_length(oftype(a - a, 1), a, len)
187+
return StepRangeLen{typeof(start),typeof(start),typeof(step)}(start, step, len)
181188
end
182189

183190
# Start and length as the only argument
@@ -188,7 +195,7 @@ function range_start_length(a, len::Integer)
188195
# overflow in recomputing length from stop is okay
189196
return UnitRange(oftype(stop, a), stop)
190197
end
191-
return range_start_step_length(a, oftype(a - a, 1), len)
198+
return StepRangeLen{typeof(stop),typeof(a),typeof(step)}(a, step, len)
192199
end
193200

194201
range_start_stop(start, stop) = start:stop

test/ranges.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ using Base.Checked: checked_length
3535
@test r == range(; stop=Float64(stop))
3636
end
3737

38-
for T = (Int8, Rational{Int16}, UInt32, Float64, Char)
38+
for T = (Int8, UInt32, Float64, Char)
3939
@test typeof(range(start=T(5), length=3)) === typeof(range(stop=T(5), length=3))
40+
@test typeof(range(start=T(5), length=Int8(3))) === typeof(range(stop=T(5), length=Int8(3)))
4041
end
42+
let T = Rational{Int16}
43+
@test typeof(range(start=T(5), length=Int16(3))) === typeof(range(stop=T(5), length=Int16(3)))
44+
end
45+
4146

4247
@test first(10:3) === 10
4348
@test last(10:3) === 9

0 commit comments

Comments
 (0)