-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Tracking Issue for assert_matches #82775
Comments
Several implementations of
This is currently not supported by Adding it would allow things like: let thing = assert_matches!(thing, Some(x) => x); and assert_matches!(thing, Some((x, y)) => {
assert_eq!(x.a, 10);
assert_eq!(y.b(), 20);
}); |
As a (almost) daily user of the I.e. asserting a certain sequence of messages with a mocked context and assuring the messages have expected content (which by design may not impl type A = u8;
struct B(u8);
struct C(u8);
struct D(u8);
enum Msgs {
Specific { a: A, b: B, c: C, d: D},
// snip
}
assert_matches!(rx.next().unwrap(), Msgs::Specific { a, b: B(b), .. } => { assert_eq!(a, b); })
assert_matches!(rx.next().unwrap(), Msgs::Specific { b, c: C(c), .. } => { assert_eq!(b, c); })
.. so for this the extension with |
In cases like that, the assert_matches!(rx.next(), Some(Msgs::Specific { a, b: B(b), .. }) if a == b); |
While your approach is valid for simple cases, I disagree for the general use case. This is not very ergonomic as soon as there are various sub patterns, that have to be asserted against transformed struct member values or larger enums with multiple elements common for messaging enums. assert_matches!(rx.next(), Some(Msgs::Specific { a, b: B(b), .. }) => {
assert_eq!(a, b);
let container = transform($container);
if container.is_empty() {
assert!(..);
} else {
assert!(..);
}
});
.. It could still be expressed with boolean expression combinators using the current impl, yet that adds a lot visual complexity that imho should not exist in a test case. Note that I am not saying the impl is bad or anything :) - I would very much like to see |
@rust-lang/libs Ping for opinions. Should |
It's an interesting extension. At first it looked a bit odd to me, but after ruminating on it a bit, it does seem fairly natural to me. In terms of moving forward:
|
This makes me think of the proposal to add This seems very similar, and I feel like the arguments are the same. If we have I personally feel that in both cases the |
We could require the |
For me as well; that was not clear to me at all. |
A separate macro that always requires a |
Well if we're agreed on leaving it as is then we can head towards stabilising it... |
@drahnr in #82775 (comment)
Your example can be rewritten to a match guard that always returns true except when it panics, like this. -assert_matches!(rx.next(), Some(Msgs::Specific { a, b: B(b), .. }) => {
+assert_matches!(rx.next(), Some(Msgs::Specific { a, b: B(b), .. }) if ({
assert_eq!(a, b);
let container = transform($container);
if container.is_empty() {
assert!(/* … */);
} else {
assert!(/* … */);
}
-});
+ true
+})); Definitely non-obvious when you don't know about it, but hardly worse to write if you are adding to an existing test suite with the pattern everywhere. |
Re-reading the comments, I fully support the current impl and withdraw my previous concerns. |
I would prefer the macro to expand as Does this sound reasonable? Would |
@fee1-dead that would be very confusing to me. An assertion should assert and nothing more imo |
I think there's a lot of value in having |
Looks like we reached consensus on not having the @rfcbot merge |
Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I'm going to add some meta-commentary because I think this conversation has gotten to a point that's not productive.
Ultimately it's up to the libs-api team to decide this. I'm sure they will consider the newest arguments put forth. |
This comment pretty much nails it. assert_matches!() should be equivalent to "assert!(matches!( .. ))" else people will be surprised if it acts differently. |
My two cents are that the precedence of |
Why are you, guys, arguing in the P.S. And if |
Please carefully read what I've previously written. I think there is a meaningful difference. Here are the two core sources of confusion:
|
Everyone, please stop, the same points are just being rehashed. |
3 years and no stabilization for something as simple as this? 😦 |
Tracking issues have a tendency to become unmanageable. Please open a dedicated new issue and link to this issue for absolutely any topics you want to discuss or have questions about. See rust-lang/compiler-team#739 for details and discussions on this prospective policy. |
I came looking for Come to think of it, maybe what I want would be better named as |
Hello, there are two unchecked boxes in the top message:
The first is done since that PR is merged? And the second one also looks done? Does this mean that |
#121732 is the PR (by me) that improved the documentation which also included "Mention this macro in the documentation of matches!()". |
So are we able to tick those two remaining boxes then? Thanks. |
Seems weird that the current stable documentation recommends using a
Wouldn't it be better to have released the documentation change to stable together with the stabilization of |
@tyilo honestly based on this comment a year ago where the libs team voted yes to stabilize this, with the only requirement being that an appropriate module would be found to put it in, I had assumed it was stabilized already. When writing that PR it never occurred to me to check that it was actually stabilized. I agree the link to |
It has the finished-final-comment-period label. Do we just need a PR to stabilize it? If so, I could make one. |
I think rust-lang/rfcs#3573 fully subsumes So, I think rust-lang/rfcs#3573 should be implemented and then |
RFC 3573 is controversial, as you can see from the nature of the opposition to the idea in the discussion... though given that "detracts from Rust's features being orthogonal and well-factored now that the v1.0 stability promise is blocking removing something else" is one of the complaints, I suppose it is the responsible thing to ask to block stabilizing this until a ruling is made on that. |
Wouldn't
As opposed to
|
There was #110382 It'd seem
|
I wonder if it would be better for this to be part of the generic asset functionality. So you could write something like: assert!(matches!(x, Some(_))) and get a useful error message. Or maybe some assert-specific syntax like: assert!(match x => Some(_)) or assert!(let Some(_) = x) |
Closes rust-lang#82775 This is a revive of rust-lang#120234, with the suggested move from the public assert_matches module to macros. This necessitates the rename of the internal macros modules to core_macros and std_macros respectively.
Closes rust-lang#82775 This is a revive of rust-lang#120234, with the suggested move from the public assert_matches module to macros. This necessitates the rename of the internal macros modules to core_macros and std_macros respectively.
Feature gate:
#![feature(assert_matches)]
This is a tracking issue for the
assert_matches!()
anddebug_assert_matches!()
macros.Public API
Steps / History
std::assert_matches::assert_matches
to avoid breakage: Is there a gentler way to land the assert_matches macro? #82913must_use
message onis_none
(per Add messages toOption
's andResult
'smust_use
annotation foris_*
#62431 (review))matches!()
Unresolved Questions
=> expr
syntax?The text was updated successfully, but these errors were encountered: