Skip to content

Commit 242229b

Browse files
lgoettgensKristofferC
authored andcommitted
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. (cherry picked from commit 7273b9a)
1 parent e53c9c0 commit 242229b

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
@@ -246,9 +246,20 @@ end
246246
@test (@inferred Base.deepcopy_internal(zeros(), IdDict())) == zeros()
247247
end
248248

249-
@testset "deepcopy_internal big" begin
249+
@testset "deepcopy_internal inference" begin
250+
@inferred Base.deepcopy_internal(1, IdDict())
251+
@inferred Base.deepcopy_internal(1.0, IdDict())
250252
@inferred Base.deepcopy_internal(big(1), IdDict())
251253
@inferred Base.deepcopy_internal(big(1.0), IdDict())
254+
@inferred Base.deepcopy_internal('a', IdDict())
255+
@inferred Base.deepcopy_internal("abc", IdDict())
256+
@inferred Base.deepcopy_internal([1,2,3], IdDict())
257+
258+
# structs without custom deepcopy_internal method
259+
struct Immutable2; x::Int; end
260+
mutable struct Mutable2; x::Int; end
261+
@inferred Base.deepcopy_internal(Immutable2(1), IdDict())
262+
@inferred Base.deepcopy_internal(Mutable2(1), IdDict())
252263
end
253264

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

0 commit comments

Comments
 (0)