-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: introduce check macro that can be disabled with a flag #25576
Conversation
+1 to documenting that |
Agree with @JeffBezanson: this is exactly what |
I think something like this is useful, and agree that merging with To make this more dynamic (and making the most of #265) I suggest the following mechanism: module DebugAsserts
export debug_asserts, @dassert
function debug_asserts(m::Module, dodebug::Bool)
m.eval(:(_debug_enabled() = $dodebug))
nothing
end
# Like @assert, but only active in debug mode
macro dassert(exs...)
if !isdefined(__module__, :_debug_enabled)
eval(__module__, :(_debug_enabled() = false))
end
quote
if $__module__._debug_enabled()
@assert $(map(esc,exs)...)
end
end
end
end This way, if you need to turn debug assertions on in a release build you can do that, and vice versa (the release and debug builds can change the default value returned by Edit - related discussion here: https://discourse.julialang.org/t/defensive-programming-assert/8383/11. To be clear, I'm not suggesting we use the name |
Why merge them? They have different purposes as @KristofferC has described. Some you always want to do and some you want to do only in debug mode. |
There's a rather strong convention from other languages that the thing called There's at least four or so prior issues/prs discussing a release-mode version of something like assert. #7732 suggests |
For those just write a normal check with no macro. |
I think most people are after the pretty printing that comes with |
Indeed, it is very simple to use. No need to come up with an error message and no need to write |
Ok, if people want a |
This is a new feature; doesn't need to be triaged now. |
What we need to decide is if we should introduce a new macro with the same effect as the current |
Lets deprecate |
How about calling it Or perhaps, since this will generally be used for checking argument validity, calling it |
Going through a cycle of renaming and un-renaming assert is too annoying. That punishes people who are using it correctly. We should just document it better. |
If you are using it correctly, the point is to just do |
It seems like we may want to just:
|
Ok, so only thing for 1.0 is to fixup the docs for |
It might be good for |
It would be strange if the default mode was the optimized one and typing (also -1 on having an |
No longer relevant for this issue, just to mention another possible name: |
I really like the general idea of having three modes: Then, something like " Possibly, one could also do very slow things like integer overflow checking (AFAIK signed integer overflows on addition are currently UB?). Possibly emit checks for other UB. |
TLDR: for triage: Add docs to
@assert
that the block might not be executed, so don't e.g. use it for needed side effects?It is common for packages to roll their own way of enabling a debug mode. This is typically done using a global const
DEBUG = true / false
which one has to edit in the source file to set. Another option is to use an ENV variable and start julia with package precompilation disabled. These are non ideal because manually editing files is awkward and doesn't really scale when you want to run multiple packages in debug mode and then swap back to release mode, and disabling package precompilation increases load time every time you want to debug.In #10614 a suggestion was made for a
@check
macro completely disables a code block when not running in debug mode / alt. only enables it when running in "optimize" mode. This PR introduces a--debug
flag that enables the@check
macro (i.e. code in a@check block is then not removed). Furthermore, running with
--debugchanges the precompilation path (adds a
debug` directory) so that it works with packages that enables precompilation.The macro is currently not usable in base because any result of it will be cached into the sysimg. For it to work in Base, we probably need a second debug sysimg that is chosen based on the
--debug
flag.Note that
@check
is not really intended for debug logging (the logging macros already work for that) but for situations where verifying certain properties might be too expensive to do normally. Such an example might be to check that the input tosearchsorted
actually is sorted.This PR is mostly to spur some discussion if this is something we want, if this type of implementation makes sense, etc. It is not aimed at 1.0 but one thing we need to decide for 1.0 is if to instead repurpose the current
@assert
macro to be what@check
is in this PR. There is currently no way to remove@assert
but if we want to use@assert
for this in 1.1 we need to document that@assert
s can be removed. Therefore, putting the triage label on it.