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

Deepcopy Fix #56990

Merged
merged 10 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
7 changes: 5 additions & 2 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ function _deepcopy_memory_t(@nospecialize(x::Memory), T, stackdict::IdDict)
end
return dest
end
@eval function deepcopy_internal(x::Array{T, N}, stackdict::IdDict) where {T, N}
function deepcopy_internal(x::Array{T, N}, stackdict::IdDict) where {T, N}
if haskey(stackdict, x)
return stackdict[x]::typeof(x)
end
stackdict[x] = $(Expr(:new, :(Array{T, N}), :(deepcopy_internal(x.ref, stackdict)), :(x.size)))
y = stackdict[x] = Array{T, N}(undef, ntuple(Returns(0), Val{N}()))
setfield!(y, :ref, deepcopy_internal(x.ref, stackdict))
setfield!(y, :size, x.size)
y
end
function deepcopy_internal(x::GenericMemoryRef, stackdict::IdDict)
if haskey(stackdict, x)
Expand Down
8 changes: 8 additions & 0 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ end
@test copyto!(s, String[]) == [1, 2] # No error
end

@testset "circular reference arrays" begin
# issue 56775
p = Any[nothing]
p[1] = p
p2 = deepcopy(p)
@test p2 === p2[1]
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
end

@testset "deepcopy_internal arrays" begin
@test (@inferred Base.deepcopy_internal(zeros(), IdDict())) == zeros()
end
Expand Down
Loading