Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restack for struct with undef fields #498

Merged
merged 4 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/AutoObjectsReStacker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ _getnames(::Type{<:NamedTuple{names}}) where names = names
# https://github.com/JuliaLang/julia/issues/30210
ex = :x
elseif isstructtype(x)
new = Expr(
:new,
x,
map(n -> :(restack(getfield(x, $(QuoteNode(n))))), fieldnames(x))...,
)
fn = fieldnames(x)
lastfield = QuoteNode(fn[end])
new = Expr(:new, x, map(n -> :(restack(getfield(x, $(QuoteNode(n))))), fn)...)

ex = quote
isimmutable(x) ? $new : x
if isimmutable(x) && isdefined(x, $lastfield)
$new
else
x
end
end
else
ex = :x
Expand Down
7 changes: 7 additions & 0 deletions test/test_restack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ end

Base.getproperty(::CustomizedProperties, n::Symbol) = n

struct IncompleteInitialization{A,B}
a::A
b::B
IncompleteInitialization{A,B}(a::A) where {A,B}= new{A,B}(a)
end

function testlabel(x)
n = 50
s = repr(x)
Expand Down Expand Up @@ -56,6 +62,7 @@ end
),
CustomizedProperties(1, 2, 3),
CustomizedProperties(1, 2, ABC(1, 2, CustomizedProperties(1, 2, 3))),
IncompleteInitialization{Int,String}(2)
]
@test restack(x) === x
end
Expand Down