Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intersect is type-unstable for some types of ranges #32410

Closed
sostock opened this issue Jun 25, 2019 · 2 comments · Fixed by #37251
Closed

intersect is type-unstable for some types of ranges #32410

sostock opened this issue Jun 25, 2019 · 2 comments · Fixed by #37251

Comments

@sostock
Copy link
Contributor

sostock commented Jun 25, 2019

In the following examples, intersect is not type-stable:

julia> @code_warntype intersect(1:2:5, 1:5)
Body::Union{StepRange{Int64,Int64}, UnitRange{Int64}}
[...]

julia> @code_warntype intersect(1:2:5, 1//1:1:5//1)
Body::Union{StepRange{Rational{Int64},Int64}, StepRange{Int64,Int64}}
[...]

julia> @code_warntype intersect(1, big(2):big(5))
Body::Union{UnitRange{Int64}, UnitRange{BigInt}}
[...]

The type-instabilities also occur if the positions of the arguments are switched.

Edit: added another example

@c42f
Copy link
Member

c42f commented Jun 25, 2019

The problem is in intersect(r::AbstractUnitRange{<:Integer}, s::StepRange{<:Integer}) which has some branches which return unit ranges and some which return step ranges. For similar reasons the eltype appears to be uninferrable when ranges based on different integer types are mixed.

@sostock
Copy link
Contributor Author

sostock commented Jun 26, 2019

While preparing a fix for this, I came across a problem with StepRange{<:Integer}s with non-integer step (#32419), in which case intersect(r::AbstractUnitRange{<:Integer}, s::StepRange{<:Integer}) returns a wrong result (it expects the step of s to be integer). IMO, the assumption that a StepRange{<:Integer} has an integer step is reasonable, so I don’t think this needs to be fixed in intersect. I will nevertheless wait for a decision on #32419 before making a PR for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants