-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Remove the assert_eq!
macro in favor of special-casing a check for equality in the assert!
macro
#16633
Comments
Hey, random question, but it seems Lines 65 to 97 in 43f040d
|
Github accidentally submitted this issue (twice...), hold on commenting until I finish it! :P |
@sinistersnare, that's a good question. At a quick glance it looks totally redundant. |
cc @gankro |
Prior IRC discussion: https://botbot.me/mozilla/rust-internals/2014-08-20/?msg=20089459&page=3 |
An open question is whether to preserve the current (and rather surprising behavior) of |
Upon further consideration I think that the current two-way equality check in |
This must be implemented as a syntax extension, because macros defined with |
I'd be interested in having the double-equality-check be relegated to a different special form like |
@pczarn, I'm fine with this becoming a syntax extension. Heading off a combinatorial explosion of macros is a win in my book. |
@gankro, I'd be tempted to just tell those people to do Also, wasn't there some upcoming changes to traits that could end up making |
@bstrie that's kind of annoying when |
Also of course, I think it would be great to be able to do this for basically all of the comparison operators. In fact, if it could be written in a general enough way to basically dump the values of all the variables in the expression, regardless of the expression, that would be amazing. If it could be done in a way that it outputs intermediate results of compound expressions, I would basically have the author's child. |
@gankro, I agree that it's definitely annoying if you happen to need it. I still question whether anyone happens to need it, and intuitively I'm guessing that most people who use |
@bstrie Fair enough. |
I second the use of combing them into one macro, instead of two. I was also not aware of the extra equality check. |
Can all other comparison operators be special-cased too? It's kind of annoying when I want a check like: |
See issue #16625 why assert! must be made simpler. In effect two separate assert! are needed; and efficient version for implementing library APIs and a convenient and verbose version for writing tests. |
In light of rust-lang/rfcs#550 (implemented in #20563), this is unfortunately no longer possible. The |
Does "not possible today" suggest "maybe possible eventually", or does this change preclude such a design from ever working? |
To allow this design I believe we would have to add assert!(foo == bar == baz) For that macro invocation, what's |
It can be implemented as a syntax extension. However, the change to
How could you detect whether a compound expression's type implements There's an issue with intermediate results of compound expressions like these: assert!(consume(foo) == bar) In this example, |
We currently have four macros for runtime assertions:
assert!
,assert_eq!
,debug_assert!
, anddebug_assert_eq!
. See https://github.com/rust-lang/rust/blob/master/src/libcore/macros.rs#L50 for their definitions, which are all quite simple. The reason for having the_eq
form for bothassert!
anddebug_assert!
is because equality checks are the most common kinds of assertions (assert_eq!
is found 7100 times in the Rust repo, vs 4600 forassert!
) and that by encoding the intent of the assertion we can produce a better error message, like so:However, I don't see a reason for the existence of this extra macro. I propose that we leverage our existing
macro_rules!
facilities to special-case the formassert!(a == b)
such that it implements the current logic ofassert_eq!
. In non-tested copy-pasted code:Does anyone see any problems with this?
The text was updated successfully, but these errors were encountered: