From 21c51771f13a1798922afda3cffdf9d2dda83fc3 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 15 Mar 2024 00:00:19 +0530 Subject: [PATCH 1/3] Resolve ambiguities in similar and reshape --- src/structarray.jl | 8 +++++--- test/runtests.jl | 8 +++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/structarray.jl b/src/structarray.jl index d83ccbd3..d00ec4ad 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -289,13 +289,14 @@ for type in ( :(Tuple{Union{Integer, AbstractUnitRange}, Vararg{Union{Integer, AbstractUnitRange}}}), # disambiguation with Base :(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}), + :(Tuple{Integer, Vararg{Integer}}), ) @eval function Base.similar(::Type{<:StructArray{T, N, C}}, sz::$(type)) where {T, N, C} return buildfromschema(typ -> similar(typ, sz), T, C) end - @eval function Base.similar(s::StructArray, S::Type, sz::$(type)) - return _similar(s, S, sz) + @eval function Base.similar(s::StructArray, ::Type{T}, sz::$(type)) where {T} + return _similar(s, T, sz) end end @@ -470,8 +471,9 @@ for type in ( # mimic OffsetArrays signature :(Tuple{Union{Integer, AbstractUnitRange, Colon}, Vararg{Union{Integer, AbstractUnitRange, Colon}}}), # disambiguation with Base + :(Tuple{Integer, Vararg{Integer}}), :(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}), - :(Tuple{Vararg{Union{Colon, Integer}}}), + :(Tuple{Union{Colon, Integer}, Vararg{Union{Colon, Integer}}}), :(Tuple{Vararg{Union{Colon, Int}}}), :(Tuple{Colon}), ) diff --git a/test/runtests.jl b/test/runtests.jl index 29ff3efb..470f9538 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -394,6 +394,10 @@ end @test s isa Matrix{Float32} @test size(s) == (2, 2) + s = similar(t, Float32, Int8(2), 2) + @test s isa Matrix{Float32} + @test size(s) == (2, 2) + s = similar(t, Float32, 0:1, 2) @test s isa OffsetMatrix{Float32, Matrix{Float32}} @test axes(s) == (0:1, 1:2) @@ -1000,6 +1004,8 @@ end @test rs.a == [1 3; 2 4] @test rs.b == ["a" "c"; "b" "d"] + @test reshape(s, (Int8(2), 2)) == reshape(s, (2, 2)) + rs = reshape(s, (:,)) @test rs.a == s.a @test rs.b == s.b @@ -1543,5 +1549,5 @@ end end @testset "project quality" begin - Aqua.test_all(StructArrays, ambiguities=(; broken=true)) + Aqua.test_all(StructArrays) end From 7383d789b72a32bad269f9c028a3abbe6e6dba53 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 15 Mar 2024 00:09:19 +0530 Subject: [PATCH 2/3] Test for ambiguities only on recent Julia versions --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 470f9538..733d2297 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1549,5 +1549,5 @@ end end @testset "project quality" begin - Aqua.test_all(StructArrays) + Aqua.test_all(StructArrays, ambiguities = VERSION >= v"1.10") end From 84621ded2a889f7ecf56ee9c2114a3a1a4e1212a Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 27 Nov 2024 13:08:22 +0530 Subject: [PATCH 3/3] Update `similar` test to match failing case --- test/runtests.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 733d2297..97d4d860 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -394,7 +394,7 @@ end @test s isa Matrix{Float32} @test size(s) == (2, 2) - s = similar(t, Float32, Int8(2), 2) + s = similar(t, Float32, (Int8(2), 2)) @test s isa Matrix{Float32} @test size(s) == (2, 2) @@ -1212,7 +1212,7 @@ end # The following code defines `MyArray1/2/3` with different `BroadcastStyle`s. # 1. `MyArray1` and `MyArray1` have `similar` defined. # We use them to simulate `BroadcastStyle` overloading `Base.copyto!`. -# 2. `MyArray3` has no `similar` defined. +# 2. `MyArray3` has no `similar` defined. # We use it to simulate `BroadcastStyle` overloading `Base.copy`. # 3. Their resolved style could be summaryized as (`-` means conflict) # | MyArray1 | MyArray2 | MyArray3 | Array @@ -1308,7 +1308,7 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS f(s) = s .+= 1 f(s) @test (@allocated f(s)) == 0 - + # issue #185 A = StructArray(randn(ComplexF64, 3, 3)) B = randn(ComplexF64, 3, 3) @@ -1327,7 +1327,7 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS @testset "ambiguity check" begin test_set = Any[StructArray([1;2+im]), - 1:2, + 1:2, (1,2), StructArray(@SArray [1;1+2im]), (@SArray [1 2]),