Skip to content

Commit 7273b9a

Browse files
authored
Make some more deepcopy_internal methods type stable (#54562)
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.
1 parent d122b55 commit 7273b9a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

base/deepcopy.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ deepcopy_internal(x::Module, stackdict::IdDict) = error("deepcopy of Modules not
3737

3838
function deepcopy_internal(x::SimpleVector, stackdict::IdDict)
3939
if haskey(stackdict, x)
40-
return stackdict[x]
40+
return stackdict[x]::typeof(x)
4141
end
4242
y = Core.svec(Any[deepcopy_internal(x[i], stackdict) for i = 1:length(x)]...)
4343
stackdict[x] = y
@@ -46,7 +46,7 @@ end
4646

4747
function deepcopy_internal(x::String, stackdict::IdDict)
4848
if haskey(stackdict, x)
49-
return stackdict[x]
49+
return stackdict[x]::typeof(x)
5050
end
5151
y = GC.@preserve x unsafe_string(pointer(x), sizeof(x))
5252
stackdict[x] = y
@@ -58,7 +58,7 @@ function deepcopy_internal(@nospecialize(x), stackdict::IdDict)
5858
nf = nfields(x)
5959
if ismutable(x)
6060
if haskey(stackdict, x)
61-
return stackdict[x]
61+
return stackdict[x]::typeof(x)
6262
end
6363
y = ccall(:jl_new_struct_uninit, Any, (Any,), T)
6464
stackdict[x] = y

test/copy.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,20 @@ end
248248
@test (@inferred Base.deepcopy_internal(zeros(), IdDict())) == zeros()
249249
end
250250

251-
@testset "deepcopy_internal big" begin
251+
@testset "deepcopy_internal inference" begin
252+
@inferred Base.deepcopy_internal(1, IdDict())
253+
@inferred Base.deepcopy_internal(1.0, IdDict())
252254
@inferred Base.deepcopy_internal(big(1), IdDict())
253255
@inferred Base.deepcopy_internal(big(1.0), IdDict())
256+
@inferred Base.deepcopy_internal('a', IdDict())
257+
@inferred Base.deepcopy_internal("abc", IdDict())
258+
@inferred Base.deepcopy_internal([1,2,3], IdDict())
259+
260+
# structs without custom deepcopy_internal method
261+
struct Immutable2; x::Int; end
262+
mutable struct Mutable2; x::Int; end
263+
@inferred Base.deepcopy_internal(Immutable2(1), IdDict())
264+
@inferred Base.deepcopy_internal(Mutable2(1), IdDict())
254265
end
255266

256267
@testset "`copyto!`'s unaliasing" begin

0 commit comments

Comments
 (0)