diff --git a/base/abstractarray.jl b/base/abstractarray.jl index fa37bc0b7dc1b..8b9f3217b930d 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -904,9 +904,8 @@ end function copyto!(dest::AbstractArray, dstart::Integer, src) i = Int(dstart) - if haslength(src) - checkbounds(dest, i) - checkbounds(dest, i + length(src) - 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 diff --git a/test/arrayops.jl b/test/arrayops.jl index abdf61e654c01..fd5ba64c32a52 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2122,6 +2122,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