Skip to content

missing sub2ind ambiguity detection confuses inference #18307

@vtjnash

Description

@vtjnash

came across this while investigating why the inference of isassigned is bad. it seems the sub2ind definitions cause several problems for type intersection. for example:

julia> @which sub2ind((1,))
ERROR: no method found for the specified argument types
 in which(::Any, ::Any) at ./reflection.jl:466

julia> sub2ind((1,)) # supposed to throw a MethodError (ambiguity)
1

this is caused by the intersection of the following 3 methods:

sub2ind{N,T<:Integer}(inds:: Union{Base.Indices{N}, Dims{N}}, I::AbstractArray{T,1}...) at abstractarray.jl:1482
sub2ind(::Base.DimsInteger) at abstractarray.jl:1429
sub2ind(dims::Base.DimsInteger, I::Integer...) at abstractarray.jl:1433

testing with:

julia> g{N,T<:Integer}(inds::Dims{N}, I::AbstractArray{T,1}...) = 1
julia> g(::Base.DimsInteger) = 2
julia> g(dims::Base.DimsInteger, I::Integer...) = 3

with all 3, @which thinks none will be called, but actually 2 gets (incorrectly) called
with just method 1 and 2, @which thinks that 1 will definitely be called (but 2 is actually – correctly – called)
with just 1 and 3, it fails correctly
with just 2 and 3, it works correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIndicates that a maintainer wants help on an issue or pull requestneeds testsUnit tests are required for this changetypes and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions