Skip to content

Commit

Permalink
Disambiguate convert & showarg methods with eltype Union{} (#22322)
Browse files Browse the repository at this point in the history
This is the type of method that we deliberately exclude from our ambiguity detection, but it can be triggered by, e.g., a grow_to! method on Dicts or trying to display `collect(tuple())`.
  • Loading branch information
timholy authored and vtjnash committed Sep 25, 2017
1 parent ee7b0b8 commit d012a5d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)

## Conversions ##

# arises in similar(dest, Pair{Union{},Union{}}) where dest::Dict:
convert(::Type{Vector{Union{}}}, a::Vector{Union{}}) = a

convert(::Type{Vector}, x::AbstractVector{T}) where {T} = convert(Vector{T}, x)
convert(::Type{Matrix}, x::AbstractMatrix{T}) where {T} = convert(Matrix{T}, x)

Expand Down
6 changes: 6 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,13 @@ function showarg(io::IO, x, toplevel)
toplevel || print(io, "::")
print(io, typeof(x))
end
# This method resolves an ambiguity for packages that specialize on eltype
function showarg(io::IO, a::Array{Union{}}, toplevel)
toplevel || print(io, "::")
print(io, typeof(a))
end

# Container specializations
function showarg(io::IO, v::SubArray, toplevel)
print(io, "view(")
showarg(io, parent(v), false)
Expand Down
7 changes: 7 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,10 @@ end
# nextind
@test nextind(zeros(4), 2) == 3
@test nextind(zeros(2,3), CartesianIndex(2,1)) == CartesianIndex(1, 2)

@testset "ImageCore #40" begin
Base.convert(::Type{Array{T,n}}, a::Array{T,n}) where {T<:Number,n} = a
Base.convert(::Type{Array{T,n}}, a::Array) where {T<:Number,n} =
copy!(Array{T,n}(size(a)), a)
@test isa(similar(Dict(:a=>1, :b=>2.0), Pair{Union{},Union{}}), Dict{Union{}, Union{}})
end

0 comments on commit d012a5d

Please sign in to comment.