Skip to content

Commit

Permalink
narrow array conversions. fixes #26294, fixes #26178.
Browse files Browse the repository at this point in the history
Not all array types can convert from any AbstractArray via a
1-argument constructor call.
  • Loading branch information
JeffBezanson committed Mar 5, 2018
1 parent 4e67f87 commit 49f6f7c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
1 change: 0 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Supertype for `N`-dimensional arrays (or array-like types) with elements of type
AbstractArray

convert(::Type{T}, a::T) where {T<:AbstractArray} = a
convert(::Type{T}, a::AbstractArray) where {T<:AbstractArray} = T(a)

if nameof(@__MODULE__) === :Base # avoid method overwrite
# catch undefined constructors before the deprecation kicks in
Expand Down
7 changes: 5 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ 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{T}, a::T) where {T<:Array} = a
convert(::Type{T}, a::AbstractArray) where {T<:Array} = T(a)

convert(::Type{AbstractArray{T}}, a::AbstractArray) where {T} = AbstractArray{T}(a)
convert(::Type{AbstractArray{T,N}}, a::AbstractArray{<:Any,N}) where {T,N} = AbstractArray{T,N}(a)

promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(promote_type(T,S), a, b)

Expand Down
3 changes: 3 additions & 0 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ julia> BitArray(x+y == 3 for x = 1:2 for y = 1:3)
"""
BitArray(itr) = gen_bitarray(IteratorSize(itr), itr)

convert(::Type{BitArray}, a::AbstractArray) = BitArray(a)
convert(::Type{BitArray{N}}, a::AbstractArray{<:Any,N}) where {N} = BitArray{N}(a)

# generic constructor from an iterable without compile-time info
# (we pass start(itr) explicitly to avoid a type-instability with filters)
gen_bitarray(isz::IteratorSize, itr) = gen_bitarray_from_itr(itr, start(itr))
Expand Down
3 changes: 3 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ abstract type AbstractRange{T} <: AbstractArray{T,1} end
RangeStepStyle(::Type{<:AbstractRange}) = RangeStepIrregular()
RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular()

convert(::Type{T}, r::T) where {T<:AbstractRange} = r
convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = T(r)

## ordinal ranges

abstract type OrdinalRange{T,S} <: AbstractRange{T} end
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ srand(1)
@test Diagonal{elty}(x)::Diagonal{elty,typeof(x)} == DM
@test Diagonal{elty}(x).diag === x
end
# issue #26178
@test_throws MethodError convert(Diagonal, [1, 2, 3, 4])
end

@testset "Basic properties" begin
Expand Down

0 comments on commit 49f6f7c

Please sign in to comment.