about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 22:46:17 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 23:05:04 +0200
commit3a405421e7c1437416e225ea8d2f0fdfb501df7b (patch)
tree807ad098380196eaf00764e90854aaa7486277ff /src
parenta9ef8592e47808539ffd9237c22ce5518aa7b188 (diff)
downloadrust-3a405421e7c1437416e225ea8d2f0fdfb501df7b.tar.gz
rust-3a405421e7c1437416e225ea8d2f0fdfb501df7b.zip
parse_top_pat: silence leading vert gating sometimes.
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/parse/parser/pat.rs18
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.rs8
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.stderr12
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.rs8
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.stderr12
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns-leading.stderr12
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns.rs2
-rw-r--r--src/test/ui/or-patterns/feature-gate-or_patterns.stderr44
8 files changed, 80 insertions, 36 deletions
diff --git a/src/libsyntax/parse/parser/pat.rs b/src/libsyntax/parse/parser/pat.rs
index c168d033781..724edbcfaed 100644
--- a/src/libsyntax/parse/parser/pat.rs
+++ b/src/libsyntax/parse/parser/pat.rs
@@ -46,11 +46,23 @@ impl<'a> Parser<'a> {
     /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
     pub(super) fn parse_top_pat(&mut self, gate_or: GateOr) -> PResult<'a, P<Pat>> {
         // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
-        if self.eat_or_separator() && gate_or == GateOr::Yes {
-            self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
+        let gated_leading_vert = self.eat_or_separator() && gate_or == GateOr::Yes;
+
+        // Parse the possibly-or-pattern.
+        let pat = self.parse_pat_with_or(None, gate_or, TopLevel::Yes)?;
+
+        // If we parsed a leading `|` which should be gated,
+        // and no other gated or-pattern has been parsed thus far,
+        // then we should really gate the leading `|`.
+        // This complicated procedure is done purely for diagnostics UX.
+        if gated_leading_vert {
+            let mut or_pattern_spans = self.sess.gated_spans.or_patterns.borrow_mut();
+            if or_pattern_spans.is_empty() {
+                or_pattern_spans.push(self.prev_span);
+            }
         }
 
-        self.parse_pat_with_or(None, gate_or, TopLevel::Yes)
+        Ok(pat)
     }
 
     /// Parses a pattern, that may be a or-pattern (e.g. `Foo | Bar` in `Some(Foo | Bar)`).
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.rs b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.rs
new file mode 100644
index 00000000000..de8e1bba557
--- /dev/null
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.rs
@@ -0,0 +1,8 @@
+// Test feature gating for a sole leading `|` in `let`.
+
+fn main() {}
+
+#[cfg(FALSE)]
+fn gated_leading_vert_in_let() {
+    for | A in 0 {} //~ ERROR or-patterns syntax is experimental
+}
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.stderr b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.stderr
new file mode 100644
index 00000000000..83804d564f3
--- /dev/null
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-for.stderr
@@ -0,0 +1,12 @@
+error[E0658]: or-patterns syntax is experimental
+  --> $DIR/feature-gate-or_patterns-leading-for.rs:7:11
+   |
+LL |     for | A in 0 {}
+   |           ^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/54883
+   = help: add `#![feature(or_patterns)]` to the crate attributes to enable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.rs b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.rs
new file mode 100644
index 00000000000..a4ea4e25d86
--- /dev/null
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.rs
@@ -0,0 +1,8 @@
+// Test feature gating for a sole leading `|` in `let`.
+
+fn main() {}
+
+#[cfg(FALSE)]
+fn gated_leading_vert_in_let() {
+    let | A; //~ ERROR or-patterns syntax is experimental
+}
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.stderr b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.stderr
new file mode 100644
index 00000000000..f7954ad7a8c
--- /dev/null
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns-leading-let.stderr
@@ -0,0 +1,12 @@
+error[E0658]: or-patterns syntax is experimental
+  --> $DIR/feature-gate-or_patterns-leading-let.rs:7:11
+   |
+LL |     let | A;
+   |           ^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/54883
+   = help: add `#![feature(or_patterns)]` to the crate attributes to enable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns-leading.stderr b/src/test/ui/or-patterns/feature-gate-or_patterns-leading.stderr
new file mode 100644
index 00000000000..8b18082fca7
--- /dev/null
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns-leading.stderr
@@ -0,0 +1,12 @@
+error[E0658]: or-patterns syntax is experimental
+  --> $DIR/feature-gate-or_patterns-leading.rs:7:11
+   |
+LL |     let | A;
+   |           ^
+   |
+   = note: for more information, see https://github.com/rust-lang/rust/issues/54883
+   = help: add `#![feature(or_patterns)]` to the crate attributes to enable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns.rs b/src/test/ui/or-patterns/feature-gate-or_patterns.rs
index 560db20e668..e638838147a 100644
--- a/src/test/ui/or-patterns/feature-gate-or_patterns.rs
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns.rs
@@ -26,10 +26,8 @@ fn or_patterns() {
     // Gated:
 
     let | A | B; //~ ERROR or-patterns syntax is experimental
-    //~^ ERROR or-patterns syntax is experimental
     let A | B; //~ ERROR or-patterns syntax is experimental
     for | A | B in 0 {} //~ ERROR or-patterns syntax is experimental
-    //~^ ERROR or-patterns syntax is experimental
     for A | B in 0 {} //~ ERROR or-patterns syntax is experimental
     fn fun((A | B): _) {} //~ ERROR or-patterns syntax is experimental
     let _ = |(A | B): u8| (); //~ ERROR or-patterns syntax is experimental
diff --git a/src/test/ui/or-patterns/feature-gate-or_patterns.stderr b/src/test/ui/or-patterns/feature-gate-or_patterns.stderr
index e2abfbfd525..aae6644dac2 100644
--- a/src/test/ui/or-patterns/feature-gate-or_patterns.stderr
+++ b/src/test/ui/or-patterns/feature-gate-or_patterns.stderr
@@ -8,15 +8,6 @@ LL |         Some(0 | 1 | 2) => {}
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:28:9
-   |
-LL |     let | A | B;
-   |         ^
-   |
-   = note: for more information, see https://github.com/rust-lang/rust/issues/54883
-   = help: add `#![feature(or_patterns)]` to the crate attributes to enable
-
-error[E0658]: or-patterns syntax is experimental
   --> $DIR/feature-gate-or_patterns.rs:28:11
    |
 LL |     let | A | B;
@@ -26,7 +17,7 @@ LL |     let | A | B;
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:30:9
+  --> $DIR/feature-gate-or_patterns.rs:29:9
    |
 LL |     let A | B;
    |         ^^^^^
@@ -35,16 +26,7 @@ LL |     let A | B;
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:31:9
-   |
-LL |     for | A | B in 0 {}
-   |         ^
-   |
-   = note: for more information, see https://github.com/rust-lang/rust/issues/54883
-   = help: add `#![feature(or_patterns)]` to the crate attributes to enable
-
-error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:31:11
+  --> $DIR/feature-gate-or_patterns.rs:30:11
    |
 LL |     for | A | B in 0 {}
    |           ^^^^^
@@ -53,7 +35,7 @@ LL |     for | A | B in 0 {}
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:33:9
+  --> $DIR/feature-gate-or_patterns.rs:31:9
    |
 LL |     for A | B in 0 {}
    |         ^^^^^
@@ -62,7 +44,7 @@ LL |     for A | B in 0 {}
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:34:13
+  --> $DIR/feature-gate-or_patterns.rs:32:13
    |
 LL |     fn fun((A | B): _) {}
    |             ^^^^^
@@ -71,7 +53,7 @@ LL |     fn fun((A | B): _) {}
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:35:15
+  --> $DIR/feature-gate-or_patterns.rs:33:15
    |
 LL |     let _ = |(A | B): u8| ();
    |               ^^^^^
@@ -80,7 +62,7 @@ LL |     let _ = |(A | B): u8| ();
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:36:10
+  --> $DIR/feature-gate-or_patterns.rs:34:10
    |
 LL |     let (A | B);
    |          ^^^^^
@@ -89,7 +71,7 @@ LL |     let (A | B);
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:37:10
+  --> $DIR/feature-gate-or_patterns.rs:35:10
    |
 LL |     let (A | B,);
    |          ^^^^^
@@ -98,7 +80,7 @@ LL |     let (A | B,);
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:38:11
+  --> $DIR/feature-gate-or_patterns.rs:36:11
    |
 LL |     let A(B | C);
    |           ^^^^^
@@ -107,7 +89,7 @@ LL |     let A(B | C);
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:39:14
+  --> $DIR/feature-gate-or_patterns.rs:37:14
    |
 LL |     let E::V(B | C);
    |              ^^^^^
@@ -116,7 +98,7 @@ LL |     let E::V(B | C);
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:40:17
+  --> $DIR/feature-gate-or_patterns.rs:38:17
    |
 LL |     let S { f1: B | C, f2 };
    |                 ^^^^^
@@ -125,7 +107,7 @@ LL |     let S { f1: B | C, f2 };
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:41:20
+  --> $DIR/feature-gate-or_patterns.rs:39:20
    |
 LL |     let E::V { f1: B | C, f2 };
    |                    ^^^^^
@@ -134,7 +116,7 @@ LL |     let E::V { f1: B | C, f2 };
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
 error[E0658]: or-patterns syntax is experimental
-  --> $DIR/feature-gate-or_patterns.rs:42:10
+  --> $DIR/feature-gate-or_patterns.rs:40:10
    |
 LL |     let [A | B];
    |          ^^^^^
@@ -187,6 +169,6 @@ LL | accept_pat!([p | q]);
    = note: for more information, see https://github.com/rust-lang/rust/issues/54883
    = help: add `#![feature(or_patterns)]` to the crate attributes to enable
 
-error: aborting due to 21 previous errors
+error: aborting due to 19 previous errors
 
 For more information about this error, try `rustc --explain E0658`.