Skip to content

Commit

Permalink
Revert "Finalizers take function as first argument (closes #16307) (#…
Browse files Browse the repository at this point in the history
…24605)"

This reverts commit 52d81b0.
  • Loading branch information
yuyichao committed Dec 13, 2017
1 parent 87c1d4f commit f7c6095
Show file tree
Hide file tree
Showing 34 changed files with 76 additions and 102 deletions.
8 changes: 1 addition & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,7 @@ This section lists changes that do not have deprecation warnings.
* The return type of `reinterpret` has changed to `ReinterpretArray`. `reinterpret` on sparse
arrays has been discontinued.

* `Base.find_in_path` is now `Base.find_package` or `Base.find_source_file` ([#24320]).

* `finalizer` now takes functions or pointers as its first argument, and the object being
finalized as its second (rather than the reverse). For the majority of use cases
deprecation warnings will be triggered. However, deprecation warnings will not trigger where
(1) the callable argument is not a subtype of `Function`; or (2) both arguments are
`Function`s or `Ptr{Void}`s ([#24605]).
* `Base.find_in_path` is now `Base.find_package` or `Base.find_source_file` ([#24320])

* The `kill` function now throws errors on user error (e.g. on permission
errors), but returns successfully if the process had previously exited.
Expand Down
10 changes: 0 additions & 10 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2138,16 +2138,6 @@ end
@deprecate cumsum(A::AbstractArray) cumsum(A, 1)
@deprecate cumprod(A::AbstractArray) cumprod(A, 1)

# issue #16307
@deprecate finalizer(o, f::Function) finalizer(f, o)
# This misses other callables but they are very rare in the wild
@deprecate finalizer(o, f::Ptr{Void}) finalizer(f, o)

# Avoid ambiguity, can remove when deprecations are removed:
# This is almost certainly going to be a silent failure for code that is not updated.
finalizer(f::Ptr{Void}, o::Ptr{Void}) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)
finalizer(f::Ptr{Void}, o::Function) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)

# Broadcast extension API (#23939)
@eval Broadcast begin
Base.@deprecate_binding containertype combine_styles false
Expand Down
6 changes: 1 addition & 5 deletions base/distributed/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,7 @@ function create_worker(manager, wconfig)

w = Worker(w.id, r_s, w_s, manager; config=wconfig)
# install a finalizer to perform cleanup if necessary
finalizer(w) do w
if myid() == 1
manage(w.manager, w.id, w.config, :finalize)
end
end
finalizer(w, (w)->if myid() == 1 manage(w.manager, w.id, w.config, :finalize) end)

# set when the new worker has finshed connections with all other workers
ntfy_oid = RRID()
Expand Down
8 changes: 2 additions & 6 deletions base/distributed/clusterserialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ function serialize(s::ClusterSerializer, t::TypeName)
serialize_typename(s, t)
s.anonfunc_id = prev
push!(s.tn_obj_sent, identifier)
finalizer(t) do x
cleanup_tname_glbs(s, identifier)
end
finalizer(t, x->cleanup_tname_glbs(s, identifier))
end

# Send global refs if required.
Expand Down Expand Up @@ -135,9 +133,7 @@ function serialize_global_from_main(s::ClusterSerializer, sym)
elseif !haskey(s.glbs_sent, oid)
# set up a finalizer the first time this object is sent
try
finalizer(v) do x
delete_global_tracker(s,x)
end
finalizer(v, x->delete_global_tracker(s,x))
catch ex
# Do not track objects that cannot be finalized.
if isa(ex, ErrorException)
Expand Down
4 changes: 2 additions & 2 deletions base/distributed/remotecall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ function test_existing_ref(r::AbstractRemoteRef)
end

client_refs[r] = nothing
finalizer(finalize_ref, r)
finalizer(r, finalize_ref)
return r
end

function finalize_ref(r::AbstractRemoteRef)
if r.where > 0 # Handle the case of the finalizer having been called manually
if islocked(client_refs)
# delay finalizer for later, when it's not already locked
finalizer(finalize_ref, r)
finalizer(r, finalize_ref)
return nothing
end
delete!(client_refs, r)
Expand Down
2 changes: 1 addition & 1 deletion base/distributed/workerpool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mutable struct CachingPool <: AbstractWorkerPool

function CachingPool()
wp = new(Channel{Int}(typemax(Int)), Set{Int}(), Dict{Int, Function}())
finalizer(clear!, wp)
finalizer(wp, clear!)
wp
end
end
Expand Down
4 changes: 2 additions & 2 deletions base/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ mutable struct AsyncCondition
function AsyncCondition()
this = new(Libc.malloc(_sizeof_uv_async), Condition(), true)
associate_julia_struct(this.handle, this)
finalizer(uvfinalize, this)
finalizer(this, uvfinalize)
err = ccall(:uv_async_init, Cint, (Ptr{Void}, Ptr{Void}, Ptr{Void}),
eventloop(), this, uv_jl_asynccb::Ptr{Void})
if err != 0
Expand Down Expand Up @@ -372,7 +372,7 @@ mutable struct Timer
end

associate_julia_struct(this.handle, this)
finalizer(uvfinalize, this)
finalizer(this, uvfinalize)

ccall(:uv_update_time, Void, (Ptr{Void},), eventloop())
ccall(:uv_timer_start, Cint, (Ptr{Void}, Ptr{Void}, UInt64, UInt64),
Expand Down
6 changes: 3 additions & 3 deletions base/gcutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
==(w, v::WeakRef) = isequal(w, v.value)

"""
finalizer(f, x)
finalizer(x, f)
Register a function `f(x)` to be called when there are no program-accessible references to
`x`, and return `x`. The type of `x` must be a `mutable struct`, otherwise the behavior of
this function is unpredictable.
"""
function finalizer(@nospecialize(f), @nospecialize(o))
function finalizer(@nospecialize(o), @nospecialize(f))
if isimmutable(o)
error("objects of type ", typeof(o), " cannot be finalized")
end
Expand All @@ -20,7 +20,7 @@ function finalizer(@nospecialize(f), @nospecialize(o))
return o
end

function finalizer(f::Ptr{Void}, o::T) where T
function finalizer(o::T, f::Ptr{Void}) where T
@_inline_meta
if isimmutable(T)
error("objects of type ", T, " cannot be finalized")
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mutable struct BigInt <: Signed
function BigInt()
b = new(zero(Cint), zero(Cint), C_NULL)
MPZ.init!(b)
finalizer(cglobal((:__gmpz_clear, :libgmp)), b)
finalizer(b, cglobal((:__gmpz_clear, :libgmp)))
return b
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function IOStream(name::AbstractString, finalize::Bool)
buf = zeros(UInt8,sizeof_ios_t)
x = IOStream(name, buf)
if finalize
finalizer(close, x)
finalizer(x, close)
end
return x
end
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/gitcredential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mutable struct GitCredential
username::Nullable{<:AbstractString},
password::Nullable{<:AbstractString})
c = new(protocol, host, path, username, password, true)
finalizer(securezero!, c)
finalizer(c, securezero!)
return c
end
end
Expand Down
10 changes: 5 additions & 5 deletions base/libgit2/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ for (typ, owntyp, sup, cname) in [
obj = new(ptr)
if fin
Threads.atomic_add!(REFCOUNT, UInt(1))
finalizer(Base.close, obj)
finalizer(obj, Base.close)
end
return obj
end
Expand All @@ -958,7 +958,7 @@ for (typ, owntyp, sup, cname) in [
obj = new(owner, ptr)
if fin
Threads.atomic_add!(REFCOUNT, UInt(1))
finalizer(Base.close, obj)
finalizer(obj, Base.close)
end
return obj
end
Expand Down Expand Up @@ -1000,7 +1000,7 @@ mutable struct GitSignature <: AbstractGitObject
function GitSignature(ptr::Ptr{SignatureStruct})
@assert ptr != C_NULL
obj = new(ptr)
finalizer(Base.close, obj)
finalizer(obj, Base.close)
return obj
end
end
Expand Down Expand Up @@ -1143,7 +1143,7 @@ mutable struct UserPasswordCredential <: AbstractCredential
pass::String
function UserPasswordCredential(user::AbstractString="", pass::AbstractString="")
c = new(user, pass)
finalizer(securezero!, c)
finalizer(c, securezero!)
return c
end

Expand Down Expand Up @@ -1181,7 +1181,7 @@ mutable struct SSHCredential <: AbstractCredential
function SSHCredential(user::AbstractString="", pass::AbstractString="",
prvkey::AbstractString="", pubkey::AbstractString="")
c = new(user, pass, prvkey, pubkey)
finalizer(securezero!, c)
finalizer(c, securezero!)
return c
end

Expand Down
2 changes: 1 addition & 1 deletion base/locks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mutable struct Mutex <: AbstractLock
function Mutex()
m = new(zero(Int16), Libc.malloc(UV_MUTEX_SIZE))
ccall(:uv_mutex_init, Void, (Ptr{Void},), m.handle)
finalizer(_uv_hook_close, m)
finalizer(m, _uv_hook_close)
return m
end
end
Expand Down
4 changes: 2 additions & 2 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mutable struct BigFloat <: AbstractFloat
prec = precision(BigFloat)
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL)
ccall((:mpfr_init2,:libmpfr), Void, (Ref{BigFloat}, Clong), z, prec)
finalizer(cglobal((:mpfr_clear, :libmpfr)), z)
finalizer(z, cglobal((:mpfr_clear, :libmpfr)))
return z
end

Expand Down Expand Up @@ -967,7 +967,7 @@ function Base.deepcopy_internal(x::BigFloat, stackdict::ObjectIdDict)
prec = precision(x)
y = BigFloat(zero(Clong), zero(Cint), zero(Clong), C_NULL)
ccall((:mpfr_init2,:libmpfr), Void, (Ref{BigFloat}, Clong), y, prec)
finalizer(cglobal((:mpfr_clear, :libmpfr)), y)
finalizer(y, cglobal((:mpfr_clear, :libmpfr)))
ccall((:mpfr_set, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), y, x, ROUNDING_MODE[])
stackdict[x] = y
return y
Expand Down
8 changes: 4 additions & 4 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ precompile(Tuple{typeof(Base.copy!), Array{String, 1}, Int64, Array{Any, 1}, Int
precompile(Tuple{typeof(Base.empty!), Base.Dict{Int64, Union{Base.Distributed.Worker, Base.Distributed.LocalProcess}}})
precompile(Tuple{typeof(Core.Inference.isbits), Base.Distributed.DefaultClusterManager})
precompile(Tuple{typeof(Base.Distributed.init_worker), String, Base.Distributed.DefaultClusterManager})
precompile(Tuple{typeof(Base.finalizer), typeof(Base.uvfinalize), Base.TCPServer})
precompile(Tuple{typeof(Base.finalizer), Base.TCPServer, typeof(Base.uvfinalize)})
precompile(Tuple{Type{Base.TCPServer}, Ptr{Void}, Int64})
precompile(Tuple{typeof(Base.show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Int32})
precompile(Tuple{typeof(Base.print), Base.GenericIOBuffer{Array{UInt8, 1}}, Base.Libc.RawFD})
Expand Down Expand Up @@ -102,7 +102,7 @@ precompile(Tuple{typeof(Core.Inference.start), Core.Inference.Generator{Tuple{Tu
precompile(Tuple{typeof(Core.Inference.done), Core.Inference.Generator{Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}, Type{QuoteNode}}, Int64})
precompile(Tuple{typeof(Core.Inference.next), Core.Inference.Generator{Tuple{Tuple{Base.DevNullStream, Base.DevNullStream, Base.DevNullStream}}, Type{QuoteNode}}, Int64})
precompile(Tuple{typeof(Core.Inference.isbits), Base.DevNullStream})
precompile(Tuple{typeof(Base.finalizer), typeof(Base.uvfinalize), Base.Process})
precompile(Tuple{typeof(Base.finalizer), Base.Process, typeof(Base.uvfinalize)})
precompile(Tuple{typeof(Core.Inference.isbits), Tuple{Base.DevNullStream, Bool}})
precompile(Tuple{Type{Ref{Base.Cstring}}, Array{String, 1}})
precompile(Tuple{typeof(Core.Inference.eltype), Type{Array{String, 1}}})
Expand Down Expand Up @@ -1544,7 +1544,7 @@ precompile(Tuple{typeof(Base.rehash!), Base.Dict{WeakRef, Void}, Int64})
precompile(Tuple{typeof(Base.ht_keyindex2!), Base.Dict{WeakRef, Void}, WeakRef})
precompile(Tuple{typeof(Base._setindex!), Base.Dict{WeakRef, Void}, Void, WeakRef, Int64})
precompile(Tuple{typeof(Base.setindex!), Base.Dict{WeakRef, Void}, Void, WeakRef})
precompile(Tuple{typeof(Base.finalizer), typeof(Base.Distributed.finalize_ref), Base.Distributed.RemoteChannel{Base.Channel{Any}}})
precompile(Tuple{typeof(Base.finalizer), Base.Distributed.RemoteChannel{Base.Channel{Any}}, typeof(Base.Distributed.finalize_ref)})
precompile(Tuple{typeof(Base.Distributed.test_existing_ref), Base.Distributed.RemoteChannel{Base.Channel{Any}}})
precompile(Tuple{Type{Base.Distributed.RemoteChannel{T} where T<:Base.AbstractChannel}, Int64})
precompile(Tuple{Type{Base.Channel{Int64}}, Int64})
Expand Down Expand Up @@ -1704,7 +1704,7 @@ precompile(Tuple{typeof(Base.Filesystem.mkdir), String, UInt16})
precompile(Tuple{typeof(Base.Filesystem.mkpath), String, UInt16})
precompile(Tuple{typeof(Base.Filesystem.samefile), String, String})
precompile(Tuple{typeof(Base.Filesystem.unlink), String})
precompile(Tuple{typeof(Base.finalizer), typeof(Base.Distributed.finalize_ref), Base.Distributed.Future})
precompile(Tuple{typeof(Base.finalizer), Base.Distributed.Future, typeof(Base.Distributed.finalize_ref)})
precompile(Tuple{typeof(Base.find_all_in_cache_path), Symbol})
precompile(Tuple{typeof(Base.find_package), String})
precompile(Tuple{typeof(Base.find_source_file), String})
Expand Down
2 changes: 1 addition & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ mutable struct Process <: AbstractPipe
typemin(fieldtype(Process, :exitcode)),
typemin(fieldtype(Process, :termsignal)),
Condition(), Condition())
finalizer(uvfinalize, this)
finalizer(this, uvfinalize)
return this
end
end
Expand Down
8 changes: 4 additions & 4 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ mutable struct Regex
end
re = compile(new(pattern, compile_options, match_options, C_NULL,
C_NULL, Csize_t[], C_NULL))
finalizer(re) do re
re.regex == C_NULL || PCRE.free_re(re.regex)
re.match_data == C_NULL || PCRE.free_match_data(re.match_data)
end
finalizer(re, re->begin
re.regex == C_NULL || PCRE.free_re(re.regex)
re.match_data == C_NULL || PCRE.free_match_data(re.match_data)
end)
re
end
end
Expand Down
4 changes: 1 addition & 3 deletions base/repl/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,7 @@ function setup_interface(
try
hist_path = find_hist_file()
f = open(hist_path, true, true, true, false, false)
finalizer(replc) do replc
close(f)
end
finalizer(replc, replc->close(f))
hist_from_file(hp, f, hist_path)
catch e
print_response(repl, e, catch_backtrace(), true, Base.have_color)
Expand Down
6 changes: 3 additions & 3 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ mutable struct TCPSocket <: LibuvStream
ReentrantLock(),
DEFAULT_READ_BUFFER_SZ)
associate_julia_struct(tcp.handle, tcp)
finalizer(uvfinalize, tcp)
finalizer(tcp, uvfinalize)
return tcp
end
end
Expand Down Expand Up @@ -308,7 +308,7 @@ mutable struct TCPServer <: LibuvServer
Condition(),
Condition())
associate_julia_struct(tcp.handle, tcp)
finalizer(uvfinalize, tcp)
finalizer(tcp, uvfinalize)
return tcp
end
end
Expand Down Expand Up @@ -370,7 +370,7 @@ mutable struct UDPSocket <: LibuvStream
Condition(),
Condition())
associate_julia_struct(udp.handle, udp)
finalizer(uvfinalize, udp)
finalizer(udp, uvfinalize)
return udp
end
end
Expand Down
6 changes: 3 additions & 3 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mutable struct PipeEndpoint <: LibuvStream
ReentrantLock(),
DEFAULT_READ_BUFFER_SZ)
associate_julia_struct(handle, p)
finalizer(uvfinalize, p)
finalizer(p, uvfinalize)
return p
end
end
Expand All @@ -136,7 +136,7 @@ mutable struct PipeServer <: LibuvServer
Condition(),
Condition())
associate_julia_struct(p.handle, p)
finalizer(uvfinalize, p)
finalizer(p, uvfinalize)
return p
end
end
Expand Down Expand Up @@ -169,7 +169,7 @@ mutable struct TTY <: LibuvStream
nothing, ReentrantLock(),
DEFAULT_READ_BUFFER_SZ)
associate_julia_struct(handle, tty)
finalizer(uvfinalize, tty)
finalizer(tty, uvfinalize)
@static if Sys.iswindows()
tty.ispty = ccall(:jl_ispty, Cint, (Ptr{Void},), handle) != 0
end
Expand Down
8 changes: 4 additions & 4 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mutable struct WeakKeyDict{K,V} <: Associative{K,V}
t.finalizer = function (k)
# when a weak key is finalized, remove from dictionary if it is still there
if islocked(t)
finalizer(t.finalizer, k)
finalizer(k, t.finalizer)
return nothing
end
delete!(t, k)
Expand Down Expand Up @@ -91,7 +91,7 @@ trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock)

function setindex!(wkh::WeakKeyDict{K}, v, key) where K
k = convert(K, key)
finalizer(wkh.finalizer, k)
finalizer(k, wkh.finalizer)
lock(wkh) do
wkh.ht[WeakRef(k)] = v
end
Expand Down Expand Up @@ -121,12 +121,12 @@ length(t::WeakKeyDict) = length(t.ht)

function start(t::WeakKeyDict{K,V}) where V where K
gc_token = Ref{Bool}(false) # no keys will be deleted via finalizers until this token is gc'd
finalizer(gc_token) do r
finalizer(gc_token, function(r)
if r[]
r[] = false
unlock(t.lock)
end
end
end)
s = lock(t.lock)
gc_token[] = true
return (start(t.ht), gc_token)
Expand Down
Loading

0 comments on commit f7c6095

Please sign in to comment.