diff --git a/src/array-lib.jl b/src/array-lib.jl index 020916f0c..5c44d6cf1 100644 --- a/src/array-lib.jl +++ b/src/array-lib.jl @@ -22,12 +22,12 @@ function Base.getindex(x::SymArray, idx...) meta = metadata(unwrap(x)) if shape(x) !== Unknown() && all(i -> i isa Integer, idx) II = CartesianIndices(axes(x)) + ii = CartesianIndex(idx) @boundscheck begin - if !checkbounds(Bool, II, idx...) + if !in(ii, II) throw(BoundsError(x, idx)) end end - ii = II[idx...] res = Term{eltype(symtype(x))}(getindex, [x, Tuple(ii)...]; metadata = meta) elseif all(i -> symtype(i) <: Integer, idx) shape(x) !== Unknown() && @boundscheck begin diff --git a/src/arrays.jl b/src/arrays.jl index 3c8b7b84e..ac74050e2 100644 --- a/src/arrays.jl +++ b/src/arrays.jl @@ -526,7 +526,7 @@ end function axes(A::Union{Arr, SymArray}) s = shape(unwrap(A)) s === Unknown() && error("axes of $A not known") - return map(x->1:length(x), s) + return s end diff --git a/test/arrays.jl b/test/arrays.jl index 07157333f..24626a3b6 100644 --- a/test/arrays.jl +++ b/test/arrays.jl @@ -393,3 +393,19 @@ end @test !isequal(a, b) && !isequal(b, c) && !isequal(a, c) @test hash(a) != hash(b) && hash(b) != hash(c) && hash(a) != hash(c) end + +@testset "Offset Indices" begin + @variables k[0:3] + + @testset "i = $i" for i in 0:3 + sym = unwrap(k[i]) + @test operation(sym) === getindex + args = arguments(sym) + @test length(args) == 2 + @test args[1] === unwrap(k) + @test args[2] === i + end + + @test_throws BoundsError k[-1] + @test_throws BoundsError k[4] +end \ No newline at end of file