From 45a1e9b989263161861b3fa6888731610210b8b6 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 24 Apr 2019 17:30:10 -0400 Subject: [PATCH] fix `deepcopy` on 0-field mutable structs --- base/deepcopy.jl | 5 ++--- test/copy.jl | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/base/deepcopy.jl b/base/deepcopy.jl index bc9802f0b36ce..9ec829e41558c 100644 --- a/base/deepcopy.jl +++ b/base/deepcopy.jl @@ -53,8 +53,7 @@ end function deepcopy_internal(@nospecialize(x), stackdict::IdDict) T = typeof(x)::DataType - nf = nfields(x) - (isbitstype(T) || nf == 0) && return x + isbitstype(T) && return x if haskey(stackdict, x) return stackdict[x] end @@ -62,7 +61,7 @@ function deepcopy_internal(@nospecialize(x), stackdict::IdDict) if T.mutable stackdict[x] = y end - for i in 1:nf + for i in 1:nfields(x) if isdefined(x,i) ccall(:jl_set_nth_field, Cvoid, (Any, Csize_t, Any), y, i-1, deepcopy_internal(getfield(x,i), stackdict)) diff --git a/test/copy.jl b/test/copy.jl index 10bdca12522df..44504709db0a9 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -176,3 +176,10 @@ end end end end + +# issue #17149 +mutable struct Bar17149 +end +let x = Bar17149() + @test deepcopy(x) !== x +end