Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Or patterns, i.e `Foo(Bar(x) | Baz(x))` #2535
Conversation
Centril
added some commits
Apr 9, 2018
Centril
added
the
T-lang
label
Aug 30, 2018
Centril
self-assigned this
Aug 30, 2018
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
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!()
} |
This comment has been minimized.
This comment has been minimized.
|
@whataloadofwhat Yeah, |
scottmcm
reviewed
Sep 3, 2018
|
|
||
| The only real choice that we do have to make is whether the new addition to the | ||
| pattern grammar should be `pat : .. | pat "|" pat ;` or if it instead should be | ||
| `pat : .. | "|"? pat "|" pat ;`. We have chosen the former for 4 reasons: |
This comment has been minimized.
This comment has been minimized.
scottmcm
Sep 3, 2018
Member
nit: pat ::= "|"? pat "|" pat; is definitely bad, since it would allow patterns like | | A | B | C via the interpretation | (| A | B) | C. So I'm absolutely in favour of the current plan.
This comment has been minimized.
This comment has been minimized.
|
+1 on the general goal. What is the impact on macro_rules! foo {
($a: pat) => { false };
($a: pat | $b: pat) => { true };
}
fn main() {
assert!(foo!(Some(_) | None));
} |
This comment has been minimized.
This comment has been minimized.
|
RFC 550’s "Edit history" section links to #1336. |
This comment has been minimized.
This comment has been minimized.
|
We can and have disallowed some characters in macros before (e.g. |
This comment has been minimized.
This comment has been minimized.
|
“Fast” as in before May 2015? |
scottmcm
reviewed
Sep 25, 2018
| + the type inferred for `p` does not unify with the type inferred for `q`, or | ||
| + the same set of bindings are not introduced in `p` and `q`, or | ||
| + the type of any two bindings with the same name in `p` and `q` do not unify | ||
| with respect to types or binding modes. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Centril
Sep 25, 2018
Author
Contributor
Summary from Discord conversation:
...So according to the coercion rules, if match is a coercion site, then the following should work according to the type coercion rules:
fn main() {
enum E<'a> {
A(&'a u8),
B(&'a mut u8),
}
match unimplemented!() {
E::A(x) | E::B(x) => {
let x: &u8 = x;
}
}
}However; match is not coercion site, so possibly the coercions would apply for let.
For now, I think we can be conservative and require an exact match and relax it later if need be / it makes sense.
In relation to type ascription, one could reasonably expect that E::A(x : τ) | E::B(x: τ) should work if x on both sides are implicitly coercible to τ but I don't think we need to specify that interaction just yet.
This comment has been minimized.
This comment has been minimized.
Centril
Sep 25, 2018
Author
Contributor
Amended the RFC with a clarification according to the above notes.
This comment has been minimized.
This comment has been minimized.
eddyb
Sep 26, 2018
Member
match wouldn't be the coercion site here, the individual patterns would need to be. Currently, the x binding there is the same variable (initialized on two possible disjoint execution paths), and can't possibly have two types (but we can relax that in the future).
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Sep 25, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Sep 25, 2018
This was referenced Sep 26, 2018
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Oh, okay great! That should be in the guide-level docs as well, not just buried in the grammar definition. |
rfcbot
added
the
finished-final-comment-period
label
Oct 5, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Oct 5, 2018
|
The final comment period, with a disposition to merge, as per the review above, is now complete. |
rfcbot
removed
the
final-comment-period
label
Oct 5, 2018
Centril
referenced this pull request
Oct 7, 2018
Open
Tracking issue for RFC 2535, 2530, 2175, "Or patterns, i.e `Foo(Bar(x) | Baz(x))`" #54883
Centril
merged commit df1ea7e
into
rust-lang:master
Oct 7, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC has been merged! Tracking issue: rust-lang/rust#54883 |
Centril commentedAug 30, 2018
•
edited
Allow
|to be arbitrarily nested within a pattern such thatSome(A(0) | B(1 | 2))becomes a valid pattern.To @Nemo157 and @joshtriplett for reviewing the draft version of this RFC.