Skip to content

Commit

Permalink
Disambiguate sub2ind
Browse files Browse the repository at this point in the history
Also adds a broken test for #18307

(cherry picked from commit e242d46)
ref #18352
  • Loading branch information
timholy authored and tkelman committed Sep 16, 2016
1 parent 1666d7c commit 9ac7312
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,12 @@ _div(ind, d::Integer) = div(ind, d), 1, d
_div(ind, r::AbstractUnitRange) = (d = unsafe_length(r); (div(ind, d), first(r), d))

# Vectorized forms
function sub2ind{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, I::AbstractVector{T}...)
function sub2ind{T<:Integer}(inds::Indices{1}, I1::AbstractVector{T}, I::AbstractVector{T}...)
throw(ArgumentError("Linear indexing is not defined for one-dimensional arrays"))
end
sub2ind{T<:Integer}(inds::Tuple{OneTo}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
sub2ind{T<:Integer}(inds::Union{DimsInteger,Indices}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
function _sub2ind_vecs(inds, I::AbstractVector...)
I1 = I[1]
Iinds = indices1(I1)
for j = 2:length(I)
Expand All @@ -1502,7 +1507,7 @@ sub2ind_vec(inds, i, I) = (@_inline_meta; _sub2ind_vec(inds, (), i, I...))
_sub2ind_vec(inds, out, i, I1, I...) = (@_inline_meta; _sub2ind_vec(inds, (out..., I1[i]), i, I...))
_sub2ind_vec(inds, out, i) = (@_inline_meta; sub2ind(inds, out...))

function ind2sub{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, ind::AbstractVector{T})
function ind2sub{N,T<:Integer}(inds::Union{DimsInteger{N},Indices{N}}, ind::AbstractVector{T})
M = length(ind)
t = ntuple(n->similar(ind),Val{N})
for (i,idx) in enumerate(ind) # FIXME: change to eachindexvalue
Expand Down
17 changes: 17 additions & 0 deletions test/ambiguous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,21 @@ end

@test isempty(detect_ambiguities(Ambig17648))

module Ambig8
using Base: DimsInteger, Indices
g18307{T<:Integer}(::Union{Indices,Dims}, I::AbstractVector{T}...) = 1
g18307(::DimsInteger) = 2
g18307(::DimsInteger, I::Integer...) = 3
end
try
# want this to be a test_throws MethodError, but currently it's not (see #18307)
Ambig8.g18307((1,))
catch err
if isa(err, MethodError)
error("Test correctly returned a MethodError, please change to @test_throws MethodError")
else
rethrow(err)
end
end

nothing

0 comments on commit 9ac7312

Please sign in to comment.