Skip to content

Commit

Permalink
Distinguish getindex/setindex! error message
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Apr 13, 2018
1 parent d63b203 commit 8d8655d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ julia> getindex(A, 2:4)
"""
function getindex(A::AbstractArray, I...)
@_propagate_inbounds_meta
error_if_canonical_indexing(IndexStyle(A), A, I...)
error_if_canonical_getindex(IndexStyle(A), A, I...)
_getindex(IndexStyle(A), A, to_indices(A, I)...)
end
function unsafe_getindex(A::AbstractArray, I...)
Expand All @@ -963,15 +963,15 @@ function unsafe_getindex(A::AbstractArray, I...)
r
end

error_if_canonical_indexing(::IndexLinear, A::AbstractArray, ::Int) =
error("indexing not defined for ", typeof(A))
error_if_canonical_indexing(::IndexCartesian, A::AbstractArray{T,N}, ::Vararg{Int,N}) where {T,N} =
error("indexing not defined for ", typeof(A))
error_if_canonical_indexing(::IndexStyle, ::AbstractArray, ::Any...) = nothing
error_if_canonical_getindex(::IndexLinear, A::AbstractArray, ::Int) =
error("getindex not defined for ", typeof(A))
error_if_canonical_getindex(::IndexCartesian, A::AbstractArray{T,N}, ::Vararg{Int,N}) where {T,N} =
error("getindex not defined for ", typeof(A))
error_if_canonical_getindex(::IndexStyle, ::AbstractArray, ::Any...) = nothing

## Internal definitions
_getindex(::IndexStyle, A::AbstractArray, I...) =
error("indexing $(typeof(A)) with types $(typeof(I)) is not supported")
error("getindex for $(typeof(A)) with types $(typeof(I)) is not supported")

## IndexLinear Scalar indexing: canonical method is one Int
_getindex(::IndexLinear, A::AbstractArray, i::Int) = (@_propagate_inbounds_meta; getindex(A, i))
Expand Down Expand Up @@ -1031,17 +1031,24 @@ Store values from array `X` within some subset of `A` as specified by `inds`.
"""
function setindex!(A::AbstractArray, v, I...)
@_propagate_inbounds_meta
error_if_canonical_indexing(IndexStyle(A), A, I...)
error_if_canonical_setindex(IndexStyle(A), A, I...)
_setindex!(IndexStyle(A), A, v, to_indices(A, I)...)
end
function unsafe_setindex!(A::AbstractArray, v, I...)
@_inline_meta
@inbounds r = setindex!(A, v, I...)
r
end

error_if_canonical_setindex(::IndexLinear, A::AbstractArray, ::Int) =
error("setindex! not defined for ", typeof(A))
error_if_canonical_setindex(::IndexCartesian, A::AbstractArray{T,N}, ::Vararg{Int,N}) where {T,N} =
error("setindex! not defined for ", typeof(A))
error_if_canonical_setindex(::IndexStyle, ::AbstractArray, ::Any...) = nothing

## Internal defitions
_setindex!(::IndexStyle, A::AbstractArray, v, I...) =
error("indexing $(typeof(A)) with types $(typeof(I)) is not supported")
error("setindex! for $(typeof(A)) with types $(typeof(I)) is not supported")

## IndexLinear Scalar indexing
_setindex!(::IndexLinear, A::AbstractArray, v, i::Int) = (@_propagate_inbounds_meta; setindex!(A, v, i))
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ julia> x[1]
0x31
julia> x[1] = 0x32
ERROR: indexing not defined for Base.CodeUnits{UInt8,String}
ERROR: setindex! not defined for Base.CodeUnits{UInt8,String}
[...]
julia> Vector{UInt8}(x)
Expand Down

0 comments on commit 8d8655d

Please sign in to comment.