From e96cf4aa87eb1ce9a392be85d392aa2f821f07a9 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Thu, 12 Nov 2026 09:51:41 -0500 Subject: [PATCH 1/2] Fix aliasing detection for LazyRows Define dataids and unaliascopy for LazyRows, so broadcasting from LazyRows into one of its own columns no longer gives wrong results. --- src/lazy.jl | 2 ++ test/runtests.jl | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/lazy.jl b/src/lazy.jl index aacb599a..06db4cb4 100644 --- a/src/lazy.jl +++ b/src/lazy.jl @@ -77,6 +77,8 @@ struct LazyRows{T, N, C, I} <: AbstractArray{LazyRow{T, N, C, I}, N} columns::StructArray{T, N, C, I} end Base.parent(v::LazyRows) = getfield(v, 1) +Base.dataids(v::LazyRows) = Base.dataids(parent(v)) +Base.unaliascopy(v::LazyRows) = LazyRows(Base.unaliascopy(parent(v))) components(v::LazyRows) = components(parent(v)) component(v::LazyRows, key) = component(parent(v), key) diff --git a/test/runtests.jl b/test/runtests.jl index bad13d78..66a2bb7e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1203,6 +1203,20 @@ end Base.showarg(io, rows, false) str = String(take!(io)) @test str == "LazyRows(::Matrix{Float64}, ::Matrix{Float64})" + + # LazyRows shares memory with its columns + s = StructArray(a=[1, 2, 3, 4], b=[10, 20, 30, 40]) + rows = LazyRows(s) + @test Base.dataids(rows) == Base.dataids(s) + @test Base.mightalias(rows, s.a) + @test Base.mightalias(view(rows, 2:3), s.b) + @test !Base.mightalias(rows, [1, 2, 3, 4]) + rc = Base.unaliascopy(rows) + @test rc isa typeof(rows) + @test parent(rc) == s + @test !Base.mightalias(rc, s) + s.a .= (r -> r.a).(view(rows, 4:-1:1)) + @test s.a == [4, 3, 2, 1] end @testset "refarray" begin From 4fe01e89c975e4793cbbd4b5023540a954919219 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Mon, 16 Nov 2026 09:15:55 -0500 Subject: [PATCH 2/2] Define unaliascopy for StructArray Base's fallback uses copy, which changes the type for components such as views, and errors during broadcast unaliasing. --- src/structarray.jl | 1 + test/runtests.jl | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/structarray.jl b/src/structarray.jl index 2b07a517..f01858f3 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -654,6 +654,7 @@ function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray) end Base.dataids(u::StructArray) = mapreduce(Base.dataids, (a, b) -> (a..., b...), values(components(u)), init=()) +Base.unaliascopy(s::StructArray{T}) where {T} = StructArray{T}(map(Base.unaliascopy, components(s))) # Since all the components have the same axes, we choose the type of the first one to # define IteratorSize for a StructArray diff --git a/test/runtests.jl b/test/runtests.jl index 66a2bb7e..08adc61b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1217,6 +1217,10 @@ end @test !Base.mightalias(rc, s) s.a .= (r -> r.a).(view(rows, 4:-1:1)) @test s.a == [4, 3, 2, 1] + s = StructArray(a=view([1, 2, 3], :)) + @test Base.unaliascopy(LazyRows(s)) isa typeof(LazyRows(s)) + s.a .= (r -> r.a).(view(LazyRows(s), 3:-1:1)) + @test s.a == [3, 2, 1] end @testset "refarray" begin @@ -1375,6 +1379,15 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS # used inside of broadcast but we also test it here explicitly @test isa(@inferred(Base.dataids(s)), NTuple{N, UInt} where {N}) + # unaliasing keeps the exact type even when `copy` of a component would change it + u = StructArray(a=view([1, 2, 3], :), b=1:3, c=StructArray(x=reshape([1, 2, 3], 3))) + uc = @inferred Base.unaliascopy(u) + @test uc isa typeof(u) + @test uc == u + @test !Base.mightalias(uc, u) + u.a .= (r -> r.a + r.b + r.c.x).(view(u, 3:-1:1)) + @test u.a == [9, 6, 3] + @testset "style conflict check" begin using StructArrays: StructArrayStyle