Skip to content

Commit a2fa248

Browse files
authored
Merge pull request #24679 from Sacha0/finret
22018: make finalizer(f, x) return x rather than nothing
2 parents b8a488b + 0b6fb9f commit a2fa248

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ This section lists changes that do not have deprecation warnings.
151151

152152
* Juxtaposing string literals (e.g. `"x"y`) is now a syntax error ([#20575]).
153153

154+
* `finalizer(function, object)` now returns `object` rather than `nothing` ([#24679]).
155+
154156
* Macro calls with `for` expressions are now parsed as generators inside
155157
function argument lists ([#18650]). Examples:
156158

base/distributed/remotecall.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ end
5858

5959
function finalize_ref(r::AbstractRemoteRef)
6060
if r.where > 0 # Handle the case of the finalizer having been called manually
61-
islocked(client_refs) && return finalizer(finalize_ref, r) # delay finalizer for later, when it's not already locked
61+
if islocked(client_refs)
62+
# delay finalizer for later, when it's not already locked
63+
finalizer(finalize_ref, r)
64+
return nothing
65+
end
6266
delete!(client_refs, r)
6367
if isa(r, RemoteChannel)
6468
send_del_client(r)

base/gcutils.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
finalizer(f, x)
99
1010
Register a function `f(x)` to be called when there are no program-accessible references to
11-
`x`. The type of `x` must be a `mutable struct`, otherwise the behavior of this function is
12-
unpredictable.
11+
`x`, and return `x`. The type of `x` must be a `mutable struct`, otherwise the behavior of
12+
this function is unpredictable.
1313
"""
1414
function finalizer(@nospecialize(f), @nospecialize(o))
1515
if isimmutable(o)
1616
error("objects of type ", typeof(o), " cannot be finalized")
1717
end
1818
ccall(:jl_gc_add_finalizer_th, Void, (Ptr{Void}, Any, Any),
1919
Core.getptls(), o, f)
20+
return o
2021
end
2122

2223
function finalizer(f::Ptr{Void}, o::T) where T
@@ -26,6 +27,7 @@ function finalizer(f::Ptr{Void}, o::T) where T
2627
end
2728
ccall(:jl_gc_add_ptr_finalizer, Void, (Ptr{Void}, Any, Ptr{Void}),
2829
Core.getptls(), o, f)
30+
return o
2931
end
3032

3133
"""

base/weakkeydict.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ mutable struct WeakKeyDict{K,V} <: Associative{K,V}
2121
t = new(Dict{Any,V}(), Threads.RecursiveSpinLock(), identity)
2222
t.finalizer = function (k)
2323
# when a weak key is finalized, remove from dictionary if it is still there
24-
islocked(t) && return finalizer(t.finalizer, k)
24+
if islocked(t)
25+
finalizer(t.finalizer, k)
26+
return nothing
27+
end
2528
delete!(t, k)
2629
end
2730
return t

0 commit comments

Comments
 (0)