Skip to content

Commit

Permalink
Deprecate assert function. Addresses #8856.
Browse files Browse the repository at this point in the history
  • Loading branch information
twadleigh committed Feb 25, 2018
1 parent 5929c56 commit 96813aa
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 60 deletions.
2 changes: 1 addition & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ function abstract_apply(@nospecialize(aft), fargs::Vector{Any}, aargtypes::Vecto
end
res = Union{}
nargs = length(fargs)
assert(nargs == length(aargtypes))
@assert nargs == length(aargtypes)
splitunions = 1 < countunionsplit(aargtypes) <= sv.params.MAX_APPLY_UNION_ENUM
ctypes = Any[Any[aft]]
for i = 1:nargs
Expand Down
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,13 @@ end
@deprecate(matchall(r::Regex, s::AbstractString; overlap::Bool = false),
collect(m.match for m in eachmatch(r, s, overlap = overlap)))

# PR 26194
export assert
function assert(x)
depwarn("`assert` is deprecated, use `@assert` instead.", :assert)
@assert x ""
end

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
19 changes: 2 additions & 17 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,9 @@ Raises a `SystemError` for `errno` with the descriptive string `sysfunc` if `ift
"""
systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(string(p), Libc.errno(), extrainfo)) : nothing

## assertion functions and macros ##
## assertion macro ##


"""
assert(cond)
Throw an [`AssertionError`](@ref) if `cond` is `false`.
Also available as the macro [`@assert`](@ref).
!!! warning
An assert might be disabled at various optimization levels.
Assert should therefore only be used as a debugging tool
and not used for authentication verification (e.g. verifying passwords),
nor should side effects needed for the function to work correctly
be used inside of asserts.
"""
assert(x) = x ? nothing : throw(AssertionError())

"""
@assert cond [text]
Expand All @@ -132,7 +117,7 @@ Message `text` is optionally displayed upon assertion failure.
!!! warning
An assert might be disabled at various optimization levels.
Assert should therefore only be used as a debugging tool
and not used for authentication verification (e.g. verifying passwords),
and not used for authentication verification (e.g., verifying passwords),
nor should side effects needed for the function to work correctly
be used inside of asserts.
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,6 @@ export
time_ns,

# errors
assert,
backtrace,
catch_backtrace,
error,
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ function readlink(path::AbstractString)
if ret < 0
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
uv_error("readlink", ret)
assert(false)
@assert false
end
tgt = unsafe_string(ccall(:jl_uv_fs_t_ptr, Ptr{Cchar}, (Ptr{Cvoid},), req))
ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct IOContext{IO_t <: IO} <: AbstractPipe
dict::ImmutableDict{Symbol, Any}

function IOContext{IO_t}(io::IO_t, dict::ImmutableDict{Symbol, Any}) where IO_t<:IO
assert(!(IO_t <: IOContext))
@assert !(IO_t <: IOContext) "Cannot create `IOContext` from another `IOContext`."
return new(io, dict)
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function lookup(ip::Base.InterpreterIP)
# interpreted top-level expression with no CodeInfo
return [StackFrame(top_level_scope_sym, empty_sym, 0, nothing, false, false, 0)]
else
assert(ip.code isa Core.CodeInfo)
@assert ip.code isa Core.CodeInfo
codeinfo = ip.code
func = top_level_scope_sym
file = empty_sym
Expand Down
2 changes: 1 addition & 1 deletion base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ macro view(ex)
if Meta.isexpr(ex, :ref)
ex = Expr(:call, view, ex.args...)
else # ex replaced by let ...; foo[...]; end
assert(Meta.isexpr(ex, :let) && Meta.isexpr(ex.args[2], :ref))
@assert Meta.isexpr(ex, :let) && Meta.isexpr(ex.args[2], :ref)
ex.args[2] = Expr(:call, view, ex.args[2].args...)
end
Expr(:&&, true, esc(ex))
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ Core.throw
Base.rethrow
Base.backtrace
Base.catch_backtrace
Base.assert
Base.@assert
Base.ArgumentError
Base.AssertionError
Expand Down
3 changes: 1 addition & 2 deletions examples/clustermanager/0mq/ZMQCM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function recv_data()
try
#println("On $(manager.zid_self) waiting to recv message")
zid = parse(Int,String(ZMQ.recv(manager.sub)))
assert(zid == manager.zid_self)
@assert zid == manager.zid_self

from_zid = parse(Int,String(ZMQ.recv(manager.sub)))
mtype = String(ZMQ.recv(manager.sub))
Expand Down Expand Up @@ -279,4 +279,3 @@ function print_worker_stdout(io, pid)
println(" From worker $(pid):\t$line")
end
end

4 changes: 2 additions & 2 deletions examples/clustermanager/simple/test_simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ cmanpath = joinpath(@__DIR__, "UnixDomainCM.jl")
include(cmanpath)

npids = addprocs(UnixDomainCM(2))
assert(length(npids) == 2)
@assert length(npids) == 2
test_pids = [remotecall_fetch(myid, x) for x in npids]
assert(npids == test_pids)
@assert npids == test_pids
rmprocs(npids; waitfor=1.0)

exit(0)
20 changes: 10 additions & 10 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ function init_worker(cookie::AbstractString, manager::ClusterManager=DefaultClus
cluster_manager = manager

# Since our pid has yet to be set, ensure no RemoteChannel / Future have been created or addprocs() called.
assert(nprocs() <= 1)
assert(isempty(PGRP.refs))
assert(isempty(client_refs))
@assert nprocs() <= 1
@assert isempty(PGRP.refs)
@assert isempty(client_refs)

# System is started in head node mode, cleanup related entries
empty!(PGRP.workers)
Expand Down Expand Up @@ -493,7 +493,7 @@ end

function create_worker(manager, wconfig)
# only node 1 can add new nodes, since nobody else has the full list of address:port
assert(LPROC.id == 1)
@assert LPROC.id == 1

# initiate a connect. Does not wait for connection completion in case of TCP.
w = Worker()
Expand Down Expand Up @@ -650,8 +650,8 @@ Set the passed cookie as the cluster cookie, then returns it.
"""
function cluster_cookie(cookie)
# The cookie must be an ASCII string with length <= HDR_COOKIE_LEN
assert(isascii(cookie))
assert(length(cookie) <= HDR_COOKIE_LEN)
@assert isascii(cookie)
@assert length(cookie) <= HDR_COOKIE_LEN

cookie = rpad(cookie, HDR_COOKIE_LEN)

Expand Down Expand Up @@ -685,7 +685,7 @@ function topology(t)
Base.depwarn("The topology :master_slave is deprecated, use :master_worker instead.", :topology)
t = :master_worker
end
assert(t in [:all_to_all, :master_worker, :custom])
@assert t in [:all_to_all, :master_worker, :custom]
if (PGRP.topology==t) || ((myid()==1) && (nprocs()==1)) || (myid() > 1)
PGRP.topology = t
else
Expand Down Expand Up @@ -1004,7 +1004,7 @@ end


function interrupt(pid::Integer)
assert(myid() == 1)
@assert myid() == 1
w = map_pid_wrkr[pid]
if isa(w, Worker)
manage(w.manager, w.id, w.config, :interrupt)
Expand All @@ -1026,7 +1026,7 @@ Interrupt the current executing task on the specified workers. This is equivalen
pressing Ctrl-C on the local machine. If no arguments are given, all workers are interrupted.
"""
function interrupt(pids::AbstractVector=workers())
assert(myid() == 1)
@assert myid() == 1
@sync begin
for pid in pids
@async interrupt(pid)
Expand Down Expand Up @@ -1120,7 +1120,7 @@ function init_parallel()
global LPROC
LPROC.id = 1
cluster_cookie(randstring(HDR_COOKIE_LEN))
assert(isempty(PGRP.workers))
@assert isempty(PGRP.workers)
register_worker(LPROC)
end

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ for (idx, tname) in enumerate(msgtypes)
end
end

let msg_cases = :(assert(false))
let msg_cases = :(@assert false)
for i = length(msgtypes):-1:1
mti = msgtypes[i]
msg_cases = :(if idx == $i
Expand Down
2 changes: 1 addition & 1 deletion stdlib/SharedArrays/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ for p in procs(d)
idxl = last(idxes_in_p)
d[idxf] = Float64(idxf)
rv = remotecall_fetch(p, d,idxf,idxl) do D,idxf,idxl
assert(D[idxf] == Float64(idxf))
@assert D[idxf] == Float64(idxf)
D[idxl] = Float64(idxl)
D[idxl]
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/SuiteSparse/src/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ function diag(F::Factor{Tv}) where Tv
xv = f.x
for j in 1:f.n
jj = unsafe_load(c0, j) + 1
assert(unsafe_load(r0, jj) == j - 1)
@assert unsafe_load(r0, jj) == j - 1
res[j] = unsafe_load(xv, jj)
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ using Distributed
cs = [Channel(N) for i in 1:5]
tf2 = () -> begin
if N > 0
foreach(c->assert(take!(c)==2), cs)
foreach(c->(@assert take!(c)==2), cs)
end
yield()
error("foo")
Expand Down Expand Up @@ -145,7 +145,7 @@ using Distributed

# channeled_tasks
for T in [Any, Int]
chnls, tasks = Base.channeled_tasks(2, (c1,c2)->(assert(take!(c1)==1); put!(c2,2)); ctypes=[T,T], csizes=[N,N])
chnls, tasks = Base.channeled_tasks(2, (c1,c2)->(@assert take!(c1)==1; put!(c2,2)); ctypes=[T,T], csizes=[N,N])
put!(chnls[1], 1)
@test take!(chnls[2]) == 2
@test_throws InvalidStateException wait(chnls[1])
Expand All @@ -156,7 +156,7 @@ using Distributed

f=Future()
tf4 = (c1,c2) -> begin
assert(take!(c1)==1)
@assert take!(c1)==1
wait(f)
end

Expand All @@ -181,7 +181,7 @@ using Distributed

# channel
tf6 = c -> begin
assert(take!(c)==2)
@assert take!(c)==2
error("foo")
end

Expand Down
15 changes: 0 additions & 15 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@

# Tests that do not really go anywhere else

# test assert() method
@test_throws AssertionError assert(false)
let res = assert(true)
@test res === nothing
end
let
try
assert(false)
error("unexpected")
catch ex
@test isa(ex, AssertionError)
@test isempty(ex.msg)
end
end

# test @assert macro
@test_throws AssertionError (@assert 1 == 2)
@test_throws AssertionError (@assert false)
Expand Down

0 comments on commit 96813aa

Please sign in to comment.