Skip to content

Commit 6325537

Browse files
committed
Check axes in Array(::AbstractArray) (fixes #36220)
1 parent 76a2e36 commit 6325537

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

base/abstractarray.jl

+5
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,11 @@ function copyto!(B::AbstractVecOrMat{R}, ir_dest::AbstractRange{Int}, jr_dest::A
940940
return B
941941
end
942942

943+
function copyto_axcheck!(dest, src)
944+
@noinline checkaxs(axd, axs) = axd == axs || throw(DimensionMismatch("axes must agree, got $axd and $axs"))
945+
@boundscheck checkaxs(axes(dest), axes(src))
946+
copyto!(dest, src)
947+
end
943948

944949
"""
945950
copymutable(a)

base/array.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(p
561561

562562
if nameof(@__MODULE__) === :Base # avoid method overwrite
563563
# constructors should make copies
564-
Array{T,N}(x::AbstractArray{S,N}) where {T,N,S} = copyto!(Array{T,N}(undef, size(x)), x)
565-
AbstractArray{T,N}(A::AbstractArray{S,N}) where {T,N,S} = copyto!(similar(A,T), A)
564+
Array{T,N}(x::AbstractArray{S,N}) where {T,N,S} = copyto_axcheck!(Array{T,N}(undef, size(x)), x)
565+
AbstractArray{T,N}(A::AbstractArray{S,N}) where {T,N,S} = copyto_axcheck!(similar(A,T), A)
566566
end
567567

568568
## copying iterators to containers

test/offsetarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ h = OffsetArray([-1,1,-2,2,0], (-3,))
2121
@test axes(v) == (-2:1,)
2222
@test size(v) == (4,)
2323
@test size(v, 1) == 4
24+
@test_throws DimensionMismatch Array(v)
2425

2526
A0 = [1 3; 2 4]
2627
A = OffsetArray(A0, (-1,2)) # IndexLinear

0 commit comments

Comments
 (0)