Skip to content

Commit

Permalink
Rollup merge of #85678 - lukas-code:matches2021, r=dtolnay
Browse files Browse the repository at this point in the history
fix `matches!` and `assert_matches!` on edition 2021

Previously this code failed to compile on edition 2021. [(Playground)](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=53960f2f051f641777b9e458da747707)
```rust
fn main() {
    matches!((), ());
}
```
```
   Compiling playground v0.0.1 (/playground)
error: `$pattern:pat` may be followed by `|`, which is not allowed for `pat` fragments
    |
    = note: allowed there are: `=>`, `,`, `=`, `if` or `in`

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.
```
  • Loading branch information
Dylan-DPC authored May 26, 2021
2 parents 12ab323 + 824c743 commit 3c2a709
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
#![feature(no_coverage)] // rust-lang/rust#84605
#![feature(int_error_matching)]
#![deny(unsafe_op_in_unsafe_fn)]
#![deny(or_patterns_back_compat)]

// allow using `core::` in intra-doc links
#[allow(unused_extern_crates)]
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ macro_rules! assert_ne {
#[unstable(feature = "assert_matches", issue = "82775")]
#[allow_internal_unstable(core_panic)]
macro_rules! assert_matches {
($left:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => ({
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand All @@ -150,7 +150,7 @@ macro_rules! assert_matches {
}
}
});
($left:expr, $( $pattern:pat )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
($left:expr, $( $pattern:pat_param )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
Expand Down Expand Up @@ -315,7 +315,7 @@ macro_rules! debug_assert_matches {
#[macro_export]
#[stable(feature = "matches_macro", since = "1.42.0")]
macro_rules! matches {
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => {
($expression:expr, $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => true,
_ => false
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/matches2021.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// run-pass
// edition:2021
// compile-flags: -Zunstable-options

// regression test for https://github.com/rust-lang/rust/pull/85678

#![feature(assert_matches)]

fn main() {
assert!(matches!((), ()));
assert_matches!((), ());
}

0 comments on commit 3c2a709

Please sign in to comment.