From 5563a1e42c31541cf534d02724bec3e375df5e03 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 098e85942a7277..2a351c0e75a8f9 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -910,9 +910,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 b2badb66ce93da..e289f6d87d8894 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