Skip to content

Commit eb938da

Browse files
authored
Move bounds checks on copyto!(dst, n, src) (#43517)
1 parent c69a202 commit eb938da

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

base/abstractarray.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,17 @@ end
914914

915915
function copyto!(dest::AbstractArray, dstart::Integer, src)
916916
i = Int(dstart)
917-
for x in src
918-
dest[i] = x
919-
i += 1
917+
if haslength(src) && length(dest) > 0
918+
@boundscheck checkbounds(dest, i:(i + length(src) - 1))
919+
for x in src
920+
@inbounds dest[i] = x
921+
i += 1
922+
end
923+
else
924+
for x in src
925+
dest[i] = x
926+
i += 1
927+
end
920928
end
921929
return dest
922930
end

test/arrayops.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,16 @@ end
21052105
@test_throws ArgumentError LinearAlgebra.copy_transpose!(a,2:3,1:3,b,1:5,2:7)
21062106
end
21072107

2108+
@testset "empty copyto!" begin
2109+
@test isempty(copyto!(Int[], ()))
2110+
@test isempty(copyto!(Int[], Int[]))
2111+
@test copyto!([1,2], ()) == [1,2]
2112+
2113+
@test isempty(copyto!(Int[], 1, ()))
2114+
@test isempty(copyto!(Int[], 1, Int[]))
2115+
@test copyto!([1,2], 1, ()) == [1,2]
2116+
end
2117+
21082118
module RetTypeDecl
21092119
using Test
21102120
import Base: +, *, broadcast, convert

0 commit comments

Comments
 (0)