From 2ab8ffcc4d906eff743aa51441709e7927427a81 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 4 Sep 2016 20:41:28 -0500 Subject: [PATCH] Disambiguate sub2ind Also adds a broken test for #18307 --- base/abstractarray.jl | 9 +++++++-- test/ambiguous.jl | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 174d60905923cc..716f9e35fe692d 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1609,7 +1609,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) @@ -1633,7 +1638,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 diff --git a/test/ambiguous.jl b/test/ambiguous.jl index 37a8135f2db88b..1267a51e5fff9c 100644 --- a/test/ambiguous.jl +++ b/test/ambiguous.jl @@ -186,4 +186,12 @@ immutable T end end @test length(detect_ambiguities(Ambig7)) == 1 +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 +@test_broken @test_throws MethodError Ambig8.g18307((1,)) + nothing