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

Allow the conversion of AbstractUnitRanges to OrdinalRanges #40038

Merged
merged 5 commits into from
Feb 22, 2022

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Mar 15, 2021

Fixes #40035

Now

julia> A = OffsetArray(big(1):big(2), 0);

julia> CartesianIndices(A)
2-element CartesianIndices{1, Tuple{Main.OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}} with indices 1:2:
 CartesianIndex(1,)
 CartesianIndex(2,)

@johnnychen94
Copy link
Member

Thanks for the fix.
#37829 becomes notorious now 😱

@jishnub
Copy link
Contributor Author

jishnub commented Mar 15, 2021

This is perhaps not the right fix. The conversion of an AbstractUnitRange to an OrdinalRange is where the fix should go.

I'm not sure if the constructors

julia/base/range.jl

Lines 1133 to 1135 in b20de6a

OrdinalRange{T1, T2}(r::AbstractUnitRange{T1}) where {T1, T2<:Integer} = r
OrdinalRange{T1, T2}(r::UnitRange) where {T1, T2<:Integer} = UnitRange{T1}(r)
OrdinalRange{T1, T2}(r::OneTo) where {T1, T2<:Integer} = OneTo{T1}(r)

are the best ones? Eg. I'm not sure if

julia> OrdinalRange{Int,BigInt}(1:2) |> step |> typeof
Int64

makes sense, as the step is not a BigInt. Perhaps we should only have the constructor OrdinalRange{T,T}(r::AbstractUnitRange) where T = AbstractUnitRange{T}(r). I wonder if this will introduce ambiguities?

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a fundamental method like this might have unknown side effects. I don't know if it's suitable to backport this to 1.6 RC branch, but for this specific issue(#40035), we can get it fixed on the OffsetArray side anyway with an appropriate compat condition.

Edit:

This PR corrects the 1.6 newly added methods in #37829, so it makes sense to backport this to the 1.6-RC branch to minimize the breakage if we still have more rc releases.

@@ -1130,9 +1130,7 @@ AbstractUnitRange{T}(r::UnitRange) where {T} = UnitRange{T}(r)
AbstractUnitRange{T}(r::OneTo) where {T} = OneTo{T}(r)

OrdinalRange{T1, T2}(r::StepRange) where {T1, T2<: Integer} = StepRange{T1, T2}(r)
OrdinalRange{T1, T2}(r::AbstractUnitRange{T1}) where {T1, T2<:Integer} = r
OrdinalRange{T1, T2}(r::UnitRange) where {T1, T2<:Integer} = UnitRange{T1}(r)
OrdinalRange{T1, T2}(r::OneTo) where {T1, T2<:Integer} = OneTo{T1}(r)
Copy link
Member

@johnnychen94 johnnychen94 Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these methods are still needed:

julia> OrdinalRange{Int, Int8}(1:10)
1:10 # master
MethodError # current PR

We might need more tests to catch this.

Copy link
Contributor Author

@jishnub jishnub Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should this produce? I don't think that the behavior on master is correct.

julia> OrdinalRange{Int, Int8}(1:10)
1:10

julia> OrdinalRange{Int, Int8}(1:10) |> step |> typeof
Int64

However the docstring of OrdinalRange states that OrdinalRange{T, S} has spacings of type S. The present behavior on master is not consistent with the docstring.

Since

julia> supertype(AbstractUnitRange)
OrdinalRange{T, T} where T

that is perhaps the conversion that makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. Given that the previous 4 methods are newly added in Julia 1.6 only(#37829), I believe it's totally fine to revert that with OrdinalRange{T, T}(r::AbstractUnitRange) method.

@johnnychen94 johnnychen94 added the backport 1.6 Change should be backported to release-1.6 label Mar 15, 2021
@jishnub jishnub changed the title Add a specialized CartesianIndices constructor for a tuple of AbstractUnitRanges Allow the conversion of AbstractUnitRanges to OrdinalRanges Mar 16, 2021
@KristofferC KristofferC mentioned this pull request Mar 23, 2021
10 tasks
@johnnychen94 johnnychen94 requested review from mbauman and timholy March 23, 2021 08:59
@KristofferC KristofferC mentioned this pull request Mar 26, 2021
33 tasks
@jishnub
Copy link
Contributor Author

jishnub commented Apr 28, 2021

Gentle bump, would be nice to have this fixed

@KristofferC KristofferC mentioned this pull request May 4, 2021
45 tasks
@jishnub jishnub force-pushed the CartesianIndicesAUR branch from bd1afa9 to 04d25e5 Compare July 4, 2021 07:12
@KristofferC KristofferC mentioned this pull request Jul 12, 2021
75 tasks
@jishnub jishnub force-pushed the CartesianIndicesAUR branch from 04d25e5 to 61c1928 Compare July 23, 2021 08:20
base/range.jl Outdated Show resolved Hide resolved
Co-authored-by: Jameson Nash <[email protected]>
base/range.jl Outdated Show resolved Hide resolved
@vtjnash vtjnash merged commit afd9926 into JuliaLang:master Feb 22, 2022
staticfloat pushed a commit to JuliaCI/julia-buildkite-testing that referenced this pull request Mar 2, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.6 Change should be backported to release-1.6
Projects
None yet
3 participants