about summary refs log tree commit diff
path: root/src/libsyntax/parse/parser
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/libsyntax/parse/parser
parenta9ef8592e47808539ffd9237c22ce5518aa7b188 (diff)
downloadrust-3a405421e7c1437416e225ea8d2f0fdfb501df7b.tar.gz
rust-3a405421e7c1437416e225ea8d2f0fdfb501df7b.zip
parse_top_pat: silence leading vert gating sometimes.
Diffstat (limited to 'src/libsyntax/parse/parser')
-rw-r--r--src/libsyntax/parse/parser/pat.rs18
1 files changed, 15 insertions, 3 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)`).