-
-
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
omit assert under --optimize
; introduce@check
macro?
#10614
Comments
+1
|
+1 to removing asserts, but I'd prefer |
People like just writing |
Fair enough. But what would we throw? ArgumentError? ErrorException? |
|
|
assert now throws an |
It will probably have to be an ErrorException. You don't want a special type of exception just for using |
I'm struggling to see the value in this proposal, does this mean that "library" code will have assertions which can not be turned off and user level code will have |
Read it again; |
Being able to turn |
The alternative is people misusing |
We already have one concept for turning off correctness tests with the |
Ref #7799. To elaborate: Bounds checking and assertions are both checks that are written in order to ensure that if the caller of a function makes a mistake, you get an exception instead of data corruption. Some code might be written with the assuption that assertions/bounds checks are on, while other code does the checking before calling the function. A global flag to turn theese sanity checks on and off, is great for debugging, and performance testing. For real programs though, I'd want to have them on by default, and use some sort of signaling at the call site to disable them for specific regions where I can verify that the conditions are true and that the performance is critical. I'm not qualified to talk about implementation, but the only way I can really see this work is to have a Boolean parameter to all functions that tells if it was called in a assertion free context (or a bitmask style enum to have finer granularity, but I can't imagine when you'd want to disable bounds checks, but not assertions) |
I like the idea of conceptual unification with I find myself writing my own assertion macro for input checking that lets me know what went wrong, what the input values were if they were unexpected, sort of part way towards |
I wonder if |
It's not. |
Though, just because it's not turned off right now, doesn't mean it's safe to use for checking. It's explicitly allowed to no longer be run with any new version, so relying on |
Any progress on this? The advantage of disabling Something like function fun(a::AbstractArray{<:Number})
i = findfirst(iseven, a)
@assert !isnothing(i)
return a[i] + 1
end would loose the @assert i >= 1
@assert i <= length(a)
a[length(a) - i + 1] # no @inbounds needed and the bounds checking. |
I don't think this has anything to do with the |
Sure, that's my point exactly. I want something that
For performance critical code, it is the caller who makes sure the function is used correctly. Yet, I'd like to be able to tell the compiler that it's fine to assume that the asserted condition holds. Also, being able to check asserts during "debug builds" (whatever that may turn out to be) is a nice plus. Asserting / checking and throwing leaves all the code in the assembly. Sure, most of it is cold. But the branches are there and code locality isn't optimal either. For that use case, having a macro |
People often use
@assert
in their code to check that functions are being used correctly. It's typical in other systems to eliminate assertions under higher optimization levels. Should we do that under--optimize
? If assertions are ignored (compiled to no-ops) but also used to check correct usage – as opposed to checking the correctness of internal logic – then this can accidentally introduce a dangerous lack of argument checking. So perhaps we should introduce a@check
macro that is not eliminated by--optimize
and is intended for checking usage as opposed to internal correctness.The text was updated successfully, but these errors were encountered: