diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 239e75df52510..1592f4f2d03db 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -914,9 +914,17 @@ end function copyto!(dest::AbstractArray, dstart::Integer, src) i = Int(dstart) - for x in src - dest[i] = x - i += 1 + if haslength(src) && length(dest) > 0 + @boundscheck checkbounds(dest, i:(i + length(src) - 1)) + for x in src + @inbounds dest[i] = x + i += 1 + end + else + for x in src + dest[i] = x + i += 1 + end end return dest end diff --git a/test/arrayops.jl b/test/arrayops.jl index b2badb66ce93d..e289f6d87d889 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2105,6 +2105,16 @@ end @test_throws ArgumentError LinearAlgebra.copy_transpose!(a,2:3,1:3,b,1:5,2:7) end +@testset "empty copyto!" begin + @test isempty(copyto!(Int[], ())) + @test isempty(copyto!(Int[], Int[])) + @test copyto!([1,2], ()) == [1,2] + + @test isempty(copyto!(Int[], 1, ())) + @test isempty(copyto!(Int[], 1, Int[])) + @test copyto!([1,2], 1, ()) == [1,2] +end + module RetTypeDecl using Test import Base: +, *, broadcast, convert