Skip to content

Commit 428b1be

Browse files
Merge pull request SciML#542 from JoshuaLampert/fix-recursivecopy
Fix `recursivecopy!` for mixed nested `VectorOfArray`
2 parents 2bcebfb + d443b77 commit 428b1be

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ function recursivecopy!(
9595
end
9696

9797
function recursivecopy!(b::AbstractVectorOfArray, a::AbstractVectorOfArray)
98-
if ArrayInterface.ismutable(eltype(b.u))
99-
@inbounds for i in eachindex(b.u, a.u)
98+
@inbounds for i in eachindex(b.u, a.u)
99+
if ArrayInterface.ismutable(b.u[i]) || b.u[i] isa AbstractVectorOfArray
100100
recursivecopy!(b.u[i], a.u[i])
101+
else
102+
b.u[i] = recursivecopy(a.u[i])
101103
end
102-
else
103-
copyto!(b.u, a.u)
104104
end
105105
return b
106106
end

test/utils_test.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ end
139139
@test u1.u[2] == [2.0, 2.0]
140140
@test u1.u[1] isa SVector
141141
@test u1.u[2] isa SVector
142+
143+
# mixed/nested partition types create a Union eltype for `u`.
144+
# recursivecopy! must not fall back to a shallow copy in this case.
145+
a = VectorOfArray([ones(2), VectorOfArray([1.0, 1.0])])
146+
b = recursivecopy(a)
147+
recursivecopy!(b, a)
148+
@test !(b.u[1] === a.u[1])
149+
@test !(b.u[2] === a.u[2])
150+
b.u[1][1] = 99.0
151+
@test a.u[1][1] == 1.0
142152
end
143153

144154
@testset "VectorOfArray similar with nested scalar leaves" begin

0 commit comments

Comments
 (0)