diff --git a/src/AutoObjectsReStacker.jl b/src/AutoObjectsReStacker.jl index 372d709bb3..ec7e342f12 100644 --- a/src/AutoObjectsReStacker.jl +++ b/src/AutoObjectsReStacker.jl @@ -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 diff --git a/test/test_restack.jl b/test/test_restack.jl index e2439bd9b8..7712b5b9af 100644 --- a/test/test_restack.jl +++ b/test/test_restack.jl @@ -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) @@ -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