Skip to content

Commit

Permalink
Make some more deepcopy_internal methods type stable (#54562)
Browse files Browse the repository at this point in the history
Same as #54496 for some more
methods.
I added some more testcases than the changed methods. For
`SimpleVector`, I don't know how to construct an instance for a
testcase.

This could be backported to 1.11.
  • Loading branch information
lgoettgens authored May 23, 2024
1 parent d122b55 commit 7273b9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deepcopy_internal(x::Module, stackdict::IdDict) = error("deepcopy of Modules not

function deepcopy_internal(x::SimpleVector, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
return stackdict[x]::typeof(x)
end
y = Core.svec(Any[deepcopy_internal(x[i], stackdict) for i = 1:length(x)]...)
stackdict[x] = y
Expand All @@ -46,7 +46,7 @@ end

function deepcopy_internal(x::String, stackdict::IdDict)
if haskey(stackdict, x)
return stackdict[x]
return stackdict[x]::typeof(x)
end
y = GC.@preserve x unsafe_string(pointer(x), sizeof(x))
stackdict[x] = y
Expand All @@ -58,7 +58,7 @@ function deepcopy_internal(@nospecialize(x), stackdict::IdDict)
nf = nfields(x)
if ismutable(x)
if haskey(stackdict, x)
return stackdict[x]
return stackdict[x]::typeof(x)
end
y = ccall(:jl_new_struct_uninit, Any, (Any,), T)
stackdict[x] = y
Expand Down
13 changes: 12 additions & 1 deletion test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,20 @@ end
@test (@inferred Base.deepcopy_internal(zeros(), IdDict())) == zeros()
end

@testset "deepcopy_internal big" begin
@testset "deepcopy_internal inference" begin
@inferred Base.deepcopy_internal(1, IdDict())
@inferred Base.deepcopy_internal(1.0, IdDict())
@inferred Base.deepcopy_internal(big(1), IdDict())
@inferred Base.deepcopy_internal(big(1.0), IdDict())
@inferred Base.deepcopy_internal('a', IdDict())
@inferred Base.deepcopy_internal("abc", IdDict())
@inferred Base.deepcopy_internal([1,2,3], IdDict())

# structs without custom deepcopy_internal method
struct Immutable2; x::Int; end
mutable struct Mutable2; x::Int; end
@inferred Base.deepcopy_internal(Immutable2(1), IdDict())
@inferred Base.deepcopy_internal(Mutable2(1), IdDict())
end

@testset "`copyto!`'s unaliasing" begin
Expand Down

0 comments on commit 7273b9a

Please sign in to comment.