Rename assert to enforce, add debug_assert#12108
Rename assert to enforce, add debug_assert#12108huonw wants to merge 4 commits intorust-lang:masterfrom
Conversation
|
debug_assert_eq!() is rather a mouthful. Google uses ASSERT() and CHECK() for checks that should be done in debug and release builds respectively. How about assert!() and check!() macros? |
|
Something like that is fine by me long term; but I think we need to have a (reasonably long) period without an (I do think |
|
logging uses |
|
I like |
|
I would prefer |
|
I personally feel very strongly that we need a macro with the exact name of Right now we don't have a debug assertion, and I think that it's a problem. In my opinion the world should look like:
I don't really have a strong preference as to which bucket |
|
I'd vote for |
|
Some languages allow you to disable asserts in non-debug builds, but not all. And in those languages that do, you're not required to disable them. I'm personally in favor of keeping |
|
Please let's consider this very carefully. |
|
Updated to use e.g. assert_level!(std::logging::DEBUG, super_expensive());
assert_level!(std::logging::WARN, reasonably_cheap()); |
|
My favored course of action is that |
|
I agree with @alexcrichton. Also, asserts that are dynamically activated with |
|
I agree with @alexcrichton's proposal:
except that I would prefer the name |
|
@alexcrichton: At least in the world of C and Python, an assertion is not expected to be there in a release build. I don't see a need for Rust to deviate from the expectation of many users rather than giving it a less overloaded name like If it's on by default in a release build, then it's not a debugging feature. Code will be built to depend on the failure being thrown and caught. It's definitely not what I think of as an assertion. |
|
@kballard: Which languages call it |
|
@thestinger Well, even C's |
|
Eiffel uses Python will remove |
|
I personally always find it ambiguous whether I would rather have a macro which clearly says in its name that it will be optimized out willy nilly, so you shouldn't rely on it. I don't personally think that enforce/assert is a good split because it's not clear which one gets optimized away and which one doesn't (I think it's the same problem with assert/check). The debug_assert/assert split is quite clear, but the debug version is a tad bit long. Using dassert/assert it's not super clear that dassert == debug assert. (just my current thoughts) |
|
@thestinger I only see |
|
If the code is correct (and my understanding is correct), the asserts in the destructors will never be triggered. |
|
1+ for Having used the language for over a year, I still hesitate to actually use In my opinion a name like It seems logical to me to have, in the longterm, the split between always-on, semantic relevant A bit nonsenical example: fn foo(i: uint) -> uint {
enforce!(i < 10, "The Algorithm only works for i values 0-9");
/* complicated algorithm */
assert!(result <= 1024); // This should always be true if the algorithm is correct
result
} |
|
+1 to @Kimundi's suggestion. There is long-standing precedent from many languages that assertions are for enforcing static properties you believe will never fail -- and hence it is safe to remove them. I see no reason to change the name assert. The biggest question is if we should compile asserts out by default under I know that the choice to have assertions always be compiled in was a conscious choice. |
|
I see everything I said has already been said and with more precision. Oh well, I should always read the full comment threads. =) Anyway, +22 to @brson's point of "let's not land this without much consideration". |
|
FWIW, modulo the name of (I just realised that I hadn't updated the PR description, which may've been a source of confusion; updated now.) |
|
|
|
I'm in favor of this pull request as-is, but would also be in favor of That said, I'd prefer if it used |
|
I have put this on the next meeting agenda. |
|
In the meeting this week we decided to do the following:
|
|
Updated to use a publically exported |
|
|
|
@huonw has it been decided how assert interacts with tests after this change? |
|
As a naive user, I'd expect |
|
@brson tests use |
|
As a naive user, I want Did we get rid of |
|
|
|
I still think |
|
I like Also, @kballard, if it's not clear, the removal of |
|
accidental nomination... |
|
Ping? |
|
I'm happy to update/rebase/change this, but I don't think we have a sensible resolution yet? |
|
So, just to put it out there, I've been floating this idea around: instead of allocating a macro name for the "always on" assert, one could follow Perl's example: I had thought we would need to put in a special case into our "unused value" lint to deal with this, but apparently it works without any warnings right out of the box: (True, this requires the user to spell out what the condition was that they were checking. But that might be a pro, since one probably wants to control how much their code gets bloated by the output strings for this construct.) Anyway, this pattern means we could just make |
|
That's so cute I can't decide if it's a good idea or not. |
|
I'm with @glaebhoerl; that seems cute and workable... but it's possibly a little too cute. I guess what's now |
|
Oh, that raises a downside: |
|
@huonw right: what is now Though perhaps we could tweak rustc for the special case of |
|
Closing due to inactivity. I think that with our new RFC process that this should likely go through an RFC before getting another implementation. This appears to be a fairly contentious topic, but I would very much like to see this resolved for 1.0. |
|
@alexcrichton To make sure this does not slip through the cracks, should we keep an open issue for this with a "backward compat" flag, or whatever is appropriate to have it reconsidered before 1.0? |
minor: Record snippet config errors
Do not suggest `bool::then()` and `bool::then_some` in `const` contexts Fix rust-lang#12103 changelog: [`if_then_some_else_none`]: Do not trigger in `const` contexts
Currently
assert!is used to enforce invariants, not just doing sanity checks that get removed in release builds (unlike traditional asserts). This changeset renames them tofail_unless(andfail_unless_eqtoenforce_eq), and addsdebug_assertanddebug_assert_eqfor assertions that are active unless--cfg ndebug.Closes #12049.
General points:
debug_assertetc. are never automatically removed, since not even-Osets--cfg ndebug(I did not add any use of them to the codebase, and am happy to remove the addition if people think we should work out a better arrangement)