diff --git a/stdlib/LinearAlgebra/src/dense.jl b/stdlib/LinearAlgebra/src/dense.jl index 3db54933d2305d..99dd97ec5e7064 100644 --- a/stdlib/LinearAlgebra/src/dense.jl +++ b/stdlib/LinearAlgebra/src/dense.jl @@ -300,7 +300,11 @@ function diagm_size(size::Tuple{Int,Int}, kv::Pair{<:Integer,<:AbstractVector}.. return m, n end # For some type `T`, `zero(T)` is not a `T` and `zeros(T, ...)` fails. -promote_with_zero(T::Type) = promote_type(T, typeof(zero(T))) +# `zero_type(T::Type) = typeof(zero(T))` would not work for types such +# as `Array` for which `zero(::T)` is defined but not `zero(::Type{T})` +# so we use `promote_op` instead. +zero_type(T::Type) = promote_op(zero, T) +promote_with_zero(T::Type) = promote_type(T, zero_type(T)) function diagm_container(size, kv::Pair{<:Integer,<:AbstractVector}...) T = promote_type(map(x -> eltype(x.second), kv)...) return zeros(promote_with_zero(T), diagm_size(size, kv...)...) diff --git a/stdlib/LinearAlgebra/test/dense.jl b/stdlib/LinearAlgebra/test/dense.jl index 50a7ca860b9842..fb5b0856377bf4 100644 --- a/stdlib/LinearAlgebra/test/dense.jl +++ b/stdlib/LinearAlgebra/test/dense.jl @@ -940,7 +940,7 @@ end end struct TypeWithoutZero end -Base.zero(::Type{TypeWithoutZero}) = TypeWithZero() +Base.zero(::Union{TypeWithoutZero, Type{TypeWithoutZero}}) = TypeWithZero() struct TypeWithZero end Base.promote_rule(::Type{TypeWithoutZero}, ::Type{TypeWithZero}) = TypeWithZero Base.convert(::Type{TypeWithZero}, ::TypeWithoutZero) = TypeWithZero()