From b617e8d3a77e49cd5625ca663af88e40f7796f15 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 1 Jul 2021 00:33:57 +0400 Subject: [PATCH] fix eltype of zero(::AbstractVector) (#41380) --- base/abstractarray.jl | 2 +- test/arrayops.jl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 49be81c74f858..92ae758ab1eb3 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1133,7 +1133,7 @@ function copymutable(a::AbstractArray) end copymutable(itr) = collect(itr) -zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T)) +zero(x::AbstractArray{T}) where {T} = fill!(similar(x, typeof(zero(T))), zero(T)) ## iteration support for arrays by iterating over `eachindex` in the array ## # Allows fast iteration by default for both IndexLinear and IndexCartesian arrays diff --git a/test/arrayops.jl b/test/arrayops.jl index 7823fce7a6175..f2f79f4212524 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2908,3 +2908,13 @@ end @test [fill(1); fill(2, (2,1,1))] == reshape([1; 2; 2], (3, 1, 1)) @test_throws DimensionMismatch [fill(1); rand(2, 2, 2)] end + +@testset "eltype of zero for arrays (issue #41348)" begin + for a in Any[[DateTime(2020), DateTime(2021)], [Date(2000), Date(2001)], [Time(1), Time(2)]] + @test a + zero(a) == a + b = reshape(a, :, 1) + @test b + zero(b) == b + c = view(b, 1:1, 1:1) + @test c + zero(c) == c + end +end