2019-10-11 11:06:36 +00:00
|
|
|
use super::{Parser, PathStyle, PrevTokenKind, TokenType};
|
2019-10-11 08:54:26 +00:00
|
|
|
use super::item::ParamCfg;
|
2019-08-11 17:59:27 +00:00
|
|
|
|
|
|
|
use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath};
|
2019-10-15 20:48:13 +00:00
|
|
|
|
2019-12-05 05:38:06 +00:00
|
|
|
use rustc_errors::{PResult, Applicability, pluralize};
|
|
|
|
use rustc_error_codes::*;
|
2019-10-15 20:48:13 +00:00
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
|
|
|
|
use syntax::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
|
2019-12-08 07:19:53 +00:00
|
|
|
use syntax::ast::{Mutability, Mac};
|
2019-10-15 20:48:13 +00:00
|
|
|
use syntax::token::{self, Token};
|
2019-12-08 05:32:58 +00:00
|
|
|
use syntax::struct_span_err;
|
2019-12-05 05:38:06 +00:00
|
|
|
use syntax_pos::source_map::Span;
|
2019-10-15 20:48:13 +00:00
|
|
|
use syntax_pos::symbol::kw;
|
2019-08-11 17:59:27 +00:00
|
|
|
|
|
|
|
/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
|
|
|
|
/// `IDENT<<u8 as Trait>::AssocTy>`.
|
|
|
|
///
|
|
|
|
/// Types can also be of the form `IDENT(u8, u8) -> u8`, however this assumes
|
|
|
|
/// that `IDENT` is not the ident of a fn trait.
|
|
|
|
fn can_continue_type_after_non_fn_ident(t: &Token) -> bool {
|
|
|
|
t == &token::ModSep || t == &token::Lt ||
|
|
|
|
t == &token::BinOp(token::Shl)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
/// Parses a type.
|
|
|
|
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(true, true, false)
|
|
|
|
}
|
|
|
|
|
2019-12-01 15:00:08 +00:00
|
|
|
/// Parse a type suitable for a function or function pointer parameter.
|
|
|
|
/// The difference from `parse_ty` is that this version allows `...`
|
|
|
|
/// (`CVarArgs`) at the top level of the the type.
|
2019-12-02 01:38:33 +00:00
|
|
|
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(true, true, true)
|
2019-12-01 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 17:59:27 +00:00
|
|
|
/// Parses a type in restricted contexts where `+` is not permitted.
|
|
|
|
///
|
|
|
|
/// Example 1: `&'a TYPE`
|
|
|
|
/// `+` is prohibited to maintain operator priority (P(+) < P(&)).
|
|
|
|
/// Example 2: `value1 as TYPE + value2`
|
|
|
|
/// `+` is prohibited to avoid interactions with expression grammar.
|
|
|
|
pub(super) fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(false, true, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an optional return type `[ -> TY ]` in a function declaration.
|
2019-12-01 15:00:08 +00:00
|
|
|
pub(super) fn parse_ret_ty(
|
|
|
|
&mut self,
|
|
|
|
allow_plus: bool,
|
|
|
|
allow_qpath_recovery: bool,
|
|
|
|
) -> PResult<'a, FunctionRetTy> {
|
2019-12-01 11:52:59 +00:00
|
|
|
Ok(if self.eat(&token::RArrow) {
|
|
|
|
// FIXME(Centril): Can we unconditionally `allow_plus`?
|
2019-12-01 15:00:08 +00:00
|
|
|
FunctionRetTy::Ty(self.parse_ty_common(allow_plus, allow_qpath_recovery, false)?)
|
2019-08-11 17:59:27 +00:00
|
|
|
} else {
|
2019-12-01 11:52:59 +00:00
|
|
|
FunctionRetTy::Default(self.token.span.shrink_to_lo())
|
|
|
|
})
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 15:00:08 +00:00
|
|
|
fn parse_ty_common(
|
|
|
|
&mut self,
|
|
|
|
allow_plus: bool,
|
|
|
|
allow_qpath_recovery: bool,
|
|
|
|
// Is `...` (`CVarArgs`) legal in the immediate top level call?
|
|
|
|
allow_c_variadic: bool,
|
|
|
|
) -> PResult<'a, P<Ty>> {
|
2019-08-11 17:59:27 +00:00
|
|
|
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
|
|
|
|
maybe_whole!(self, NtTy, |x| x);
|
|
|
|
|
|
|
|
let lo = self.token.span;
|
|
|
|
let mut impl_dyn_multi = false;
|
2019-12-08 06:55:38 +00:00
|
|
|
let kind = if self.check(&token::OpenDelim(token::Paren)) {
|
|
|
|
self.parse_ty_tuple_or_parens(lo, allow_plus)?
|
2019-08-11 17:59:27 +00:00
|
|
|
} else if self.eat(&token::Not) {
|
|
|
|
// Never type `!`
|
|
|
|
TyKind::Never
|
|
|
|
} else if self.eat(&token::BinOp(token::Star)) {
|
2019-12-08 06:58:45 +00:00
|
|
|
self.parse_ty_ptr()?
|
2019-08-11 17:59:27 +00:00
|
|
|
} else if self.eat(&token::OpenDelim(token::Bracket)) {
|
2019-12-08 07:19:53 +00:00
|
|
|
self.parse_array_or_slice_ty()?
|
2019-08-11 17:59:27 +00:00
|
|
|
} else if self.check(&token::BinOp(token::And)) || self.check(&token::AndAnd) {
|
|
|
|
// Reference
|
|
|
|
self.expect_and()?;
|
|
|
|
self.parse_borrowed_pointee()?
|
|
|
|
} else if self.eat_keyword_noexpect(kw::Typeof) {
|
2019-12-08 07:23:10 +00:00
|
|
|
self.parse_typeof_ty()?
|
2019-08-11 17:59:27 +00:00
|
|
|
} else if self.eat_keyword(kw::Underscore) {
|
|
|
|
// A type to be inferred `_`
|
|
|
|
TyKind::Infer
|
|
|
|
} else if self.token_is_bare_fn_keyword() {
|
|
|
|
// Function pointer type
|
|
|
|
self.parse_ty_bare_fn(Vec::new())?
|
|
|
|
} else if self.check_keyword(kw::For) {
|
|
|
|
// Function pointer type or bound list (trait object type) starting with a poly-trait.
|
|
|
|
// `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
|
|
|
|
// `for<'lt> Trait1<'lt> + Trait2 + 'a`
|
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
|
|
|
if self.token_is_bare_fn_keyword() {
|
|
|
|
self.parse_ty_bare_fn(lifetime_defs)?
|
|
|
|
} else {
|
|
|
|
let path = self.parse_path(PathStyle::Type)?;
|
|
|
|
let parse_plus = allow_plus && self.check_plus();
|
|
|
|
self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)?
|
|
|
|
}
|
|
|
|
} else if self.eat_keyword(kw::Impl) {
|
2019-12-08 07:29:12 +00:00
|
|
|
self.parse_impl_ty(&mut impl_dyn_multi)?
|
2019-12-08 07:38:23 +00:00
|
|
|
} else if self.is_explicit_dyn_type() {
|
|
|
|
self.parse_dyn_ty(&mut impl_dyn_multi)?
|
|
|
|
} else if self.check(&token::Question)
|
|
|
|
|| self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus())
|
|
|
|
{
|
2019-08-11 17:59:27 +00:00
|
|
|
// Bound list (trait object type)
|
2019-12-08 07:38:23 +00:00
|
|
|
let bounds = self.parse_generic_bounds_common(allow_plus, None)?;
|
|
|
|
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
|
2019-08-11 17:59:27 +00:00
|
|
|
} else if self.eat_lt() {
|
|
|
|
// Qualified path
|
|
|
|
let (qself, path) = self.parse_qpath(PathStyle::Type)?;
|
|
|
|
TyKind::Path(Some(qself), path)
|
|
|
|
} else if self.token.is_path_start() {
|
2019-12-08 07:49:20 +00:00
|
|
|
self.parse_path_start_ty(lo, allow_plus)?
|
2019-12-08 05:32:58 +00:00
|
|
|
} else if self.eat(&token::DotDotDot) {
|
2019-08-11 17:59:27 +00:00
|
|
|
if allow_c_variadic {
|
|
|
|
TyKind::CVarArgs
|
|
|
|
} else {
|
2019-12-01 15:00:08 +00:00
|
|
|
// FIXME(Centril): Should we just allow `...` syntactically
|
|
|
|
// anywhere in a type and use semantic restrictions instead?
|
2019-12-08 08:01:26 +00:00
|
|
|
self.error_illegal_c_varadic_ty(lo);
|
2019-12-08 05:32:58 +00:00
|
|
|
TyKind::Err
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let msg = format!("expected type, found {}", self.this_token_descr());
|
2019-12-08 08:08:19 +00:00
|
|
|
let mut err = self.struct_span_err(self.token.span, &msg);
|
2019-08-11 17:59:27 +00:00
|
|
|
err.span_label(self.token.span, "expected type");
|
|
|
|
self.maybe_annotate_with_ascription(&mut err, true);
|
|
|
|
return Err(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = lo.to(self.prev_span);
|
2019-10-08 12:39:58 +00:00
|
|
|
let ty = self.mk_ty(span, kind);
|
2019-08-11 17:59:27 +00:00
|
|
|
|
|
|
|
// Try to recover from use of `+` with incorrect priority.
|
|
|
|
self.maybe_report_ambiguous_plus(allow_plus, impl_dyn_multi, &ty);
|
|
|
|
self.maybe_recover_from_bad_type_plus(allow_plus, &ty)?;
|
|
|
|
self.maybe_recover_from_bad_qpath(ty, allow_qpath_recovery)
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:44:24 +00:00
|
|
|
/// Parses either:
|
|
|
|
/// - `(TYPE)`, a parenthesized type.
|
|
|
|
/// - `(TYPE,)`, a tuple with a single field of type TYPE.
|
2019-12-08 06:55:38 +00:00
|
|
|
fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: bool) -> PResult<'a, TyKind> {
|
|
|
|
let mut trailing_plus = false;
|
|
|
|
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
|
|
|
|
let ty = p.parse_ty()?;
|
|
|
|
trailing_plus = p.prev_token_kind == PrevTokenKind::Plus;
|
|
|
|
Ok(ty)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if ts.len() == 1 && !trailing {
|
2019-12-08 05:44:24 +00:00
|
|
|
let ty = ts.into_iter().nth(0).unwrap().into_inner();
|
|
|
|
let maybe_bounds = allow_plus && self.token.is_like_plus();
|
|
|
|
match ty.kind {
|
|
|
|
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
|
2019-12-08 06:55:38 +00:00
|
|
|
TyKind::Path(None, path) if maybe_bounds => {
|
|
|
|
self.parse_remaining_bounds(Vec::new(), path, lo, true)
|
2019-12-08 05:44:24 +00:00
|
|
|
}
|
2019-12-08 06:55:38 +00:00
|
|
|
TyKind::TraitObject(mut bounds, TraitObjectSyntax::None)
|
|
|
|
if maybe_bounds && bounds.len() == 1 && !trailing_plus => {
|
|
|
|
let path = match bounds.remove(0) {
|
|
|
|
GenericBound::Trait(pt, ..) => pt.trait_ref.path,
|
2019-12-08 05:44:24 +00:00
|
|
|
GenericBound::Outlives(..) => self.bug("unexpected lifetime bound"),
|
|
|
|
};
|
|
|
|
self.parse_remaining_bounds(Vec::new(), path, lo, true)
|
|
|
|
}
|
|
|
|
// `(TYPE)`
|
|
|
|
_ => Ok(TyKind::Paren(P(ty)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(TyKind::Tup(ts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:10:17 +00:00
|
|
|
fn parse_remaining_bounds(
|
|
|
|
&mut self,
|
|
|
|
generic_params: Vec<GenericParam>,
|
|
|
|
path: ast::Path,
|
|
|
|
lo: Span,
|
|
|
|
parse_plus: bool,
|
|
|
|
) -> PResult<'a, TyKind> {
|
2019-08-11 17:59:27 +00:00
|
|
|
let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span));
|
|
|
|
let mut bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)];
|
|
|
|
if parse_plus {
|
|
|
|
self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded
|
|
|
|
bounds.append(&mut self.parse_generic_bounds(Some(self.prev_span))?);
|
|
|
|
}
|
|
|
|
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
|
|
|
|
}
|
|
|
|
|
2019-12-08 06:58:45 +00:00
|
|
|
/// Parses a raw pointer type: `*[const | mut] $type`.
|
|
|
|
fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
|
2019-09-30 00:36:08 +00:00
|
|
|
let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
|
2019-08-11 17:59:27 +00:00
|
|
|
let span = self.prev_span;
|
|
|
|
let msg = "expected mut or const in raw pointer type";
|
|
|
|
self.struct_span_err(span, msg)
|
|
|
|
.span_label(span, msg)
|
|
|
|
.help("use `*mut T` or `*const T` as appropriate")
|
|
|
|
.emit();
|
|
|
|
Mutability::Immutable
|
2019-09-30 00:36:08 +00:00
|
|
|
});
|
2019-12-08 06:58:45 +00:00
|
|
|
let ty = self.parse_ty_no_plus()?;
|
|
|
|
Ok(TyKind::Ptr(MutTy { ty, mutbl }))
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 07:19:53 +00:00
|
|
|
/// Parses an array (`[TYPE; EXPR]`) or slice (`[TYPE]`) type.
|
|
|
|
/// The opening `[` bracket is already eaten.
|
|
|
|
fn parse_array_or_slice_ty(&mut self) -> PResult<'a, TyKind> {
|
|
|
|
let elt_ty = self.parse_ty()?;
|
|
|
|
let ty = if self.eat(&token::Semi) {
|
|
|
|
TyKind::Array(elt_ty, self.parse_anon_const_expr()?)
|
2019-08-11 17:59:27 +00:00
|
|
|
} else {
|
2019-12-08 07:19:53 +00:00
|
|
|
TyKind::Slice(elt_ty)
|
|
|
|
};
|
|
|
|
self.expect(&token::CloseDelim(token::Bracket))?;
|
|
|
|
Ok(ty)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
|
|
|
|
let opt_lifetime = if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
|
|
|
|
let mutbl = self.parse_mutability();
|
|
|
|
let ty = self.parse_ty_no_plus()?;
|
2019-12-08 07:23:10 +00:00
|
|
|
Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses the `typeof(EXPR)`.
|
|
|
|
// To avoid ambiguity, the type is surrounded by parenthesis.
|
|
|
|
fn parse_typeof_ty(&mut self) -> PResult<'a, TyKind> {
|
|
|
|
self.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let expr = self.parse_anon_const_expr()?;
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
Ok(TyKind::Typeof(expr))
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Is the current token one of the keywords that signals a bare function type?
|
|
|
|
fn token_is_bare_fn_keyword(&mut self) -> bool {
|
|
|
|
self.check_keyword(kw::Fn) ||
|
|
|
|
self.check_keyword(kw::Unsafe) ||
|
|
|
|
self.check_keyword(kw::Extern)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a `TyKind::BareFn` type.
|
|
|
|
fn parse_ty_bare_fn(&mut self, generic_params: Vec<GenericParam>) -> PResult<'a, TyKind> {
|
|
|
|
/*
|
|
|
|
|
|
|
|
[unsafe] [extern "ABI"] fn (S) -> T
|
|
|
|
^~~~^ ^~~~^ ^~^ ^
|
|
|
|
| | | |
|
|
|
|
| | | Return type
|
|
|
|
| | Argument types
|
|
|
|
| |
|
|
|
|
| ABI
|
|
|
|
Function Style
|
|
|
|
*/
|
|
|
|
|
|
|
|
let unsafety = self.parse_unsafety();
|
2019-11-09 19:05:20 +00:00
|
|
|
let ext = self.parse_extern()?;
|
2019-08-11 17:59:27 +00:00
|
|
|
self.expect_keyword(kw::Fn)?;
|
2019-10-11 08:54:26 +00:00
|
|
|
let cfg = ParamCfg {
|
2019-10-03 01:39:46 +00:00
|
|
|
is_self_allowed: false,
|
|
|
|
is_name_required: |_| false,
|
|
|
|
};
|
|
|
|
let decl = self.parse_fn_decl(cfg, false)?;
|
2019-08-11 17:59:27 +00:00
|
|
|
Ok(TyKind::BareFn(P(BareFnTy {
|
2019-11-09 19:05:20 +00:00
|
|
|
ext,
|
2019-08-11 17:59:27 +00:00
|
|
|
unsafety,
|
|
|
|
generic_params,
|
|
|
|
decl,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:29:12 +00:00
|
|
|
/// Parses an `impl B0 + ... + Bn` type.
|
|
|
|
fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
|
|
|
|
// Always parse bounds greedily for better error recovery.
|
|
|
|
let bounds = self.parse_generic_bounds(None)?;
|
|
|
|
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
|
|
|
|
Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:38:23 +00:00
|
|
|
/// Is a `dyn B0 + ... + Bn` type allowed here?
|
|
|
|
fn is_explicit_dyn_type(&mut self) -> bool {
|
|
|
|
self.check_keyword(kw::Dyn)
|
|
|
|
&& (self.token.span.rust_2018()
|
|
|
|
|| self.look_ahead(1, |t| {
|
|
|
|
t.can_begin_bound() && !can_continue_type_after_non_fn_ident(t)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a `dyn B0 + ... + Bn` type.
|
|
|
|
///
|
|
|
|
/// Note that this does *not* parse bare trait objects.
|
|
|
|
fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
|
|
|
|
self.bump(); // `dyn`
|
|
|
|
// Always parse bounds greedily for better error recovery.
|
|
|
|
let bounds = self.parse_generic_bounds(None)?;
|
|
|
|
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
|
|
|
|
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:49:20 +00:00
|
|
|
/// Parses a type starting with a path.
|
|
|
|
///
|
|
|
|
/// This can be:
|
|
|
|
/// 1. a type macro, `mac!(...)`,
|
|
|
|
/// 2. a bare trait object, `B0 + ... + Bn`,
|
|
|
|
/// 3. or a path, `path::to::MyType`.
|
|
|
|
fn parse_path_start_ty(&mut self, lo: Span, allow_plus: bool) -> PResult<'a, TyKind> {
|
|
|
|
// Simple path
|
|
|
|
let path = self.parse_path(PathStyle::Type)?;
|
|
|
|
if self.eat(&token::Not) {
|
|
|
|
// Macro invocation in type position
|
|
|
|
Ok(TyKind::Mac(Mac {
|
|
|
|
path,
|
|
|
|
args: self.parse_mac_args()?,
|
|
|
|
prior_type_ascription: self.last_type_ascription,
|
|
|
|
}))
|
|
|
|
} else if allow_plus && self.check_plus() {
|
|
|
|
// `Trait1 + Trait2 + 'a`
|
|
|
|
self.parse_remaining_bounds(Vec::new(), path, lo, true)
|
|
|
|
} else {
|
|
|
|
// Just a type path.
|
|
|
|
Ok(TyKind::Path(None, path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:01:26 +00:00
|
|
|
fn error_illegal_c_varadic_ty(&self, lo: Span) {
|
|
|
|
struct_span_err!(
|
|
|
|
self.sess.span_diagnostic,
|
|
|
|
lo.to(self.prev_span),
|
|
|
|
E0743,
|
|
|
|
"C-variadic type `...` may not be nested inside another type",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:10:17 +00:00
|
|
|
pub(super) fn parse_generic_bounds(
|
|
|
|
&mut self,
|
|
|
|
colon_span: Option<Span>,
|
|
|
|
) -> PResult<'a, GenericBounds> {
|
2019-08-11 17:59:27 +00:00
|
|
|
self.parse_generic_bounds_common(true, colon_span)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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>`)
|
|
|
|
/// ```
|
2019-12-08 08:10:17 +00:00
|
|
|
fn parse_generic_bounds_common(
|
|
|
|
&mut self,
|
|
|
|
allow_plus: bool,
|
|
|
|
colon_span: Option<Span>,
|
|
|
|
) -> PResult<'a, GenericBounds> {
|
2019-08-11 17:59:27 +00:00
|
|
|
let mut bounds = Vec::new();
|
|
|
|
let mut negative_bounds = Vec::new();
|
|
|
|
let mut last_plus_span = None;
|
|
|
|
let mut was_negative = false;
|
|
|
|
loop {
|
|
|
|
// This needs to be synchronized with `TokenKind::can_begin_bound`.
|
2019-12-08 08:10:17 +00:00
|
|
|
let is_bound_start = self.check_path()
|
|
|
|
|| self.check_lifetime()
|
|
|
|
|| self.check(&token::Not) // Used for error reporting only.
|
|
|
|
|| self.check(&token::Question)
|
|
|
|
|| self.check_keyword(kw::For)
|
|
|
|
|| self.check(&token::OpenDelim(token::Paren));
|
|
|
|
|
2019-08-11 17:59:27 +00:00
|
|
|
if is_bound_start {
|
|
|
|
let lo = self.token.span;
|
|
|
|
let has_parens = self.eat(&token::OpenDelim(token::Paren));
|
|
|
|
let inner_lo = self.token.span;
|
|
|
|
let is_negative = self.eat(&token::Not);
|
|
|
|
let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
|
|
|
|
if self.token.is_lifetime() {
|
2019-12-08 08:18:34 +00:00
|
|
|
self.error_opt_out_lifetime(question);
|
2019-08-11 17:59:27 +00:00
|
|
|
bounds.push(GenericBound::Outlives(self.expect_lifetime()));
|
|
|
|
if has_parens {
|
|
|
|
let inner_span = inner_lo.to(self.prev_span);
|
|
|
|
self.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
let mut err = self.struct_span_err(
|
|
|
|
lo.to(self.prev_span),
|
|
|
|
"parenthesized lifetime bounds are not supported"
|
|
|
|
);
|
|
|
|
if let Ok(snippet) = self.span_to_snippet(inner_span) {
|
|
|
|
err.span_suggestion_short(
|
|
|
|
lo.to(self.prev_span),
|
|
|
|
"remove the parentheses",
|
|
|
|
snippet.to_owned(),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
if is_negative {
|
|
|
|
was_negative = true;
|
|
|
|
if let Some(sp) = last_plus_span.or(colon_span) {
|
|
|
|
negative_bounds.push(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
|
|
|
|
};
|
|
|
|
bounds.push(GenericBound::Trait(poly_trait, modifier));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allow_plus || !self.eat_plus() {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
last_plus_span = Some(self.prev_span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !negative_bounds.is_empty() || was_negative {
|
2019-09-19 07:13:40 +00:00
|
|
|
let negative_bounds_len = negative_bounds.len();
|
2019-08-11 17:59:27 +00:00
|
|
|
let last_span = negative_bounds.last().map(|sp| *sp);
|
|
|
|
let mut err = self.struct_span_err(
|
|
|
|
negative_bounds,
|
|
|
|
"negative trait bounds are not supported",
|
|
|
|
);
|
|
|
|
if let Some(sp) = last_span {
|
|
|
|
err.span_label(sp, "negative trait bounds are not supported");
|
|
|
|
}
|
|
|
|
if let Some(bound_list) = colon_span {
|
|
|
|
let bound_list = bound_list.to(self.prev_span);
|
|
|
|
let mut new_bound_list = String::new();
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
let mut snippets = bounds.iter().map(|bound| bound.span())
|
|
|
|
.map(|span| self.span_to_snippet(span));
|
|
|
|
while let Some(Ok(snippet)) = snippets.next() {
|
|
|
|
new_bound_list.push_str(" + ");
|
|
|
|
new_bound_list.push_str(&snippet);
|
|
|
|
}
|
|
|
|
new_bound_list = new_bound_list.replacen(" +", ":", 1);
|
|
|
|
}
|
|
|
|
err.span_suggestion_hidden(
|
|
|
|
bound_list,
|
2019-11-05 20:10:24 +00:00
|
|
|
&format!("remove the trait bound{}", pluralize!(negative_bounds_len)),
|
2019-08-11 17:59:27 +00:00
|
|
|
new_bound_list,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(bounds);
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:18:34 +00:00
|
|
|
fn error_opt_out_lifetime(&self, question: Option<Span>) {
|
|
|
|
if let Some(span) = question {
|
|
|
|
self.struct_span_err(span, "`?` may only modify trait bounds, not lifetime bounds")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 17:59:27 +00:00
|
|
|
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
|
|
|
|
if self.eat_keyword(kw::For) {
|
|
|
|
self.expect_lt()?;
|
|
|
|
let params = self.parse_generic_params()?;
|
|
|
|
self.expect_gt()?;
|
|
|
|
// We rely on AST validation to rule out invalid cases: There must not be type
|
|
|
|
// parameters, and the lifetime parameters must not have bounds.
|
|
|
|
Ok(params)
|
|
|
|
} else {
|
|
|
|
Ok(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 18:46:34 +00:00
|
|
|
|
2019-10-16 08:59:30 +00:00
|
|
|
pub fn check_lifetime(&mut self) -> bool {
|
2019-08-11 18:46:34 +00:00
|
|
|
self.expected_tokens.push(TokenType::Lifetime);
|
|
|
|
self.token.is_lifetime()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a single lifetime `'a` or panics.
|
2019-10-16 08:59:30 +00:00
|
|
|
pub fn expect_lifetime(&mut self) -> Lifetime {
|
2019-08-11 18:46:34 +00:00
|
|
|
if let Some(ident) = self.token.lifetime() {
|
|
|
|
let span = self.token.span;
|
|
|
|
self.bump();
|
|
|
|
Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID }
|
|
|
|
} else {
|
|
|
|
self.span_bug(self.token.span, "not a lifetime")
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 12:39:58 +00:00
|
|
|
|
|
|
|
pub(super) fn mk_ty(&self, span: Span, kind: TyKind) -> P<Ty> {
|
|
|
|
P(Ty { kind, span, id: ast::DUMMY_NODE_ID })
|
|
|
|
}
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|