Skip to content

Commit 49f6f7c

Browse files
committed
narrow array conversions. fixes #26294, fixes #26178.
Not all array types can convert from any AbstractArray via a 1-argument constructor call.
1 parent 4e67f87 commit 49f6f7c

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

base/abstractarray.jl

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Supertype for `N`-dimensional arrays (or array-like types) with elements of type
1212
AbstractArray
1313

1414
convert(::Type{T}, a::T) where {T<:AbstractArray} = a
15-
convert(::Type{T}, a::AbstractArray) where {T<:AbstractArray} = T(a)
1615

1716
if nameof(@__MODULE__) === :Base # avoid method overwrite
1817
# catch undefined constructors before the deprecation kicks in

base/array.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,11 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)
413413

414414
## Conversions ##
415415

416-
# arises in similar(dest, Pair{Union{},Union{}}) where dest::Dict:
417-
convert(::Type{Vector{Union{}}}, a::Vector{Union{}}) = a
416+
convert(::Type{T}, a::T) where {T<:Array} = a
417+
convert(::Type{T}, a::AbstractArray) where {T<:Array} = T(a)
418+
419+
convert(::Type{AbstractArray{T}}, a::AbstractArray) where {T} = AbstractArray{T}(a)
420+
convert(::Type{AbstractArray{T,N}}, a::AbstractArray{<:Any,N}) where {T,N} = AbstractArray{T,N}(a)
418421

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

base/bitarray.jl

+3
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ julia> BitArray(x+y == 3 for x = 1:2 for y = 1:3)
532532
"""
533533
BitArray(itr) = gen_bitarray(IteratorSize(itr), itr)
534534

535+
convert(::Type{BitArray}, a::AbstractArray) = BitArray(a)
536+
convert(::Type{BitArray{N}}, a::AbstractArray{<:Any,N}) where {N} = BitArray{N}(a)
537+
535538
# generic constructor from an iterable without compile-time info
536539
# (we pass start(itr) explicitly to avoid a type-instability with filters)
537540
gen_bitarray(isz::IteratorSize, itr) = gen_bitarray_from_itr(itr, start(itr))

base/range.jl

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ abstract type AbstractRange{T} <: AbstractArray{T,1} end
9797
RangeStepStyle(::Type{<:AbstractRange}) = RangeStepIrregular()
9898
RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular()
9999

100+
convert(::Type{T}, r::T) where {T<:AbstractRange} = r
101+
convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = T(r)
102+
100103
## ordinal ranges
101104

102105
abstract type OrdinalRange{T,S} <: AbstractRange{T} end

stdlib/LinearAlgebra/test/diagonal.jl

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ srand(1)
2727
@test Diagonal{elty}(x)::Diagonal{elty,typeof(x)} == DM
2828
@test Diagonal{elty}(x).diag === x
2929
end
30+
# issue #26178
31+
@test_throws MethodError convert(Diagonal, [1, 2, 3, 4])
3032
end
3133

3234
@testset "Basic properties" begin

0 commit comments

Comments
 (0)