Rust: Tracking issue for the `matches!` macro

Created on 23 Oct 2019  路  16Comments  路  Source: rust-lang/rust

https://github.com/rust-lang/rust/pull/65479 adds this macro to the prelude:

#[macro_export]
macro_rules! matches {
    ($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )?) => {
        match $expression {
            $( $pattern )|+ $( if $guard )? => true,
            _ => false
        }
    }
}

Example usage:

let foo = 'f';
assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));

let bar = Some(4);
assert!(matches!(bar, Some(x) if x > 2));
A-macros B-unstable C-tracking-issue T-libs disposition-merge finished-final-comment-period requires-nightly

Most helpful comment

Should we also add a assert_matches! macro at the same time that Debug-prints the value if the match fails?

All 16 comments

This caused a regression in html5ever because the prelude macro becomes ambiguous with a macro that was brought into scope by a glob import: https://github.com/servo/html5ever/pull/402

@rust-lang/lang, could we make that situation not an error? It seems that we could resolve the ambiguity in favor of the glob import since it is "more local" to the rest of the code in the module.

cc @petrochenkov

could we make that situation not an error?

For macros (and imports) it's hard to do.

To remove the ambiguity error we need to prove that with any expansion order and any import resolution order matches materializes from the use mac::* glob "not later" than from prelude.

Otherwise we'll get code that compiles today but not tomorrow depending on random implementation details. (Making e.g. things like this impossible.)

I have a memory of a change made to treat differently during name resolution items that are unstable, so that new standard library addition can (at first) warn instead of error when they conflict with existing code. However I couldn鈥檛 find this again. Maybe it was only for traits?

Yeah, that was for method resolution.

Would a similar special case for unstable prelude items make sense?

The 1.42 cycle starts soon. Any objection?

@rfcbot fcp merge

Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members:

  • [x] @Amanieu
  • [ ] @KodrAus
  • [x] @SimonSapin
  • [x] @alexcrichton
  • [x] @dtolnay
  • [x] @sfackler
  • [x] @withoutboats

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.

Should we also add a assert_matches! macro at the same time that Debug-prints the value if the match fails?

I personally find that one less useful, but feel free to also propose it in a PR.

:bell: This is now entering its final comment period, as per the review above. :bell:

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

How do we feel about giving this the "relnotes" tag?

Already on the stabilized PR: #67659

@jonhoo I thought RELEASES.md listed all new(ly-stabilized) APIs? I added relnotes to the stabilization PR on that basis. That鈥檚 what the label is for, right? This is not to say this should necessarily be mentioned in the release blog post.

Could matches! be enhanced to also accept a leading |?

matches!(expr,
    | SomeFirstPattern
    | SomeOtherPattern
    ...
);

This would be consistent with the syntax of Rust accepting it where fallible patterns are accepted:

match expr {
    | SomeFirstPattern $(=> ...)?
    | SomeOtherPattern => ...
}
Was this page helpful?
0 / 5 - 0 ratings