From 0c8eaa8d14b041e52cdc1ff1aa1efd256b9e7220 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Wed, 5 Jan 2022 22:51:34 -0500 Subject: [PATCH] fix an empty case, add tests --- base/abstractarray.jl | 5 ++--- test/arrayops.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) 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 3e20a92a9d990..9ba28e862afe6 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