Skip to content

Commit

Permalink
Check axes in Array(::AbstractArray) (fixes #36220) (#36397)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jun 24, 2020
1 parent 3604a25 commit 52c55d7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,12 @@ function copyto!(B::AbstractVecOrMat{R}, ir_dest::AbstractRange{Int}, jr_dest::A
return B
end

function copyto_axcheck!(dest, src)
@noinline checkaxs(axd, axs) = axd == axs || throw(DimensionMismatch("axes must agree, got $axd and $axs"))

checkaxs(axes(dest), axes(src))
copyto!(dest, src)
end

"""
copymutable(a)
Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(p

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

## copying iterators to containers
Expand Down
8 changes: 5 additions & 3 deletions test/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ end

for a in vals
a isa AbstractArray || continue
if keys(a) == keys(Array(a))
@test hash(a) == hash(Array(a)) == hash(Array{Any}(a))
aa = copyto!(Array{eltype(a)}(undef, size(a)), a)
aaa = copyto!(Array{Any}(undef, size(a)), a)
if keys(a) == keys(aa)
@test hash(a) == hash(aa) == hash(aaa)
else
@test hash(a) == hash(OffsetArray(Array(a), (first.(axes(a)).-1)...)) == hash(OffsetArray(Array{Any}(a), (first.(axes(a)).-1)...))
@test hash(a) == hash(OffsetArray(aa, (first.(axes(a)).-1)...)) == hash(OffsetArray(aaa, (first.(axes(a)).-1)...))
end
end

Expand Down
1 change: 1 addition & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ h = OffsetArray([-1,1,-2,2,0], (-3,))
@test axes(v) == (-2:1,)
@test size(v) == (4,)
@test size(v, 1) == 4
@test_throws DimensionMismatch Array(v)

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

2 comments on commit 52c55d7

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.