Open jishnub opened 1 year ago
We can fix this with
diff --git a/base/range.jl b/base/range.jl
index f0bcc0dd20..e58f1d8299 100644
--- a/base/range.jl
+++ b/base/range.jl
@@ -894,7 +894,7 @@ iterate(r::OrdinalRange) = isempty(r) ? nothing : (first(r), first(r))
function iterate(r::OrdinalRange{T}, i) where {T}
@inline
- i == last(r) && return nothing
+ (step(r) < zero(step(r)) ? i > last(r) : i < last(r)) || return nothing
next = convert(T, i + step(r))
(next, next)
end
But this begs the question: which operations are required on a number to support ranges?
EDIT to replace <=
with <
It's a bit odd to implement <
and <=
, instead of <
and ==
, since <=
is defined to fall back on those. <
should have some mention of ==
in its docstring though.
EDIT: disregard the below - the segfault happens when it tries to write the result, which is then undersized :facepalm:
I wonder why exactly this segfaults in the first place :thinking: The stacktrace suggests this is due to the +
, not ==
:
BigInt at ./gmp.jl:63 [inlined]
add at ./gmp.jl:166
+ at ./gmp.jl:490 [inlined]
+ at ./REPL[1]:15 [inlined]
iterate at ./range.jl:892 [inlined]
vcat at ./range.jl:1362
collect at ./range.jl:1367 [inlined]
|> at ./operators.jl:907
unknown function (ip: 0x7fcd365063f2)
EDIT: Ah, ==
is false, so shouldn't this lead to a stackoverflow, rather than a segfault?
Oops, there was no need for me to use <=
above.
It's a bit odd to implement < and <=, instead of < and ==, since <= is defined to fall back on those.
Fair enough, the <=
definition is redundant in this example
This seems to be a duplicate of https://github.com/JuliaLang/julia/issues/42483#issuecomment-932949350, albeit for integer ranges rather than floating-point ones. I'm leaving this open for now, as this is more about equality being required for ranges, rather than being able to compare the values exactly. In this case, the comparison is possible
This happens because of an
@inbounds
annotation in the collect. Consider the following code:In this, the user hasn't defined
Base.:(==)(a::MyInt, b::MyInt)
yet, presumably becauseBut if they try collecting a
BigInt
StepRange
, it'll lead to a segfault:This seems to happen because of the inbounds annotation in
vcat
:https://github.com/JuliaLang/julia/blob/f407a4cac3d1c660d1f8f1a9b367eec108d98178/base/range.jl#L1358-L1370
and because
Ideally, the user should receive a helpful error message (although I'm not certain what that should be in this case)
Edit: as discussed below, I've removed the
<=
definition, as this wasn't necessary to reproduce the issue.