From 3f8d86f40548c11aa5c543d65ec973cf4bb59767 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 8 Jan 2026 13:12:02 -0500 Subject: [PATCH] Fix mixing sparse and dense styles --- Project.toml | 2 +- src/abstractsparsearraystyle.jl | 7 +++++++ test/test_sparse_style.jl | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 65ec019..57d785d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SparseArraysBase" uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208" authors = ["ITensor developers and contributors"] -version = "0.8.2" +version = "0.8.3" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/src/abstractsparsearraystyle.jl b/src/abstractsparsearraystyle.jl index 24c1f08..0411b63 100644 --- a/src/abstractsparsearraystyle.jl +++ b/src/abstractsparsearraystyle.jl @@ -67,6 +67,13 @@ function FunctionImplementations.Style( ) return style1 end +# Fix ambiguity error with `Style(::AbstractSparseArrayStyle, ::AbstractArrayStyle)`. +using FunctionImplementations: DefaultArrayStyle +function FunctionImplementations.Style( + style1::AbstractSparseArrayStyle, style2::DefaultArrayStyle + ) + return style1 +end to_vec(x) = vec(collect(x)) to_vec(x::AbstractArray) = vec(x) diff --git a/test/test_sparse_style.jl b/test/test_sparse_style.jl index 9463607..735b202 100644 --- a/test/test_sparse_style.jl +++ b/test/test_sparse_style.jl @@ -1,5 +1,6 @@ -using FunctionImplementations: Style, style -using SparseArraysBase: AbstractSparseArrayStyle, SparseArrayStyle, sparse_style, sparsezeros +using FunctionImplementations: DefaultArrayStyle, Style, style +using SparseArraysBase: AbstractSparseArrayStyle, SparseArrayStyle, sparse_style, + sparsezeros using Test: @test, @testset module TestSparseStyleUtils @@ -23,4 +24,11 @@ end TestSparseStyleUtils.MySparseArrayStyle() @test style(sparsezeros(2, 2), TestSparseStyleUtils.MySparseArray{Float64, 2}((2, 2))) ≡ SparseArrayStyle() + + # Regression tests for ambiguity caused by combining AbstractSparseArrayStyle with + # DefaultArrayStyle. + @test Style(TestSparseStyleUtils.MySparseArrayStyle(), DefaultArrayStyle()) ≡ + TestSparseStyleUtils.MySparseArrayStyle() + @test style(TestSparseStyleUtils.MySparseArray{Float64, 2}((2, 2)), randn(2, 2)) ≡ + TestSparseStyleUtils.MySparseArrayStyle() end