Skip to content

Commit b7071f2

Browse files
committed
extract parse_dyn_ty
1 parent edb7b96 commit b7071f2

File tree

1 file changed

+27
-13
lines changed
  • src/librustc_parse/parser

1 file changed

+27
-13
lines changed

src/librustc_parse/parser/ty.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,14 @@ impl<'a> Parser<'a> {
108108
}
109109
} else if self.eat_keyword(kw::Impl) {
110110
self.parse_impl_ty(&mut impl_dyn_multi)?
111-
} else if self.check_keyword(kw::Dyn) &&
112-
(self.token.span.rust_2018() ||
113-
self.look_ahead(1, |t| t.can_begin_bound() &&
114-
!can_continue_type_after_non_fn_ident(t))) {
115-
self.bump(); // `dyn`
116-
// Always parse bounds greedily for better error recovery.
117-
let bounds = self.parse_generic_bounds(None)?;
118-
impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
119-
TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn)
120-
} else if self.check(&token::Question) ||
121-
self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus()) {
111+
} else if self.is_explicit_dyn_type() {
112+
self.parse_dyn_ty(&mut impl_dyn_multi)?
113+
} else if self.check(&token::Question)
114+
|| self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus())
115+
{
122116
// Bound list (trait object type)
123-
TyKind::TraitObject(self.parse_generic_bounds_common(allow_plus, None)?,
124-
TraitObjectSyntax::None)
117+
let bounds = self.parse_generic_bounds_common(allow_plus, None)?;
118+
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
125119
} else if self.eat_lt() {
126120
// Qualified path
127121
let (qself, path) = self.parse_qpath(PathStyle::Type)?;
@@ -316,6 +310,26 @@ impl<'a> Parser<'a> {
316310
Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
317311
}
318312

313+
/// Is a `dyn B0 + ... + Bn` type allowed here?
314+
fn is_explicit_dyn_type(&mut self) -> bool {
315+
self.check_keyword(kw::Dyn)
316+
&& (self.token.span.rust_2018()
317+
|| self.look_ahead(1, |t| {
318+
t.can_begin_bound() && !can_continue_type_after_non_fn_ident(t)
319+
}))
320+
}
321+
322+
/// Parses a `dyn B0 + ... + Bn` type.
323+
///
324+
/// Note that this does *not* parse bare trait objects.
325+
fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
326+
self.bump(); // `dyn`
327+
// Always parse bounds greedily for better error recovery.
328+
let bounds = self.parse_generic_bounds(None)?;
329+
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
330+
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
331+
}
332+
319333
pub(super) fn parse_generic_bounds(&mut self,
320334
colon_span: Option<Span>) -> PResult<'a, GenericBounds> {
321335
self.parse_generic_bounds_common(true, colon_span)

0 commit comments

Comments
 (0)