]> git.lizzy.rs Git - rust.git/commitdiff
extract parse_generic_ty_bound
authorMazdak Farrokhzad <twingoow@gmail.com>
Sun, 8 Dec 2019 10:19:24 +0000 (11:19 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 21 Dec 2019 18:20:41 +0000 (19:20 +0100)
src/librustc_parse/parser/ty.rs

index 2c362294ad0c9216446c422f840203018c43f2f5..a5ce46e9700f9e16324ae49768bb6f41002129df 100644 (file)
@@ -431,8 +431,6 @@ fn can_begin_bound(&mut self) -> bool {
     /// ```
     /// BOUND = TY_BOUND | LT_BOUND
     /// LT_BOUND = LIFETIME (e.g., `'a`)
-    /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
-    /// TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g., `?for<'a: 'b> m::Trait<'a>`)
     /// ```
     fn parse_generic_bound(
         &mut self,
@@ -454,22 +452,11 @@ fn parse_generic_bound(
             }
             Ok(Ok(bound))
         } else {
-            let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
-            let path = self.parse_path(PathStyle::Type)?;
-            if has_parens {
-                self.expect(&token::CloseDelim(token::Paren))?;
-            }
-            let poly_span = lo.to(self.prev_span);
+            let (poly_span, bound) = self.parse_generic_ty_bound(lo, has_parens, question)?;
             if is_negative {
                 Ok(Err(last_plus_span.or(colon_span).map(|sp| sp.to(poly_span))))
             } else {
-                let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
-                let modifier = if question.is_some() {
-                    TraitBoundModifier::Maybe
-                } else {
-                    TraitBoundModifier::None
-                };
-                Ok(Ok(GenericBound::Trait(poly_trait, modifier)))
+                Ok(Ok(bound))
             }
         }
     }
@@ -501,6 +488,28 @@ fn recover_paren_lifetime(&mut self, lo: Span, inner_lo: Span) -> PResult<'a, ()
         Ok(())
     }
 
+    /// Parses a type bound according to:
+    /// ```
+    /// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
+    /// TY_BOUND_NOPAREN = [?] [for<LT_PARAM_DEFS>] SIMPLE_PATH (e.g., `?for<'a: 'b> m::Trait<'a>`)
+    /// ```
+    fn parse_generic_ty_bound(
+        &mut self,
+        lo: Span,
+        has_parens: bool,
+        question: Option<Span>,
+    ) -> PResult<'a, (Span, GenericBound)> {
+        let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
+        let path = self.parse_path(PathStyle::Type)?;
+        if has_parens {
+            self.expect(&token::CloseDelim(token::Paren))?;
+        }
+        let poly_span = lo.to(self.prev_span);
+        let poly_trait = PolyTraitRef::new(lifetime_defs, path, poly_span);
+        let modifier = question.map_or(TraitBoundModifier::None, |_| TraitBoundModifier::Maybe);
+        Ok((poly_span, GenericBound::Trait(poly_trait, modifier)))
+    }
+
     pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
         if self.eat_keyword(kw::For) {
             self.expect_lt()?;