Skip to content

Commit 2ab8ffc

Browse files
committed
Disambiguate sub2ind
Also adds a broken test for #18307
1 parent 7126d89 commit 2ab8ffc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

base/abstractarray.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,12 @@ _div(ind, d::Integer) = div(ind, d), 1, d
16091609
_div(ind, r::AbstractUnitRange) = (d = unsafe_length(r); (div(ind, d), first(r), d))
16101610

16111611
# Vectorized forms
1612-
function sub2ind{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, I::AbstractVector{T}...)
1612+
function sub2ind{T<:Integer}(inds::Indices{1}, I1::AbstractVector{T}, I::AbstractVector{T}...)
1613+
throw(ArgumentError("Linear indexing is not defined for one-dimensional arrays"))
1614+
end
1615+
sub2ind{T<:Integer}(inds::Tuple{OneTo}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
1616+
sub2ind{T<:Integer}(inds::Union{DimsInteger,Indices}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
1617+
function _sub2ind_vecs(inds, I::AbstractVector...)
16131618
I1 = I[1]
16141619
Iinds = indices1(I1)
16151620
for j = 2:length(I)
@@ -1633,7 +1638,7 @@ sub2ind_vec(inds, i, I) = (@_inline_meta; _sub2ind_vec(inds, (), i, I...))
16331638
_sub2ind_vec(inds, out, i, I1, I...) = (@_inline_meta; _sub2ind_vec(inds, (out..., I1[i]), i, I...))
16341639
_sub2ind_vec(inds, out, i) = (@_inline_meta; sub2ind(inds, out...))
16351640

1636-
function ind2sub{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, ind::AbstractVector{T})
1641+
function ind2sub{N,T<:Integer}(inds::Union{DimsInteger{N},Indices{N}}, ind::AbstractVector{T})
16371642
M = length(ind)
16381643
t = ntuple(n->similar(ind),Val{N})
16391644
for (i,idx) in enumerate(ind) # FIXME: change to eachindexvalue

test/ambiguous.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,12 @@ immutable T end
186186
end
187187
@test length(detect_ambiguities(Ambig7)) == 1
188188

189+
module Ambig8
190+
using Base: DimsInteger, Indices
191+
g18307{T<:Integer}(::Union{Indices,Dims}, I::AbstractVector{T}...) = 1
192+
g18307(::DimsInteger) = 2
193+
g18307(::DimsInteger, I::Integer...) = 3
194+
end
195+
@test_broken @test_throws MethodError Ambig8.g18307((1,))
196+
189197
nothing

0 commit comments

Comments
 (0)