Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document that asserts might not be running at certain optimization levels #25610

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ Deprecated or removed
* `rand(t::Tuple{Vararg{Int}})` is deprecated in favor of `rand(Float64, t)` or `rand(t...)`;
`rand(::Tuple)` will have another meaning in the future ([#25429], [#25278]).

* The `assert` function (and `@assert` macro) have been documented that they are not guaranteed to run under various optimization levels and should therefore not be used to e.g. verify passwords.

* `ObjectIdDict` has been deprecated in favor of `IdDict{Any,Any}` ([#25210]).

* `gc` and `gc_enable` have been deprecated in favor of `GC.gc` and `GC.enable` ([#25616]).
Expand Down
14 changes: 14 additions & 0 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ systemerror(p, b::Bool; extrainfo=nothing) = b ? throw(Main.Base.SystemError(str

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())

Expand All @@ -122,6 +129,13 @@ assert(x) = x ? nothing : throw(AssertionError())
Throw an [`AssertionError`](@ref) if `cond` is `false`. Preferred syntax for writing assertions.
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),
nor should side effects needed for the function to work correctly
be used inside of asserts.

# Examples
```jldoctest
julia> @assert iseven(3) "3 is an odd number!"
Expand Down