2021-01-22 18:28:08 +00:00
|
|
|
use super::{ForceCollect, Parser, TrailingToken};
|
2019-08-11 18:44:09 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::token;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::{
|
|
|
|
self as ast, Attribute, GenericBounds, GenericParam, GenericParamKind, WhereClause,
|
|
|
|
};
|
2019-12-05 05:38:06 +00:00
|
|
|
use rustc_errors::PResult;
|
2020-12-31 17:32:06 +00:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-08-11 18:44:09 +00:00
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
|
|
|
|
///
|
2020-04-18 16:39:40 +00:00
|
|
|
/// ```text
|
2019-08-11 18:44:09 +00:00
|
|
|
/// BOUND = LT_BOUND (e.g., `'a`)
|
|
|
|
/// ```
|
|
|
|
fn parse_lt_param_bounds(&mut self) -> GenericBounds {
|
|
|
|
let mut lifetimes = Vec::new();
|
|
|
|
while self.check_lifetime() {
|
|
|
|
lifetimes.push(ast::GenericBound::Outlives(self.expect_lifetime()));
|
|
|
|
|
|
|
|
if !self.eat_plus() {
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lifetimes
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Matches `typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?`.
|
2019-12-22 22:42:04 +00:00
|
|
|
fn parse_ty_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, GenericParam> {
|
2019-08-11 18:44:09 +00:00
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
|
|
|
|
// Parse optional colon and param bounds.
|
|
|
|
let bounds = if self.eat(&token::Colon) {
|
2020-02-29 11:56:15 +00:00
|
|
|
self.parse_generic_bounds(Some(self.prev_token.span))?
|
2019-08-11 18:44:09 +00:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
|
2019-08-11 18:44:09 +00:00
|
|
|
|
|
|
|
Ok(GenericParam {
|
|
|
|
ident,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
attrs: preceding_attrs.into(),
|
|
|
|
bounds,
|
2019-12-22 22:42:04 +00:00
|
|
|
kind: GenericParamKind::Type { default },
|
|
|
|
is_placeholder: false,
|
2019-08-11 18:44:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:56:28 +00:00
|
|
|
crate fn parse_const_param(
|
|
|
|
&mut self,
|
|
|
|
preceding_attrs: Vec<Attribute>,
|
|
|
|
) -> PResult<'a, GenericParam> {
|
2020-06-21 22:49:56 +00:00
|
|
|
let const_span = self.token.span;
|
2019-09-21 16:58:17 +00:00
|
|
|
|
2019-08-11 18:44:09 +00:00
|
|
|
self.expect_keyword(kw::Const)?;
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
let ty = self.parse_ty()?;
|
|
|
|
|
2020-12-31 17:32:06 +00:00
|
|
|
// Parse optional const generics default value, taking care of feature gating the spans
|
|
|
|
// with the unstable syntax mechanism.
|
|
|
|
let default = if self.eat(&token::Eq) {
|
|
|
|
// The gated span goes from the `=` to the end of the const argument that follows (and
|
|
|
|
// which could be a block expression).
|
|
|
|
let start = self.prev_token.span;
|
|
|
|
let const_arg = self.parse_const_arg()?;
|
|
|
|
let span = start.to(const_arg.value.span);
|
|
|
|
self.sess.gated_spans.gate(sym::const_generics_defaults, span);
|
|
|
|
Some(const_arg)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-12-31 00:58:27 +00:00
|
|
|
|
2019-08-11 18:44:09 +00:00
|
|
|
Ok(GenericParam {
|
|
|
|
ident,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
attrs: preceding_attrs.into(),
|
|
|
|
bounds: Vec::new(),
|
2020-12-31 00:58:27 +00:00
|
|
|
kind: GenericParamKind::Const { ty, kw_span: const_span, default },
|
2019-12-22 22:42:04 +00:00
|
|
|
is_placeholder: false,
|
2019-08-11 18:44:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
|
|
|
|
/// a trailing comma and erroneous trailing attributes.
|
2019-10-08 07:35:34 +00:00
|
|
|
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, Vec<ast::GenericParam>> {
|
2019-08-11 18:44:09 +00:00
|
|
|
let mut params = Vec::new();
|
2021-01-22 18:28:08 +00:00
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2019-08-11 18:44:09 +00:00
|
|
|
let attrs = self.parse_outer_attributes()?;
|
2021-01-22 18:28:08 +00:00
|
|
|
let param =
|
|
|
|
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
|
2021-10-18 16:31:23 +00:00
|
|
|
if this.eat_keyword_noexpect(kw::SelfUpper) {
|
|
|
|
// `Self` as a generic param is invalid. Here we emit the diagnostic and continue parsing
|
|
|
|
// as if `Self` never existed.
|
|
|
|
this.struct_span_err(
|
|
|
|
this.prev_token.span,
|
|
|
|
"unexpected keyword `Self` in generic parameters",
|
|
|
|
)
|
|
|
|
.note("you cannot use `Self` as a generic parameter because it is reserved for associated items")
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
this.eat(&token::Comma);
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:28:08 +00:00
|
|
|
let param = if this.check_lifetime() {
|
|
|
|
let lifetime = this.expect_lifetime();
|
|
|
|
// Parse lifetime parameter.
|
|
|
|
let bounds = if this.eat(&token::Colon) {
|
|
|
|
this.parse_lt_param_bounds()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
Some(ast::GenericParam {
|
|
|
|
ident: lifetime.ident,
|
|
|
|
id: lifetime.id,
|
|
|
|
attrs: attrs.into(),
|
|
|
|
bounds,
|
|
|
|
kind: ast::GenericParamKind::Lifetime,
|
|
|
|
is_placeholder: false,
|
|
|
|
})
|
|
|
|
} else if this.check_keyword(kw::Const) {
|
|
|
|
// Parse const parameter.
|
|
|
|
Some(this.parse_const_param(attrs)?)
|
|
|
|
} else if this.check_ident() {
|
|
|
|
// Parse type parameter.
|
|
|
|
Some(this.parse_ty_param(attrs)?)
|
|
|
|
} else if this.token.can_begin_type() {
|
|
|
|
// Trying to write an associated type bound? (#26271)
|
|
|
|
let snapshot = this.clone();
|
|
|
|
match this.parse_ty_where_predicate() {
|
|
|
|
Ok(where_predicate) => {
|
|
|
|
this.struct_span_err(
|
|
|
|
where_predicate.span(),
|
|
|
|
"bounds on associated types do not belong here",
|
|
|
|
)
|
|
|
|
.span_label(where_predicate.span(), "belongs in `where` clause")
|
|
|
|
.emit();
|
|
|
|
// FIXME - try to continue parsing other generics?
|
|
|
|
return Ok((None, TrailingToken::None));
|
|
|
|
}
|
|
|
|
Err(mut err) => {
|
|
|
|
err.cancel();
|
|
|
|
// FIXME - maybe we should overwrite 'self' outside of `collect_tokens`?
|
|
|
|
*this = snapshot;
|
|
|
|
return Ok((None, TrailingToken::None));
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 18:44:09 +00:00
|
|
|
} else {
|
2021-01-22 18:28:08 +00:00
|
|
|
// Check for trailing attributes and stop parsing.
|
|
|
|
if !attrs.is_empty() {
|
|
|
|
if !params.is_empty() {
|
|
|
|
this.struct_span_err(
|
|
|
|
attrs[0].span,
|
|
|
|
"trailing attribute after generic parameter",
|
|
|
|
)
|
|
|
|
.span_label(attrs[0].span, "attributes must go before parameters")
|
|
|
|
.emit();
|
|
|
|
} else {
|
|
|
|
this.struct_span_err(
|
|
|
|
attrs[0].span,
|
|
|
|
"attribute without generic parameters",
|
|
|
|
)
|
|
|
|
.span_label(
|
|
|
|
attrs[0].span,
|
|
|
|
"attributes are only permitted when preceding parameters",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok((None, TrailingToken::None));
|
|
|
|
};
|
|
|
|
|
|
|
|
if !this.eat(&token::Comma) {
|
|
|
|
done = true;
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
2021-01-22 18:28:08 +00:00
|
|
|
// We just ate the comma, so no need to use `TrailingToken`
|
|
|
|
Ok((param, TrailingToken::None))
|
|
|
|
})?;
|
2019-08-11 18:44:09 +00:00
|
|
|
|
2021-01-22 18:28:08 +00:00
|
|
|
if let Some(param) = param {
|
|
|
|
params.push(param);
|
|
|
|
} else {
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a set of optional generic type parameter declarations. Where
|
|
|
|
/// clauses are not parsed here, and must be added later via
|
|
|
|
/// `parse_where_clause()`.
|
|
|
|
///
|
|
|
|
/// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
|
|
|
|
/// | ( < lifetimes , typaramseq ( , )? > )
|
|
|
|
/// where typaramseq = ( typaram ) | ( typaram , typaramseq )
|
|
|
|
pub(super) fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
|
|
|
|
let span_lo = self.token.span;
|
|
|
|
let (params, span) = if self.eat_lt() {
|
|
|
|
let params = self.parse_generic_params()?;
|
|
|
|
self.expect_gt()?;
|
2020-02-29 11:56:15 +00:00
|
|
|
(params, span_lo.to(self.prev_token.span))
|
2019-08-11 18:44:09 +00:00
|
|
|
} else {
|
2020-02-29 11:56:15 +00:00
|
|
|
(vec![], self.prev_token.span.shrink_to_hi())
|
2019-08-11 18:44:09 +00:00
|
|
|
};
|
|
|
|
Ok(ast::Generics {
|
|
|
|
params,
|
2020-02-19 22:26:46 +00:00
|
|
|
where_clause: WhereClause {
|
2020-06-09 01:09:54 +00:00
|
|
|
has_where_token: false,
|
2020-02-19 22:26:46 +00:00
|
|
|
predicates: Vec::new(),
|
2020-02-29 11:56:15 +00:00
|
|
|
span: self.prev_token.span.shrink_to_hi(),
|
2020-02-19 22:26:46 +00:00
|
|
|
},
|
2019-08-11 18:44:09 +00:00
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an optional where-clause and places it in `generics`.
|
|
|
|
///
|
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
|
|
|
/// where T : Trait<U, V> + 'b, 'a : 'b
|
|
|
|
/// ```
|
|
|
|
pub(super) fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
|
2020-06-09 01:09:54 +00:00
|
|
|
let mut where_clause = WhereClause {
|
|
|
|
has_where_token: false,
|
|
|
|
predicates: Vec::new(),
|
|
|
|
span: self.prev_token.span.shrink_to_hi(),
|
|
|
|
};
|
2019-08-11 18:44:09 +00:00
|
|
|
|
|
|
|
if !self.eat_keyword(kw::Where) {
|
|
|
|
return Ok(where_clause);
|
|
|
|
}
|
2020-06-09 01:09:54 +00:00
|
|
|
where_clause.has_where_token = true;
|
2020-02-29 11:56:15 +00:00
|
|
|
let lo = self.prev_token.span;
|
2019-08-11 18:44:09 +00:00
|
|
|
|
|
|
|
// We are considering adding generics to the `where` keyword as an alternative higher-rank
|
|
|
|
// parameter syntax (as in `where<'a>` or `where<T>`. To avoid that being a breaking
|
|
|
|
// change we parse those generics now, but report an error.
|
2020-03-21 07:32:55 +00:00
|
|
|
if self.choose_generics_over_qpath(0) {
|
2019-08-11 18:44:09 +00:00
|
|
|
let generics = self.parse_generics()?;
|
|
|
|
self.struct_span_err(
|
|
|
|
generics.span,
|
|
|
|
"generic parameters on `where` clauses are reserved for future use",
|
|
|
|
)
|
2019-12-22 22:42:04 +00:00
|
|
|
.span_label(generics.span, "currently unsupported")
|
|
|
|
.emit();
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let lo = self.token.span;
|
|
|
|
if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
|
|
|
|
let lifetime = self.expect_lifetime();
|
|
|
|
// Bounds starting with a colon are mandatory, but possibly empty.
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
let bounds = self.parse_lt_param_bounds();
|
|
|
|
where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
|
2020-02-29 11:56:15 +00:00
|
|
|
ast::WhereRegionPredicate {
|
|
|
|
span: lo.to(self.prev_token.span),
|
|
|
|
lifetime,
|
|
|
|
bounds,
|
|
|
|
},
|
2019-08-11 18:44:09 +00:00
|
|
|
));
|
|
|
|
} else if self.check_type() {
|
2019-09-22 00:11:09 +00:00
|
|
|
where_clause.predicates.push(self.parse_ty_where_predicate()?);
|
2019-08-11 18:44:09 +00:00
|
|
|
} else {
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.eat(&token::Comma) {
|
2019-12-22 22:42:04 +00:00
|
|
|
break;
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 11:56:15 +00:00
|
|
|
where_clause.span = lo.to(self.prev_token.span);
|
2019-08-11 18:44:09 +00:00
|
|
|
Ok(where_clause)
|
|
|
|
}
|
|
|
|
|
2019-09-22 00:11:09 +00:00
|
|
|
fn parse_ty_where_predicate(&mut self) -> PResult<'a, ast::WherePredicate> {
|
|
|
|
let lo = self.token.span;
|
|
|
|
// Parse optional `for<'a, 'b>`.
|
|
|
|
// This `for` is parsed greedily and applies to the whole predicate,
|
|
|
|
// the bounded type can have its own `for` applying only to it.
|
|
|
|
// Examples:
|
|
|
|
// * `for<'a> Trait1<'a>: Trait2<'a /* ok */>`
|
|
|
|
// * `(for<'a> Trait1<'a>): Trait2<'a /* not ok */>`
|
|
|
|
// * `for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /* ok */, 'b /* not ok */>`
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
|
|
|
|
|
|
|
// Parse type with mandatory colon and (possibly empty) bounds,
|
|
|
|
// or with mandatory equality sign and the second type.
|
2020-10-15 19:21:45 +00:00
|
|
|
let ty = self.parse_ty_for_where_clause()?;
|
2019-09-22 00:11:09 +00:00
|
|
|
if self.eat(&token::Colon) {
|
2020-02-29 11:56:15 +00:00
|
|
|
let bounds = self.parse_generic_bounds(Some(self.prev_token.span))?;
|
2019-12-22 22:42:04 +00:00
|
|
|
Ok(ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
|
2020-02-29 11:56:15 +00:00
|
|
|
span: lo.to(self.prev_token.span),
|
2019-12-22 22:42:04 +00:00
|
|
|
bound_generic_params: lifetime_defs,
|
|
|
|
bounded_ty: ty,
|
|
|
|
bounds,
|
|
|
|
}))
|
2019-09-22 00:11:09 +00:00
|
|
|
// FIXME: Decide what should be used here, `=` or `==`.
|
|
|
|
// FIXME: We are just dropping the binders in lifetime_defs on the floor here.
|
|
|
|
} else if self.eat(&token::Eq) || self.eat(&token::EqEq) {
|
|
|
|
let rhs_ty = self.parse_ty()?;
|
2019-12-22 22:42:04 +00:00
|
|
|
Ok(ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
|
2020-02-29 11:56:15 +00:00
|
|
|
span: lo.to(self.prev_token.span),
|
2019-12-22 22:42:04 +00:00
|
|
|
lhs_ty: ty,
|
|
|
|
rhs_ty,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
}))
|
2019-09-22 00:11:09 +00:00
|
|
|
} else {
|
|
|
|
self.unexpected()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 07:32:55 +00:00
|
|
|
pub(super) fn choose_generics_over_qpath(&self, start: usize) -> bool {
|
2019-08-11 18:44:09 +00:00
|
|
|
// There's an ambiguity between generic parameters and qualified paths in impls.
|
|
|
|
// If we see `<` it may start both, so we have to inspect some following tokens.
|
|
|
|
// The following combinations can only start generics,
|
|
|
|
// but not qualified paths (with one exception):
|
|
|
|
// `<` `>` - empty generic parameters
|
|
|
|
// `<` `#` - generic parameters with attributes
|
|
|
|
// `<` (LIFETIME|IDENT) `>` - single generic parameter
|
|
|
|
// `<` (LIFETIME|IDENT) `,` - first generic parameter in a list
|
|
|
|
// `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
|
|
|
|
// `<` (LIFETIME|IDENT) `=` - generic parameter with a default
|
|
|
|
// `<` const - generic const parameter
|
|
|
|
// The only truly ambiguous case is
|
|
|
|
// `<` IDENT `>` `::` IDENT ...
|
|
|
|
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
|
|
|
|
// because this is what almost always expected in practice, qualified paths in impls
|
|
|
|
// (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment.
|
2020-03-21 07:32:55 +00:00
|
|
|
self.look_ahead(start, |t| t == &token::Lt)
|
|
|
|
&& (self.look_ahead(start + 1, |t| t == &token::Pound || t == &token::Gt)
|
|
|
|
|| self.look_ahead(start + 1, |t| t.is_lifetime() || t.is_ident())
|
|
|
|
&& self.look_ahead(start + 2, |t| {
|
|
|
|
matches!(t.kind, token::Gt | token::Comma | token::Colon | token::Eq)
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
2020-03-21 07:32:55 +00:00
|
|
|
|| self.is_keyword_ahead(start + 1, &[kw::Const]))
|
2019-08-11 18:44:09 +00:00
|
|
|
}
|
|
|
|
}
|