Skip to content
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 the `matches!` macro #65721

Closed
SimonSapin opened this issue Oct 23, 2019 · 15 comments · Fixed by #67659
Closed

Tracking issue for the `matches!` macro #65721

SimonSapin opened this issue Oct 23, 2019 · 15 comments · Fixed by #67659

Comments

@SimonSapin
Copy link
Contributor

@SimonSapin SimonSapin commented Oct 23, 2019

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

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Oct 25, 2019

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.

@eddyb

This comment has been minimized.

Copy link
Member

@eddyb eddyb commented Oct 25, 2019

@petrochenkov

This comment has been minimized.

Copy link
Contributor

@petrochenkov petrochenkov commented Oct 25, 2019

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

@SimonSapin

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Oct 25, 2019

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?

@petrochenkov

This comment has been minimized.

Copy link
Contributor

@petrochenkov petrochenkov commented Oct 25, 2019

Yeah, that was for method resolution.

@SimonSapin

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Oct 25, 2019

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

@SimonSapin

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Dec 16, 2019

The 1.42 cycle starts soon. Any objection?

@rfcbot fcp merge

@rfcbot

This comment has been minimized.

Copy link

@rfcbot rfcbot commented Dec 16, 2019

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.

@jonhoo

This comment has been minimized.

Copy link
Contributor

@jonhoo jonhoo commented Dec 16, 2019

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

@SimonSapin

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Dec 16, 2019

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

@rfcbot

This comment has been minimized.

Copy link

@rfcbot rfcbot commented Dec 16, 2019

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

@rfcbot

This comment has been minimized.

Copy link

@rfcbot rfcbot commented Dec 26, 2019

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.

oli-obk added a commit to oli-obk/rust that referenced this issue Dec 27, 2019
Stabilize the `matches!` macro

Fixes rust-lang#65721

FCP: rust-lang#65721 (comment)
@bors bors closed this in 1c572a2 Dec 28, 2019
@jonhoo

This comment has been minimized.

Copy link
Contributor

@jonhoo jonhoo commented Dec 28, 2019

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

@lzutao

This comment has been minimized.

Copy link
Contributor

@lzutao lzutao commented Dec 28, 2019

Already on the stabilized PR: #67659

@SimonSapin

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Dec 28, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.