Skip to content

Commit 156420b

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

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

base/abstractarray.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,10 @@ _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+
sub2ind{T<:Integer}(inds::Indices{1}, I1::AbstractVector{T}, I::AbstractVector{T}...) = throw(ArgumentError("Linear indexing is not defined for one-dimensional arrays"))
1613+
sub2ind{T<:Integer}(inds::Tuple{OneTo}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
1614+
sub2ind{T<:Integer}(inds::Union{DimsInteger,Indices}, I1::AbstractVector{T}, I::AbstractVector{T}...) = _sub2ind_vecs(inds, I1, I...)
1615+
function _sub2ind_vecs(inds, I::AbstractVector...)
16131616
I1 = I[1]
16141617
Iinds = indices1(I1)
16151618
for j = 2:length(I)
@@ -1633,7 +1636,7 @@ sub2ind_vec(inds, i, I) = (@_inline_meta; _sub2ind_vec(inds, (), i, I...))
16331636
_sub2ind_vec(inds, out, i, I1, I...) = (@_inline_meta; _sub2ind_vec(inds, (out..., I1[i]), i, I...))
16341637
_sub2ind_vec(inds, out, i) = (@_inline_meta; sub2ind(inds, out...))
16351638

1636-
function ind2sub{N,T<:Integer}(inds::Union{Dims{N},Indices{N}}, ind::AbstractVector{T})
1639+
function ind2sub{N,T<:Integer}(inds::Union{DimsInteger{N},Indices{N}}, ind::AbstractVector{T})
16371640
M = length(ind)
16381641
t = ntuple(n->similar(ind),Val{N})
16391642
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)