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

RFC: Or patterns, i.e `Foo(Bar(x) | Baz(x))` #2535

Merged
merged 26 commits into from Oct 7, 2018

Conversation

@Centril
Contributor

Centril commented Aug 30, 2018

🖼️ Rendered

Tracking issue

📝 Summary

Allow | to be arbitrarily nested within a pattern such that Some(A(0) | B(1 | 2)) becomes a valid pattern.

💖 Thanks

To @Nemo157 and @joshtriplett for reviewing the draft version of this RFC.

@Centril Centril added the T-lang label Aug 30, 2018

@Centril Centril self-assigned this Aug 30, 2018

@joshtriplett

This comment has been minimized.

Show comment
Hide comment
@joshtriplett

joshtriplett Aug 30, 2018

Member

👍 I've wanted this for a while, and I look forward to using it.

Member

joshtriplett commented Aug 30, 2018

👍 I've wanted this for a while, and I look forward to using it.

@Centril Centril referenced this pull request Aug 31, 2018

Open

Or patterns #43

@whataloadofwhat

This comment has been minimized.

Show comment
Hide comment
@whataloadofwhat

whataloadofwhat Sep 2, 2018

I think a drawback is that it may be suprising with bitwise or

let x = Some(1 | 2);
match x {
    Some(1 | 2) => (), // won't happen, but intuition says it would
    _ => panic!()
}

... But I guess that it's kind of already an issue right now anyway

let x = 1 | 2;
match x {
    1 | 2 => (), // won't happen
    _ => panic!()
}

whataloadofwhat commented Sep 2, 2018

I think a drawback is that it may be suprising with bitwise or

let x = Some(1 | 2);
match x {
    Some(1 | 2) => (), // won't happen, but intuition says it would
    _ => panic!()
}

... But I guess that it's kind of already an issue right now anyway

let x = 1 | 2;
match x {
    1 | 2 => (), // won't happen
    _ => panic!()
}
@joshtriplett

This comment has been minimized.

Show comment
Hide comment
@joshtriplett

joshtriplett Sep 2, 2018

Member

@whataloadofwhat Yeah, | already doesn't mean bitwise or in the context of patterns.

Member

joshtriplett commented Sep 2, 2018

@whataloadofwhat Yeah, | already doesn't mean bitwise or in the context of patterns.

Show outdated Hide outdated text/0000-or-patterns.md Outdated
@SimonSapin

This comment has been minimized.

Show comment
Hide comment
@SimonSapin

SimonSapin Sep 5, 2018

Contributor

+1 on the general goal.

What is the impact on pat fragment in macro_rules? Unlike other some other combinations forbidden by RFC 550 Macro future-proofing, | is allowed to follow a pattern today and this runs without asserting in Rust 1.28.0:

macro_rules! foo {
    ($a: pat) => { false };
    ($a: pat | $b: pat) => { true };
}

fn main() {
    assert!(foo!(Some(_) | None));
}
Contributor

SimonSapin commented Sep 5, 2018

+1 on the general goal.

What is the impact on pat fragment in macro_rules? Unlike other some other combinations forbidden by RFC 550 Macro future-proofing, | is allowed to follow a pattern today and this runs without asserting in Rust 1.28.0:

macro_rules! foo {
    ($a: pat) => { false };
    ($a: pat | $b: pat) => { true };
}

fn main() {
    assert!(foo!(Some(_) | None));
}
@SimonSapin

This comment has been minimized.

Show comment
Hide comment
@SimonSapin

SimonSapin Sep 5, 2018

Contributor

RFC 550’s "Edit history" section links to #1336.

Contributor

SimonSapin commented Sep 5, 2018

RFC 550’s "Edit history" section links to #1336.

@mark-i-m

This comment has been minimized.

Show comment
Hide comment
@mark-i-m

mark-i-m Sep 5, 2018

Contributor

We can and have disallowed some characters in macros before (e.g. ? in separators), but we would need to act fast.

Contributor

mark-i-m commented Sep 5, 2018

We can and have disallowed some characters in macros before (e.g. ? in separators), but we would need to act fast.

@SimonSapin

This comment has been minimized.

Show comment
Hide comment
@SimonSapin

SimonSapin Sep 5, 2018

Contributor

“Fast” as in before May 2015?

Contributor

SimonSapin commented Sep 5, 2018

“Fast” as in before May 2015?

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb

eddyb Sep 20, 2018

Member

cc @varkor Do you think it would be possible to fully integrate this pattern syntax into the match/pattern checker, now that you've read through, and understood, the paper?

EDIT: the context here is rust-lang/rust#50912 and the paper is http://moscova.inria.fr/~maranget/papers/warn/index.html.

Member

eddyb commented Sep 20, 2018

cc @varkor Do you think it would be possible to fully integrate this pattern syntax into the match/pattern checker, now that you've read through, and understood, the paper?

EDIT: the context here is rust-lang/rust#50912 and the paper is http://moscova.inria.fr/~maranget/papers/warn/index.html.

@varkor

This comment has been minimized.

Show comment
Hide comment
@varkor

varkor Sep 20, 2018

Contributor

@eddyb: yes, the original algorithm already describes how to handle or-patterns — it just hasn't been implemented in Rust's version of it yet. I'd venture that implementing this should be fairly straightforward.

Contributor

varkor commented Sep 20, 2018

@eddyb: yes, the original algorithm already describes how to handle or-patterns — it just hasn't been implemented in Rust's version of it yet. I'd venture that implementing this should be fairly straightforward.

Show outdated Hide outdated text/0000-or-patterns.md Outdated
@rfcbot

This comment has been minimized.

Show comment
Hide comment
@rfcbot

rfcbot Sep 25, 2018

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

rfcbot commented Sep 25, 2018

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

@durka

This comment has been minimized.

Show comment
Hide comment
@durka

durka Oct 2, 2018

Contributor

Was serious consideration given to avoiding breakage by requiring parentheses when doing alternation at the top level? It was not added to the alternatives or drawbacks section... and this isn't the first RFC to ignore/dismiss macro breakage. Other than that I strongly approve.

Contributor

durka commented Oct 2, 2018

Was serious consideration given to avoiding breakage by requiring parentheses when doing alternation at the top level? It was not added to the alternatives or drawbacks section... and this isn't the first RFC to ignore/dismiss macro breakage. Other than that I strongly approve.

@Centril

This comment has been minimized.

Show comment
Hide comment
@Centril

Centril Oct 2, 2018

Contributor

@durka

It was not added to the alternatives or drawbacks section.

It wasn't added to the alternatives because it is the proposal ;)

Finally, pat macro fragment specifiers will also match the pat<no_top_alt> production as opposed to pat<allow_top_alt>.

Contributor

Centril commented Oct 2, 2018

@durka

It was not added to the alternatives or drawbacks section.

It wasn't added to the alternatives because it is the proposal ;)

Finally, pat macro fragment specifiers will also match the pat<no_top_alt> production as opposed to pat<allow_top_alt>.

@durka

This comment has been minimized.

Show comment
Hide comment
@durka

durka Oct 2, 2018

Contributor

Oh, okay great! That should be in the guide-level docs as well, not just buried in the grammar definition.

Contributor

durka commented Oct 2, 2018

Oh, okay great! That should be in the guide-level docs as well, not just buried in the grammar definition.

@rfcbot

This comment has been minimized.

Show comment
Hide comment
@rfcbot

rfcbot Oct 5, 2018

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

rfcbot commented Oct 5, 2018

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

@Centril Centril merged commit df1ea7e into rust-lang:master Oct 7, 2018

@Centril

This comment has been minimized.

Show comment
Hide comment
@Centril

Centril Oct 7, 2018

Contributor

Huzzah! This RFC has been merged!

Tracking issue: rust-lang/rust#54883

Contributor

Centril commented Oct 7, 2018

Huzzah! This RFC has been merged!

Tracking issue: rust-lang/rust#54883

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment