Skip to content

Commit 3a40542

Browse files
committed
parse_top_pat: silence leading vert gating sometimes.
1 parent a9ef859 commit 3a40542

8 files changed

+80
-36
lines changed

src/libsyntax/parse/parser/pat.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,23 @@ impl<'a> Parser<'a> {
4646
/// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
4747
pub(super) fn parse_top_pat(&mut self, gate_or: GateOr) -> PResult<'a, P<Pat>> {
4848
// Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
49-
if self.eat_or_separator() && gate_or == GateOr::Yes {
50-
self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
49+
let gated_leading_vert = self.eat_or_separator() && gate_or == GateOr::Yes;
50+
51+
// Parse the possibly-or-pattern.
52+
let pat = self.parse_pat_with_or(None, gate_or, TopLevel::Yes)?;
53+
54+
// If we parsed a leading `|` which should be gated,
55+
// and no other gated or-pattern has been parsed thus far,
56+
// then we should really gate the leading `|`.
57+
// This complicated procedure is done purely for diagnostics UX.
58+
if gated_leading_vert {
59+
let mut or_pattern_spans = self.sess.gated_spans.or_patterns.borrow_mut();
60+
if or_pattern_spans.is_empty() {
61+
or_pattern_spans.push(self.prev_span);
62+
}
5163
}
5264

53-
self.parse_pat_with_or(None, gate_or, TopLevel::Yes)
65+
Ok(pat)
5466
}
5567

5668
/// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test feature gating for a sole leading `|` in `let`.
2+
3+
fn main() {}
4+
5+
#[cfg(FALSE)]
6+
fn gated_leading_vert_in_let() {
7+
for | A in 0 {} //~ ERROR or-patterns syntax is experimental
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: or-patterns syntax is experimental
2+
--> $DIR/feature-gate-or_patterns-leading-for.rs:7:11
3+
|
4+
LL | for | A in 0 {}
5+
| ^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
8+
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Test feature gating for a sole leading `|` in `let`.
2+
3+
fn main() {}
4+
5+
#[cfg(FALSE)]
6+
fn gated_leading_vert_in_let() {
7+
let | A; //~ ERROR or-patterns syntax is experimental
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: or-patterns syntax is experimental
2+
--> $DIR/feature-gate-or_patterns-leading-let.rs:7:11
3+
|
4+
LL | let | A;
5+
| ^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
8+
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: or-patterns syntax is experimental
2+
--> $DIR/feature-gate-or_patterns-leading.rs:7:11
3+
|
4+
LL | let | A;
5+
| ^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
8+
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/or-patterns/feature-gate-or_patterns.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ fn or_patterns() {
2626
// Gated:
2727

2828
let | A | B; //~ ERROR or-patterns syntax is experimental
29-
//~^ ERROR or-patterns syntax is experimental
3029
let A | B; //~ ERROR or-patterns syntax is experimental
3130
for | A | B in 0 {} //~ ERROR or-patterns syntax is experimental
32-
//~^ ERROR or-patterns syntax is experimental
3331
for A | B in 0 {} //~ ERROR or-patterns syntax is experimental
3432
fn fun((A | B): _) {} //~ ERROR or-patterns syntax is experimental
3533
let _ = |(A | B): u8| (); //~ ERROR or-patterns syntax is experimental

src/test/ui/or-patterns/feature-gate-or_patterns.stderr

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ LL | Some(0 | 1 | 2) => {}
77
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
88
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
99

10-
error[E0658]: or-patterns syntax is experimental
11-
--> $DIR/feature-gate-or_patterns.rs:28:9
12-
|
13-
LL | let | A | B;
14-
| ^
15-
|
16-
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
17-
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
18-
1910
error[E0658]: or-patterns syntax is experimental
2011
--> $DIR/feature-gate-or_patterns.rs:28:11
2112
|
@@ -26,7 +17,7 @@ LL | let | A | B;
2617
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
2718

2819
error[E0658]: or-patterns syntax is experimental
29-
--> $DIR/feature-gate-or_patterns.rs:30:9
20+
--> $DIR/feature-gate-or_patterns.rs:29:9
3021
|
3122
LL | let A | B;
3223
| ^^^^^
@@ -35,16 +26,7 @@ LL | let A | B;
3526
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
3627

3728
error[E0658]: or-patterns syntax is experimental
38-
--> $DIR/feature-gate-or_patterns.rs:31:9
39-
|
40-
LL | for | A | B in 0 {}
41-
| ^
42-
|
43-
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
44-
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
45-
46-
error[E0658]: or-patterns syntax is experimental
47-
--> $DIR/feature-gate-or_patterns.rs:31:11
29+
--> $DIR/feature-gate-or_patterns.rs:30:11
4830
|
4931
LL | for | A | B in 0 {}
5032
| ^^^^^
@@ -53,7 +35,7 @@ LL | for | A | B in 0 {}
5335
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
5436

5537
error[E0658]: or-patterns syntax is experimental
56-
--> $DIR/feature-gate-or_patterns.rs:33:9
38+
--> $DIR/feature-gate-or_patterns.rs:31:9
5739
|
5840
LL | for A | B in 0 {}
5941
| ^^^^^
@@ -62,7 +44,7 @@ LL | for A | B in 0 {}
6244
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
6345

6446
error[E0658]: or-patterns syntax is experimental
65-
--> $DIR/feature-gate-or_patterns.rs:34:13
47+
--> $DIR/feature-gate-or_patterns.rs:32:13
6648
|
6749
LL | fn fun((A | B): _) {}
6850
| ^^^^^
@@ -71,7 +53,7 @@ LL | fn fun((A | B): _) {}
7153
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
7254

7355
error[E0658]: or-patterns syntax is experimental
74-
--> $DIR/feature-gate-or_patterns.rs:35:15
56+
--> $DIR/feature-gate-or_patterns.rs:33:15
7557
|
7658
LL | let _ = |(A | B): u8| ();
7759
| ^^^^^
@@ -80,7 +62,7 @@ LL | let _ = |(A | B): u8| ();
8062
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
8163

8264
error[E0658]: or-patterns syntax is experimental
83-
--> $DIR/feature-gate-or_patterns.rs:36:10
65+
--> $DIR/feature-gate-or_patterns.rs:34:10
8466
|
8567
LL | let (A | B);
8668
| ^^^^^
@@ -89,7 +71,7 @@ LL | let (A | B);
8971
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
9072

9173
error[E0658]: or-patterns syntax is experimental
92-
--> $DIR/feature-gate-or_patterns.rs:37:10
74+
--> $DIR/feature-gate-or_patterns.rs:35:10
9375
|
9476
LL | let (A | B,);
9577
| ^^^^^
@@ -98,7 +80,7 @@ LL | let (A | B,);
9880
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
9981

10082
error[E0658]: or-patterns syntax is experimental
101-
--> $DIR/feature-gate-or_patterns.rs:38:11
83+
--> $DIR/feature-gate-or_patterns.rs:36:11
10284
|
10385
LL | let A(B | C);
10486
| ^^^^^
@@ -107,7 +89,7 @@ LL | let A(B | C);
10789
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
10890

10991
error[E0658]: or-patterns syntax is experimental
110-
--> $DIR/feature-gate-or_patterns.rs:39:14
92+
--> $DIR/feature-gate-or_patterns.rs:37:14
11193
|
11294
LL | let E::V(B | C);
11395
| ^^^^^
@@ -116,7 +98,7 @@ LL | let E::V(B | C);
11698
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
11799

118100
error[E0658]: or-patterns syntax is experimental
119-
--> $DIR/feature-gate-or_patterns.rs:40:17
101+
--> $DIR/feature-gate-or_patterns.rs:38:17
120102
|
121103
LL | let S { f1: B | C, f2 };
122104
| ^^^^^
@@ -125,7 +107,7 @@ LL | let S { f1: B | C, f2 };
125107
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
126108

127109
error[E0658]: or-patterns syntax is experimental
128-
--> $DIR/feature-gate-or_patterns.rs:41:20
110+
--> $DIR/feature-gate-or_patterns.rs:39:20
129111
|
130112
LL | let E::V { f1: B | C, f2 };
131113
| ^^^^^
@@ -134,7 +116,7 @@ LL | let E::V { f1: B | C, f2 };
134116
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
135117

136118
error[E0658]: or-patterns syntax is experimental
137-
--> $DIR/feature-gate-or_patterns.rs:42:10
119+
--> $DIR/feature-gate-or_patterns.rs:40:10
138120
|
139121
LL | let [A | B];
140122
| ^^^^^
@@ -187,6 +169,6 @@ LL | accept_pat!([p | q]);
187169
= note: for more information, see https://github.com/rust-lang/rust/issues/54883
188170
= help: add `#![feature(or_patterns)]` to the crate attributes to enable
189171

190-
error: aborting due to 21 previous errors
172+
error: aborting due to 19 previous errors
191173

192174
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)