Skip to content

Commit 191c68c

Browse files
authored
fix deepcopy on 0-field mutable structs (#31825)
1 parent 74fdde5 commit 191c68c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

base/deepcopy.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ end
5656

5757
function deepcopy_internal(@nospecialize(x), stackdict::IdDict)
5858
T = typeof(x)::DataType
59-
nf = nfields(x)
60-
(isbitstype(T) || nf == 0) && return x
59+
isbitstype(T) && return x
6160
if haskey(stackdict, x)
6261
return stackdict[x]
6362
end
6463
y = ccall(:jl_new_struct_uninit, Any, (Any,), T)
6564
if T.mutable
6665
stackdict[x] = y
6766
end
68-
for i in 1:nf
67+
for i in 1:nfields(x)
6968
if isdefined(x,i)
7069
ccall(:jl_set_nth_field, Cvoid, (Any, Csize_t, Any), y, i-1,
7170
deepcopy_internal(getfield(x,i), stackdict))

test/copy.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,10 @@ end
186186
end
187187
end
188188
end
189+
190+
# issue #17149
191+
mutable struct Bar17149
192+
end
193+
let x = Bar17149()
194+
@test deepcopy(x) !== x
195+
end

0 commit comments

Comments
 (0)