Skip to content

Commit 0c8dc98

Browse files
committed
Fix constructor
1 parent 3bd326b commit 0c8dc98

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/blocksparsearray/blocksparsearray.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ function BlockSparseArray{T,N,A}(
171171
return BlockSparseArray{T,N,A}(undef, (dim1, dim_rest...))
172172
end
173173

174-
function unchecked_similartype(a, args...)
175-
A = Base.promote_op(similar, a, args...)
176-
return !isconcretetype(A) ? Array{T,N} : A
174+
function similartype_unchecked(
175+
A::Type{<:AbstractArray{T}}, axt::Type{<:Tuple{Vararg{Any,N}}}
176+
) where {T,N}
177+
A′ = Base.promote_op(similar, A, axt)
178+
return !isconcretetype(A′) ? Array{T,N} : A′
177179
end
178180

179181
function BlockSparseArray{T,N}(
@@ -186,7 +188,7 @@ function BlockSparseArray{T,N}(
186188
# ```
187189
# but that doesn't work when `similar` isn't defined or
188190
# isn't type stable.
189-
A = unchecked_similartype(Array{T}, axt)
191+
A = similartype_unchecked(Array{T}, axt)
190192
return BlockSparseArray{T,N,A}(undef, axes)
191193
end
192194

test/test_basics.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using BlockSparseArrays:
3333
eachblockstoredindex,
3434
eachstoredblock,
3535
eachstoredblockdiagindex,
36+
similartype_unchecked,
3637
sparsemortar,
3738
view!
3839
using GPUArraysCore: @allowscalar
@@ -44,6 +45,20 @@ using TestExtras: @constinferred
4445
using TypeParameterAccessors: TypeParameterAccessors, Position
4546
include("TestBlockSparseArraysUtils.jl")
4647

48+
@testset "similartype_unchecked" begin
49+
@test @constinferred(similartype_unchecked(Array{Float32}, NTuple{2,Int})) ===
50+
Matrix{Float32}
51+
@test @constinferred(similartype_unchecked(Array{Float32}, NTuple{2,Base.OneTo{Int}})) ===
52+
Matrix{Float32}
53+
@test @constinferred(similartype_unchecked(AbstractArray{Float32}, NTuple{2,Int})) ===
54+
Matrix{Float32}
55+
@test @constinferred(similartype_unchecked(JLArray{Float32}, NTuple{2,Int})) ===
56+
JLMatrix{Float32}
57+
@test @constinferred(
58+
similartype_unchecked(JLArray{Float32}, NTuple{2,Base.OneTo{Int}})
59+
) === JLMatrix{Float32}
60+
end
61+
4762
arrayts = (Array, JLArray)
4863
@testset "BlockSparseArrays (arraytype=$arrayt, eltype=$elt)" for arrayt in arrayts,
4964
elt in (Float32, Float64, Complex{Float32}, Complex{Float64})

0 commit comments

Comments
 (0)