Skip to content

Tracking issue for the matches! macro #65721

Description

@SimonSapin

#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));

Activity

  1. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-libs-api[DEPRECATED; DO NOT USE]
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Oct 23, 2019
  2. added
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on Oct 24, 2019
  3. SimonSapin commented on Oct 25, 2019

    @SimonSapin
    ContributorAuthor

    This caused a regression in html5ever because the prelude macro becomes ambiguous with a macro that was brought into scope by a glob import: servo/html5ever#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.

  4. eddyb commented on Oct 25, 2019

    @eddyb
    Contributor
  5. petrochenkov commented on Oct 25, 2019

    @petrochenkov
    Contributor

    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.)

  6. SimonSapin commented on Oct 25, 2019

    @SimonSapin
    ContributorAuthor

    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’t find this again. Maybe it was only for traits?

  7. petrochenkov commented on Oct 25, 2019

    @petrochenkov
    Contributor

    Yeah, that was for method resolution.

  8. SimonSapin commented on Oct 25, 2019

    @SimonSapin
    ContributorAuthor

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

  9. SimonSapin commented on Dec 16, 2019

    @SimonSapin
    ContributorAuthor

    The 1.42 cycle starts soon. Any objection?

    @rfcbot fcp merge

  10. rfcbot commented on Dec 16, 2019

    @rfcbot

    Team member @SimonSapin 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.

  11. 5 remaining items

  12. removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Dec 16, 2019
  13. rfcbot commented on Dec 16, 2019

    @rfcbot

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

  14. rfcbot commented on Dec 26, 2019

    @rfcbot

    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.

  15. removed
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    on Dec 26, 2019
  16. added a commit that references this issue on Dec 27, 2019
  17. added a commit that references this issue on Dec 27, 2019
  18. jonhoo commented on Dec 28, 2019

    @jonhoo
    Contributor

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

  19. tesuji commented on Dec 28, 2019

    @tesuji
    Contributor

    Already on the stabilized PR: #67659

  20. SimonSapin commented on Dec 28, 2019

    @SimonSapin
    ContributorAuthor

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

  21. danielhenrymantilla commented on Mar 6, 2020

    @danielhenrymantilla
    Contributor

    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 => ...
    }
  22. added a commit that references this issue on Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions