Skip to content

Commit

Permalink
Make it possible to disable asserts with --release
Browse files Browse the repository at this point in the history
Renamed current assert function/macro to enforce, and added a
conditional assert that delegates to enforce unless the --release
switch is provided.
  • Loading branch information
mlhetland committed Jul 26, 2014
1 parent 6ef658a commit 366e00a
Show file tree
Hide file tree
Showing 30 changed files with 119 additions and 87 deletions.
14 changes: 7 additions & 7 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,12 @@ function normalize_key(key::String)
c, i == next(key, i)
if c == 'C'
c, i == next(key, i)
@assert c == '-'
@enforce c == '-'
c, i == next(key, i)
write(buf, uppercase(c)-64)
elseif c == 'M'
c, i == next(key, i)
@assert c == '-'
@enforce c == '-'
c, i == next(key, i)
write(buf, '\e')
write(buf, c)
Expand Down Expand Up @@ -927,7 +927,7 @@ function history_set_backward(s::SearchState, backward)
s.backward = backward
end

refresh_multi_line(termbuf::TerminalBuffer, term, s::Union(SearchState,PromptState)) = (@assert term == terminal(s); refresh_multi_line(termbuf,s))
refresh_multi_line(termbuf::TerminalBuffer, term, s::Union(SearchState,PromptState)) = (@enforce term == terminal(s); refresh_multi_line(termbuf,s))
function refresh_multi_line(termbuf::TerminalBuffer, s::SearchState)
buf = IOBuffer()
write(buf, pointer(s.query_buffer.data), s.query_buffer.ptr-1)
Expand Down Expand Up @@ -978,10 +978,10 @@ HistoryPrompt{T<:HistoryProvider}(hp::T) = HistoryPrompt{T}(hp)
init_state(terminal, p::HistoryPrompt) = SearchState(terminal, p, true, IOBuffer(), IOBuffer())

state(s::MIState, p) = s.mode_state[p]
state(s::PromptState, p) = (@assert s.p == p; s)
state(s::PromptState, p) = (@enforce s.p == p; s)
mode(s::MIState) = s.current_mode
mode(s::PromptState) = s.p
mode(s::SearchState) = @assert false
mode(s::SearchState) = @enforce false

# Search Mode completions
function complete_line(s::SearchState, repeats)
Expand Down Expand Up @@ -1300,7 +1300,7 @@ function activate(p::Union(Prompt,HistoryPrompt), s::Union(SearchState,PromptSta
end

function activate(p::Union(Prompt,HistoryPrompt), s::MIState, termbuf)
@assert p == s.current_mode
@enforce p == s.current_mode
activate(p, s.mode_state[s.current_mode], termbuf)
end
activate(m::ModalInterface, s::MIState, termbuf) = activate(s.current_mode, s, termbuf)
Expand Down Expand Up @@ -1407,7 +1407,7 @@ function prompt!(terminal, prompt, s = init_state(terminal, prompt))
return buffer(s), true, true
end
else
@assert state == :ok
@enforce state == :ok
end
end
finally
Expand Down
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ end

function history_move(s::LineEdit.MIState, hist::REPLHistoryProvider, idx::Int, save_idx::Int = hist.cur_idx)
max_idx = length(hist.history) + 1
@assert 1 <= hist.cur_idx <= max_idx
@enforce 1 <= hist.cur_idx <= max_idx
(1 <= idx <= max_idx) || return :none
idx != hist.cur_idx || return :none

Expand Down
8 changes: 4 additions & 4 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ end
function gen_broadcast_body_cartesian(nd::Int, narrays::Int, f::Function)
F = Expr(:quote, f)
quote
@assert ndims(B) == $nd
@enforce ndims(B) == $nd
@ncall $narrays check_broadcast_shape size(B) k->A_k
@nloops($nd, i, B,
d->(@nexprs $narrays k->(j_d_k = size(A_k, d) == 1 ? 1 : i_d)), # pre
Expand All @@ -84,7 +84,7 @@ end
function gen_broadcast_body_iter(nd::Int, narrays::Int, f::Function)
F = Expr(:quote, f)
quote
@assert ndims(B) == $nd
@enforce ndims(B) == $nd
@ncall $narrays check_broadcast_shape size(B) k->A_k
@nexprs 1 d->(@nexprs $narrays k->(state_k_0 = state_k_{$nd} = start(A_k)))
@nexprs $nd d->(@nexprs $narrays k->(skip_k_d = size(A_k, d) == 1))
Expand Down Expand Up @@ -123,7 +123,7 @@ end
function gen_broadcast_body_cartesian_tobitarray(nd::Int, narrays::Int, f::Function)
F = Expr(:quote, f)
quote
@assert ndims(B) == $nd
@enforce ndims(B) == $nd
@ncall $narrays check_broadcast_shape size(B) k->A_k
C = Array(Bool, bitcache_size)
Bc = B.chunks
Expand Down Expand Up @@ -152,7 +152,7 @@ end
function gen_broadcast_body_iter_tobitarray(nd::Int, narrays::Int, f::Function)
F = Expr(:quote, f)
quote
@assert ndims(B) == $nd
@enforce ndims(B) == $nd
@ncall $narrays check_broadcast_shape size(B) k->A_k
C = Array(Bool, bitcache_size)
Bc = B.chunks
Expand Down
2 changes: 1 addition & 1 deletion base/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function ngenerate(itersym, returntypeexpr, prototype, bodyfunc, dims=1:CARTESIA
setitersym, extractvarargs = :(), N -> nothing
else
s = symbol(varname)
setitersym = hasparameter(prototype, itersym) ? (:(@assert $itersym == length($s))) : (:($itersym = length($s)))
setitersym = hasparameter(prototype, itersym) ? (:(@enforce $itersym == length($s))) : (:($itersym = length($s)))
extractvarargs = N -> Expr(:block, map(popescape, _nextract(N, s, s).args)...)
end
fsym = funcsym(prototype)
Expand Down
16 changes: 15 additions & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ function process_options(args::Vector{UTF8String})
startup = false
elseif args[i] == "-i"
global is_interactive = true
elseif beginswith(args[i], "--release")
if args[i] == "--release"
is_release = true
elseif args[i][13] == '='
val = args[i][14:end]
if in(val, ("yes","1","true"))
global is_release = true
elseif !in(val, ("no","0","false"))
error("invalid option: ", args[i])
end
end
elseif beginswith(args[i], "--color")
if args[i] == "--color"
color_set = true
Expand Down Expand Up @@ -297,6 +308,9 @@ const roottask = current_task()
is_interactive = false
isinteractive() = (is_interactive::Bool)

is_release = false
isrelease() = (is_release::Bool)

const LOAD_PATH = ByteString[]
function init_load_path()
vers = "v$(VERSION.major).$(VERSION.minor)"
Expand All @@ -312,7 +326,7 @@ function init_head_sched()
global PGRP
global LPROC
LPROC.id = 1
assert(length(PGRP.workers) == 0)
enforce(length(PGRP.workers) == 0)
register_worker(LPROC)
end

Expand Down
6 changes: 3 additions & 3 deletions base/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ macro math_const(sym, val, def)
$bigconvert
Base.convert(::Type{Float64}, ::MathConst{$qsym}) = $val
Base.convert(::Type{Float32}, ::MathConst{$qsym}) = $(float32(val))
@assert isa(big($esym), BigFloat)
@assert float64($esym) == float64(big($esym))
@assert float32($esym) == float32(big($esym))
@enforce isa(big($esym), BigFloat)
@enforce float64($esym) == float64(big($esym))
@enforce float32($esym) == float32(big($esym))
end
end

Expand Down
6 changes: 3 additions & 3 deletions base/darray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type DArray{T,N,A} <: AbstractArray{T,N}

function DArray(dims, chunks, pmap, indexes, cuts)
# check invariants
assert(size(chunks) == size(indexes))
assert(length(chunks) == length(pmap))
assert(dims == map(last,last(indexes)))
enforce(size(chunks) == size(indexes))
enforce(length(chunks) == length(pmap))
enforce(dims == map(last,last(indexes)))
new(dims, chunks, pmap, indexes, cuts)
end
end
Expand Down
18 changes: 16 additions & 2 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ systemerror(p, b::Bool) = b ? throw(SystemError(string(p))) : nothing

## assertion functions and macros ##

assert(x) = x ? nothing : error("assertion failed")
macro assert(ex,msgs...)
# unlike assert, enforce cannot be turned off with the --release option

enforce(x) = x ? nothing : error("assertion failed")
macro enforce(ex,msgs...)
msg = isempty(msgs) ? ex : msgs[1]
if !isempty(msgs) && isa(msg, Expr)
# message is an expression needing evaluating
Expand All @@ -54,3 +56,15 @@ macro assert(ex,msgs...)
end
:($(esc(ex)) ? $(nothing) : error($msg))
end

# assert will be a no-op if --release is supplied on command-line; otherwise
# equivalent to enforce

assert(x) = isrelease() ? nothing : enforce(x)
macro assert(ex,msgs...)
if isrelease()
:nothing
else
:(@enforce($(esc(ex)),$(esc(msgs))...))
end
end
3 changes: 3 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ export
toq,

# errors
enforce,
assert,
backtrace,
catch_backtrace,
Expand Down Expand Up @@ -1051,6 +1052,7 @@ export
isconst,
isgeneric,
isinteractive,
isrelease,
less,
methods,
methodswith,
Expand Down Expand Up @@ -1323,6 +1325,7 @@ export
@bigint_str,
@mstr,
@unexpected,
@enforce,
@assert,
@cmd,
@time,
Expand Down
8 changes: 4 additions & 4 deletions base/fftw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ for (Tr,Tc) in ((:Float32,:Complex64),(:Float64,:Complex128))

function brfft(X::StridedArray{$Tc}, d::Integer, region::Integer)
osize = [size(X)...]
@assert osize[region] == d>>1 + 1
@enforce osize[region] == d>>1 + 1
osize[region] = d
Y = Array($Tr, osize...)
p = Plan(X, Y, region, ESTIMATE | PRESERVE_INPUT, NO_TIMELIMIT)
Expand All @@ -578,7 +578,7 @@ for (Tr,Tc) in ((:Float32,:Complex64),(:Float64,:Complex128))
function brfftd(X::StridedArray{$Tc}, d::Integer, region)
d1 = region[1]
osize = [size(X)...]
@assert osize[d1] == d>>1 + 1
@enforce osize[d1] == d>>1 + 1
osize[d1] = d
Y = Array($Tr, osize...)
p = Plan(X, Y, region, ESTIMATE, NO_TIMELIMIT)
Expand All @@ -603,7 +603,7 @@ for (Tr,Tc) in ((:Float32,:Complex64),(:Float64,:Complex128))
function plan_brfft(X::StridedArray{$Tc}, d::Integer, region::Integer,
flags::Unsigned, tlim::Real)
osize = [size(X)...]
@assert osize[region] == d>>1 + 1
@enforce osize[region] == d>>1 + 1
osize[region] = d
Y = Array($Tr, osize...)
p = Plan(X, Y, region, flags | PRESERVE_INPUT, tlim)
Expand All @@ -622,7 +622,7 @@ for (Tr,Tc) in ((:Float32,:Complex64),(:Float64,:Complex128))
end
d1 = region[1]
osize = [size(X)...]
@assert osize[d1] == d>>1 + 1
@enforce osize[d1] == d>>1 + 1
osize[d1] = d
Y = Array($Tr, osize...)
X = copy(X)
Expand Down
20 changes: 10 additions & 10 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ function abstract_interpret(e::ANY, vtypes, sv::StaticVarInfo)
if isa(lhs,SymbolNode)
lhs = lhs.name
end
assert(isa(lhs,Symbol))
enforce(isa(lhs,Symbol))
return StateUpdate(lhs, t, vtypes)
elseif is(e.head,:call) || is(e.head,:call1)
abstract_eval(e, vtypes, sv)
Expand Down Expand Up @@ -1300,7 +1300,7 @@ function typeinf(linfo::LambdaStaticData,atypes::Tuple,sparams::Tuple, def, cop)

args = f_argnames(ast)
la = length(args)
assert(is(ast.head,:lambda))
enforce(is(ast.head,:lambda))
locals = (ast.args[2][1])::Array{Any,1}
vars = [args, locals]
body = (ast.args[3].args)::Array{Any,1}
Expand Down Expand Up @@ -1521,7 +1521,7 @@ function typeinf(linfo::LambdaStaticData,atypes::Tuple,sparams::Tuple, def, cop)
fulltree = type_annotate(ast, s, sv, frame.result, args)

if !rec
@assert fulltree.args[3].head === :body
@enforce fulltree.args[3].head === :body
fulltree.args[3] = inlining_pass(fulltree.args[3], sv, fulltree)[1]
# inlining can add variables
sv.vars = append_any(f_argnames(fulltree), fulltree.args[2][1])
Expand Down Expand Up @@ -1721,7 +1721,7 @@ function sym_replace(e::ANY, from1, from2, to1, to2)
# remove_redundant_temp_vars can only handle Symbols
# on the LHS of assignments, so we make sure not to put
# something else there
@assert length(e.args) == 2
@enforce length(e.args) == 2
e2 = _sym_repl(e.args[1]::Symbol, from1, from2, to1, to2, e.args[1]::Symbol)
if isa(e2, SymbolNode)
e2 = e2.name
Expand Down Expand Up @@ -2206,11 +2206,11 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
# inference step shortened our call args list, even
# though we have too many arguments to actually
# call this function
@assert isvarargtype(atypes[na])
@enforce isvarargtype(atypes[na])
return NF
end

@assert na == length(argexprs)
@enforce na == length(argexprs)

if needcopy
body = astcopy(body)
Expand Down Expand Up @@ -2288,7 +2288,7 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
if isvarargtype(methitype)
methitype = (methitype::Vararg).parameters[1]
else
@assert i==nm
@enforce i==nm
end
end
end
Expand Down Expand Up @@ -2424,8 +2424,8 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
push!(body.args, Expr(:call,:error,"fatal error in type inference"))
lastexpr = nothing
else
@assert isa(lastexpr,Expr) "inference.jl:1774"
@assert is(lastexpr.head,:return) "inference.jl:1775"
@enforce isa(lastexpr,Expr) "inference.jl:1774"
@enforce is(lastexpr.head,:return) "inference.jl:1775"
end
for a in body.args
push!(stmts, a)
Expand Down Expand Up @@ -2817,7 +2817,7 @@ function local_typeof(v, varinfo)
return typ
end
end
@assert false "v not in varinfo"
@enforce false "v not in varinfo"
end

occurs_undef(var, expr) =
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ function diag{Tv}(L::CholmodFactor{Tv})
xv = L.x
for j in 1:L.c.n
jj = c0[j]+1
assert(r0[jj] == j-1)
enforce(r0[jj] == j-1)
res[j] = xv[jj]
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ end
# f{S,T}(x::S, y::T) = x+y
# f{S,T}(y::T, x::S) = f(x, y)
macro commutative(myexpr)
@assert myexpr.head===:(=) || myexpr.head===:function #Make sure it is a function definition
@enforce myexpr.head===:(=) || myexpr.head===:function #Make sure it is a function definition
y = copy(myexpr.args[1].args[2:end])
reverse!(y)
reversed_call = Expr(:(=), Expr(:call,myexpr.args[1].args[1],y...), myexpr.args[1])
Expand Down
6 changes: 3 additions & 3 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ end
function add_workers(pg::ProcessGroup, ws::Array{Any,1})
# NOTE: currently only node 1 can add new nodes, since nobody else
# has the full list of address:port
assert(LPROC.id == 1)
enforce(LPROC.id == 1)
for w in ws
w.id = get_next_pid()
register_worker(w)
Expand Down Expand Up @@ -1544,7 +1544,7 @@ function timedwait(testcb::Function, secs::Float64; pollint::Float64=0.1)
end

function interrupt(pid::Integer)
assert(myid() == 1)
enforce(myid() == 1)
w = map_pid_wrkr[pid]
if isa(w, Worker)
w.manage(w.id, w.config, :interrupt)
Expand All @@ -1553,7 +1553,7 @@ end
interrupt(pids::Integer...) = interrupt([pids...])

function interrupt(pids::AbstractVector=workers())
assert(myid() == 1)
enforce(myid() == 1)
@sync begin
for pid in pids
@async interrupt(pid)
Expand Down
8 changes: 4 additions & 4 deletions base/osutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ function is_unix(os::Symbol)
end

function _os_test(qm,ex,test)
@assert qm == :?
@assert isa(ex,Expr)
@assert ex.head == :(:)
@assert length(ex.args) == 2
@enforce qm == :?
@enforce isa(ex,Expr)
@enforce ex.head == :(:)
@enforce length(ex.args) == 2
if test
return esc(ex.args[1])
else
Expand Down
Loading

0 comments on commit 366e00a

Please sign in to comment.