Skip to content

Commit a5c9e88

Browse files
authored
rename some copy! methods to copyto! (#24808)
When the deprecation gets lifted, they will be re-enabled with a new meaning.
1 parent e89a2b8 commit a5c9e88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+478
-458
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ Deprecated or removed
757757
* `copy!` is deprecated for `AbstractSet` and `AbstractDict`, with the intention to re-enable
758758
it with a cleaner meaning in a future version ([#24844]).
759759

760+
* `copy!` (resp. `unsafe_copy!`) is deprecated for `AbstractArray` and is renamed `copyto!`
761+
(resp. `unsafe_copyto!`); it will be re-introduced with a different meaning in a future
762+
version ([#24808]).
763+
760764
* `a:b` is deprecated for constructing a `StepRange` when `a` and `b` have physical units
761765
(Dates and Times). Use `a:s:b`, where `s = Dates.Day(1)` or `s = Dates.Second(1)`.
762766

base/abstractarray.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ empty(a::AbstractVector, ::Type{T}) where {T} = Vector{T}()
585585

586586
## from general iterable to any array
587587

588-
function copy!(dest::AbstractArray, src)
588+
function copyto!(dest::AbstractArray, src)
589589
destiter = eachindex(dest)
590590
state = start(destiter)
591591
for x in src
@@ -595,7 +595,7 @@ function copy!(dest::AbstractArray, src)
595595
return dest
596596
end
597597

598-
function copy!(dest::AbstractArray, dstart::Integer, src)
598+
function copyto!(dest::AbstractArray, dstart::Integer, src)
599599
i = Int(dstart)
600600
for x in src
601601
dest[i] = x
@@ -605,7 +605,7 @@ function copy!(dest::AbstractArray, dstart::Integer, src)
605605
end
606606

607607
# copy from an some iterable object into an AbstractArray
608-
function copy!(dest::AbstractArray, dstart::Integer, src, sstart::Integer)
608+
function copyto!(dest::AbstractArray, dstart::Integer, src, sstart::Integer)
609609
if (sstart < 1)
610610
throw(ArgumentError(string("source start offset (",sstart,") is < 1")))
611611
end
@@ -633,7 +633,7 @@ function copy!(dest::AbstractArray, dstart::Integer, src, sstart::Integer)
633633
end
634634

635635
# this method must be separate from the above since src might not have a length
636-
function copy!(dest::AbstractArray, dstart::Integer, src, sstart::Integer, n::Integer)
636+
function copyto!(dest::AbstractArray, dstart::Integer, src, sstart::Integer, n::Integer)
637637
n < 0 && throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative")))
638638
n == 0 && return dest
639639
dmax = dstart + n - 1
@@ -663,10 +663,10 @@ end
663663
## copy between abstract arrays - generally more efficient
664664
## since a single index variable can be used.
665665

666-
copy!(dest::AbstractArray, src::AbstractArray) =
667-
copy!(IndexStyle(dest), dest, IndexStyle(src), src)
666+
copyto!(dest::AbstractArray, src::AbstractArray) =
667+
copyto!(IndexStyle(dest), dest, IndexStyle(src), src)
668668

669-
function copy!(::IndexStyle, dest::AbstractArray, ::IndexStyle, src::AbstractArray)
669+
function copyto!(::IndexStyle, dest::AbstractArray, ::IndexStyle, src::AbstractArray)
670670
destinds, srcinds = linearindices(dest), linearindices(src)
671671
isempty(srcinds) || (first(srcinds) destinds && last(srcinds) destinds) ||
672672
throw(BoundsError(dest, srcinds))
@@ -676,7 +676,7 @@ function copy!(::IndexStyle, dest::AbstractArray, ::IndexStyle, src::AbstractArr
676676
return dest
677677
end
678678

679-
function copy!(::IndexStyle, dest::AbstractArray, ::IndexCartesian, src::AbstractArray)
679+
function copyto!(::IndexStyle, dest::AbstractArray, ::IndexCartesian, src::AbstractArray)
680680
destinds, srcinds = linearindices(dest), linearindices(src)
681681
isempty(srcinds) || (first(srcinds) destinds && last(srcinds) destinds) ||
682682
throw(BoundsError(dest, srcinds))
@@ -687,17 +687,17 @@ function copy!(::IndexStyle, dest::AbstractArray, ::IndexCartesian, src::Abstrac
687687
return dest
688688
end
689689

690-
function copy!(dest::AbstractArray, dstart::Integer, src::AbstractArray)
691-
copy!(dest, dstart, src, first(linearindices(src)), _length(src))
690+
function copyto!(dest::AbstractArray, dstart::Integer, src::AbstractArray)
691+
copyto!(dest, dstart, src, first(linearindices(src)), _length(src))
692692
end
693693

694-
function copy!(dest::AbstractArray, dstart::Integer, src::AbstractArray, sstart::Integer)
694+
function copyto!(dest::AbstractArray, dstart::Integer, src::AbstractArray, sstart::Integer)
695695
srcinds = linearindices(src)
696696
sstart srcinds || throw(BoundsError(src, sstart))
697-
copy!(dest, dstart, src, sstart, last(srcinds)-sstart+1)
697+
copyto!(dest, dstart, src, sstart, last(srcinds)-sstart+1)
698698
end
699699

700-
function copy!(dest::AbstractArray, dstart::Integer,
700+
function copyto!(dest::AbstractArray, dstart::Integer,
701701
src::AbstractArray, sstart::Integer,
702702
n::Integer)
703703
n == 0 && return dest
@@ -716,7 +716,7 @@ function copy(a::AbstractArray)
716716
copymutable(a)
717717
end
718718

719-
function copy!(B::AbstractVecOrMat{R}, ir_dest::AbstractRange{Int}, jr_dest::AbstractRange{Int},
719+
function copyto!(B::AbstractVecOrMat{R}, ir_dest::AbstractRange{Int}, jr_dest::AbstractRange{Int},
720720
A::AbstractVecOrMat{S}, ir_src::AbstractRange{Int}, jr_src::AbstractRange{Int}) where {R,S}
721721
if length(ir_dest) != length(ir_src)
722722
throw(ArgumentError(string("source and destination must have same size (got ",
@@ -763,7 +763,7 @@ julia> Base.copymutable(tup)
763763
"""
764764
function copymutable(a::AbstractArray)
765765
@_propagate_inbounds_meta
766-
copy!(similar(a), a)
766+
copyto!(similar(a), a)
767767
end
768768
copymutable(itr) = collect(itr)
769769

@@ -855,7 +855,7 @@ keys(s::IndexStyle, A::AbstractArray, B::AbstractArray...) = eachindex(s, A, B..
855855
## Conversions ##
856856

857857
convert(::Type{AbstractArray{T,N}}, A::AbstractArray{T,N}) where {T,N } = A
858-
convert(::Type{AbstractArray{T,N}}, A::AbstractArray{S,N}) where {T,S,N} = copy!(similar(A,T), A)
858+
convert(::Type{AbstractArray{T,N}}, A::AbstractArray{S,N}) where {T,S,N} = copyto!(similar(A,T), A)
859859
convert(::Type{AbstractArray{T}}, A::AbstractArray{S,N}) where {T,S,N} = convert(AbstractArray{T,N}, A)
860860

861861
convert(::Type{Array}, A::AbstractArray{T,N}) where {T,N} = convert(Array{T,N}, A)
@@ -1150,7 +1150,7 @@ function typed_hcat(::Type{T}, A::AbstractVecOrMat...) where T
11501150
for k=1:nargs
11511151
Ak = A[k]
11521152
n = length(Ak)
1153-
copy!(B, pos, Ak, 1, n)
1153+
copyto!(B, pos, Ak, 1, n)
11541154
pos += n
11551155
end
11561156
else
@@ -1248,7 +1248,7 @@ end
12481248
function _cat(A, shape::NTuple{N}, catdims, X...) where N
12491249
offsets = zeros(Int, N)
12501250
inds = Vector{UnitRange{Int}}(uninitialized, N)
1251-
concat = copy!(zeros(Bool, N), catdims)
1251+
concat = copyto!(zeros(Bool, N), catdims)
12521252
for x in X
12531253
for i = 1:N
12541254
if concat[i]

base/array.jl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function vect(X...)
9999
T = promote_typeof(X...)
100100
#T[ X[i] for i=1:length(X) ]
101101
# TODO: this is currently much faster. should figure out why. not clear.
102-
return copy!(Vector{T}(uninitialized, length(X)), X)
102+
return copyto!(Vector{T}(uninitialized, length(X)), X)
103103
end
104104

105105
size(a::Array, d) = arraysize(a, d)
@@ -143,7 +143,7 @@ end
143143
## copy ##
144144

145145
"""
146-
unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, N)
146+
unsafe_copyto!(dest::Ptr{T}, src::Ptr{T}, N)
147147
148148
Copy `N` elements from a source pointer to a destination, with no checking. The size of an
149149
element is determined by the type of the pointers.
@@ -152,7 +152,7 @@ The `unsafe` prefix on this function indicates that no validation is performed o
152152
pointers `dest` and `src` to ensure that they are valid. Incorrect usage may corrupt or
153153
segfault your program, in the same manner as C.
154154
"""
155-
function unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, n) where T
155+
function unsafe_copyto!(dest::Ptr{T}, src::Ptr{T}, n) where T
156156
# Do not use this to copy data between pointer arrays.
157157
# It can't be made safe no matter how carefully you checked.
158158
ccall(:memmove, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt),
@@ -161,7 +161,7 @@ function unsafe_copy!(dest::Ptr{T}, src::Ptr{T}, n) where T
161161
end
162162

163163
"""
164-
unsafe_copy!(dest::Array, do, src::Array, so, N)
164+
unsafe_copyto!(dest::Array, do, src::Array, so, N)
165165
166166
Copy `N` elements from a source array to a destination, starting at offset `so` in the
167167
source and `do` in the destination (1-indexed).
@@ -170,11 +170,11 @@ The `unsafe` prefix on this function indicates that no validation is performed t
170170
that N is inbounds on either array. Incorrect usage may corrupt or segfault your program, in
171171
the same manner as C.
172172
"""
173-
function unsafe_copy!(dest::Array{T}, doffs, src::Array{T}, soffs, n) where T
173+
function unsafe_copyto!(dest::Array{T}, doffs, src::Array{T}, soffs, n) where T
174174
t1 = @_gc_preserve_begin dest
175175
t2 = @_gc_preserve_begin src
176176
if isbits(T)
177-
unsafe_copy!(pointer(dest, doffs), pointer(src, soffs), n)
177+
unsafe_copyto!(pointer(dest, doffs), pointer(src, soffs), n)
178178
elseif isbitsunion(T)
179179
ccall(:memmove, Ptr{Void}, (Ptr{Void}, Ptr{Void}, UInt),
180180
pointer(dest, doffs), pointer(src, soffs), n * Base.bitsunionsize(T))
@@ -193,21 +193,21 @@ function unsafe_copy!(dest::Array{T}, doffs, src::Array{T}, soffs, n) where T
193193
end
194194

195195
"""
196-
copy!(dest, do, src, so, N)
196+
copyto!(dest, do, src, so, N)
197197
198198
Copy `N` elements from collection `src` starting at offset `so`, to array `dest` starting at
199199
offset `do`. Return `dest`.
200200
"""
201-
function copy!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where T
201+
function copyto!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where T
202202
n == 0 && return dest
203203
n > 0 || throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative")))
204204
if soffs < 1 || doffs < 1 || soffs+n-1 > length(src) || doffs+n-1 > length(dest)
205205
throw(BoundsError())
206206
end
207-
unsafe_copy!(dest, doffs, src, soffs, n)
207+
unsafe_copyto!(dest, doffs, src, soffs, n)
208208
end
209209

210-
copy!(dest::Array{T}, src::Array{T}) where {T} = copy!(dest, 1, src, 1, length(src))
210+
copyto!(dest::Array{T}, src::Array{T}) where {T} = copyto!(dest, 1, src, 1, length(src))
211211

212212
"""
213213
copy(x)
@@ -403,7 +403,7 @@ convert(::Type{Array{T}}, x::Array{T,n}) where {T,n} = x
403403
convert(::Type{Array{T,n}}, x::Array{T,n}) where {T,n} = x
404404

405405
convert(::Type{Array{T}}, x::AbstractArray{S,n}) where {T,n,S} = convert(Array{T,n}, x)
406-
convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copy!(Array{T,n}(uninitialized, size(x)), x)
406+
convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copyto!(Array{T,n}(uninitialized, size(x)), x)
407407

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

@@ -432,8 +432,8 @@ julia> collect(Float64, 1:2:5)
432432
"""
433433
collect(::Type{T}, itr) where {T} = _collect(T, itr, iteratorsize(itr))
434434

435-
_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Vector{T}(uninitialized, Int(length(itr)::Integer)), itr)
436-
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, axes(itr)), itr)
435+
_collect(::Type{T}, itr, isz::HasLength) where {T} = copyto!(Vector{T}(uninitialized, Int(length(itr)::Integer)), itr)
436+
_collect(::Type{T}, itr, isz::HasShape) where {T} = copyto!(similar(Array{T}, axes(itr)), itr)
437437
function _collect(::Type{T}, itr, isz::SizeUnknown) where T
438438
a = Vector{T}()
439439
for x in itr
@@ -475,7 +475,7 @@ collect(A::AbstractArray) = _collect_indices(axes(A), A)
475475
collect_similar(cont, itr) = _collect(cont, itr, iteratoreltype(itr), iteratorsize(itr))
476476

477477
_collect(cont, itr, ::HasEltype, isz::Union{HasLength,HasShape}) =
478-
copy!(_similar_for(cont, eltype(itr), itr, isz), itr)
478+
copyto!(_similar_for(cont, eltype(itr), itr, isz), itr)
479479

480480
function _collect(cont, itr, ::HasEltype, isz::SizeUnknown)
481481
a = _similar_for(cont, eltype(itr), itr, isz)
@@ -485,12 +485,12 @@ function _collect(cont, itr, ::HasEltype, isz::SizeUnknown)
485485
return a
486486
end
487487

488-
_collect_indices(::Tuple{}, A) = copy!(Array{eltype(A),0}(uninitialized), A)
488+
_collect_indices(::Tuple{}, A) = copyto!(Array{eltype(A),0}(uninitialized), A)
489489
_collect_indices(indsA::Tuple{Vararg{OneTo}}, A) =
490-
copy!(Array{eltype(A)}(uninitialized, length.(indsA)), A)
490+
copyto!(Array{eltype(A)}(uninitialized, length.(indsA)), A)
491491
function _collect_indices(indsA, A)
492492
B = Array{eltype(A)}(uninitialized, length.(indsA))
493-
copy!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA))
493+
copyto!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA))
494494
end
495495

496496
# define this as a macro so that the call to Inference
@@ -563,7 +563,7 @@ function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T
563563
else
564564
R = typejoin(T, S)
565565
new = similar(dest, R)
566-
copy!(new,1, dest,1, i-1)
566+
copyto!(new,1, dest,1, i-1)
567567
@inbounds new[i] = el
568568
return collect_to!(new, itr, i+1, st)
569569
end
@@ -589,7 +589,7 @@ function grow_to!(dest, itr, st)
589589
# TODO: merge back these two branches when copy! is re-enabled for sets
590590
union!(new, dest)
591591
else
592-
copy!(new, dest)
592+
copyto!(new, dest)
593593
end
594594
push!(new, el)
595595
return grow_to!(new, itr, st)
@@ -628,22 +628,22 @@ function getindex end
628628
@eval getindex(A::Array, i1::Int) = arrayref($(Expr(:boundscheck)), A, i1)
629629
@eval getindex(A::Array, i1::Int, i2::Int, I::Int...) = (@_inline_meta; arrayref($(Expr(:boundscheck)), A, i1, i2, I...))
630630

631-
# Faster contiguous indexing using copy! for UnitRange and Colon
631+
# Faster contiguous indexing using copyto! for UnitRange and Colon
632632
function getindex(A::Array, I::UnitRange{Int})
633633
@_inline_meta
634634
@boundscheck checkbounds(A, I)
635635
lI = length(I)
636636
X = similar(A, lI)
637637
if lI > 0
638-
unsafe_copy!(X, 1, A, first(I), lI)
638+
unsafe_copyto!(X, 1, A, first(I), lI)
639639
end
640640
return X
641641
end
642642
function getindex(A::Array, c::Colon)
643643
lI = length(A)
644644
X = similar(A, lI)
645645
if lI > 0
646-
unsafe_copy!(X, 1, A, 1, lI)
646+
unsafe_copyto!(X, 1, A, 1, lI)
647647
end
648648
return X
649649
end
@@ -694,14 +694,14 @@ function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int})
694694
return A
695695
end
696696

697-
# Faster contiguous setindex! with copy!
697+
# Faster contiguous setindex! with copyto!
698698
function setindex!(A::Array{T}, X::Array{T}, I::UnitRange{Int}) where T
699699
@_inline_meta
700700
@boundscheck checkbounds(A, I)
701701
lI = length(I)
702702
@boundscheck setindex_shape_check(X, lI)
703703
if lI > 0
704-
unsafe_copy!(A, first(I), X, 1, lI)
704+
unsafe_copyto!(A, first(I), X, 1, lI)
705705
end
706706
return A
707707
end
@@ -710,7 +710,7 @@ function setindex!(A::Array{T}, X::Array{T}, c::Colon) where T
710710
lI = length(A)
711711
@boundscheck setindex_shape_check(X, lI)
712712
if lI > 0
713-
unsafe_copy!(A, 1, X, 1, lI)
713+
unsafe_copyto!(A, 1, X, 1, lI)
714714
end
715715
return A
716716
end
@@ -806,7 +806,7 @@ function append!(a::Array{<:Any,1}, items::AbstractVector)
806806
itemindices = eachindex(items)
807807
n = length(itemindices)
808808
_growend!(a, n)
809-
copy!(a, length(a)-n+1, items, first(itemindices), n)
809+
copyto!(a, length(a)-n+1, items, first(itemindices), n)
810810
return a
811811
end
812812

@@ -850,9 +850,9 @@ function prepend!(a::Array{<:Any,1}, items::AbstractVector)
850850
n = length(itemindices)
851851
_growbeg!(a, n)
852852
if a === items
853-
copy!(a, 1, items, n+1, n)
853+
copyto!(a, 1, items, n+1, n)
854854
else
855-
copy!(a, 1, items, first(itemindices), n)
855+
copyto!(a, 1, items, first(itemindices), n)
856856
end
857857
return a
858858
end
@@ -1720,7 +1720,7 @@ function find(testf::Function, A)
17201720
end
17211721
end
17221722
I = Vector{Int}(uninitialized, length(tmpI))
1723-
copy!(I, tmpI)
1723+
copyto!(I, tmpI)
17241724
return I
17251725
end
17261726
_index_remapper(A::AbstractArray) = linearindices(A)

base/arraymath.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function flipdim(A::Array{T}, d::Integer) where T
100100
for j=0:stride:(N-stride)
101101
offs = j + 1 + (i-1)*M
102102
boffs = j + 1 + (ri-1)*M
103-
copy!(B, boffs, A, offs, M)
103+
copyto!(B, boffs, A, offs, M)
104104
end
105105
end
106106
else

0 commit comments

Comments
 (0)