Skip to content

Commit 614f34d

Browse files
authored
Fix ambiguities on to_index (#132)
* Fix ambiguities on to_index Using IndexStyle caused ambiguities and it's highly unlikely any native axis will be anything other than IndexLinear.
1 parent 18fc853 commit 614f34d

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "3.1.4"
3+
version = "3.1.5"
44

55
[deps]
66
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"

src/indexing.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,20 @@ to_index(::MyIndexStyle, axis, arg) = ...
281281
```
282282
"""
283283
@propagate_inbounds to_index(axis, arg) = to_index(IndexStyle(axis), axis, arg)
284-
to_index(axis, arg::CartesianIndices{0}) = arg
284+
285285
# Colons get converted to slices by `indices`
286-
to_index(::IndexStyle, axis, ::Colon) = indices(axis)
287-
@propagate_inbounds function to_index(::IndexStyle, axis, arg::Integer)
286+
to_index(::IndexLinear, axis, arg::Colon) = indices(axis)
287+
to_index(::IndexLinear, axis, arg::CartesianIndices{0}) = arg
288+
@propagate_inbounds function to_index(::IndexLinear, axis, arg::Integer)
288289
@boundscheck checkbounds(axis, arg)
289290
return Int(arg)
290291
end
291-
@propagate_inbounds function to_index(::IndexStyle, axis, arg::AbstractArray{Bool})
292+
@propagate_inbounds function to_index(::IndexLinear, axis, arg::AbstractArray{Bool})
292293
@boundscheck checkbounds(axis, arg)
293294
return @inbounds(axis[arg])
294295
end
295296
@propagate_inbounds function to_index(
296-
::IndexStyle,
297+
::IndexLinear,
297298
axis,
298299
arg::AbstractArray{I},
299300
) where {I<:Integer}
@@ -303,7 +304,7 @@ end
303304
return arg
304305
end
305306
@propagate_inbounds function to_index(
306-
::IndexStyle,
307+
::IndexLinear,
307308
axis,
308309
arg::AbstractRange{I},
309310
) where {I<:Integer}
@@ -312,8 +313,9 @@ end
312313
end
313314
return arg
314315
end
315-
function to_index(S::IndexStyle, axis, arg::Any)
316-
throw(ArgumentError("invalid index: IndexStyle $S does not support indices of type $(typeof(arg))."))
316+
function to_index(s, axis, arg)
317+
throw(ArgumentError("invalid index: IndexStyle $s does not support indices of " *
318+
"type $(typeof(arg)) for axis of type $(typeof(axis))."))
317319
end
318320

319321
"""

test/indexing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ end
4545
@test @inferred(ArrayInterface.to_index(axis, 1:2)) === 1:2
4646
@test @inferred(ArrayInterface.to_index(axis, [1, 2])) == [1, 2]
4747
@test @inferred(ArrayInterface.to_index(axis, [true, false, false])) == [1]
48+
@test @inferred(ArrayInterface.to_index(axis, CartesianIndices(()))) === CartesianIndices(())
4849

4950
@test_throws BoundsError ArrayInterface.to_index(axis, 4)
5051
@test_throws BoundsError ArrayInterface.to_index(axis, 1:4)
5152
@test_throws BoundsError ArrayInterface.to_index(axis, [1, 2, 5])
5253
@test_throws BoundsError ArrayInterface.to_index(axis, [true, false, false, true])
54+
@test_throws ArgumentError ArrayInterface.to_index(axis, error)
5355
end
5456

5557
@testset "unsafe_reconstruct" begin

0 commit comments

Comments
 (0)