2020-02-10 17:20:01 +00:00
|
|
|
use super::{Parser, PathStyle, TokenType};
|
2019-08-11 17:59:27 +00:00
|
|
|
|
|
|
|
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
|
2019-10-15 20:48:13 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-26 12:40:14 +00:00
|
|
|
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
|
2021-05-15 21:56:28 +00:00
|
|
|
use rustc_ast::{
|
|
|
|
self as ast, BareFnTy, FnRetTy, GenericBound, GenericBounds, GenericParam, Generics, Lifetime,
|
|
|
|
MacCall, MutTy, Mutability, PolyTraitRef, TraitBoundModifier, TraitObjectSyntax, Ty, TyKind,
|
|
|
|
};
|
2019-12-31 20:25:16 +00:00
|
|
|
use rustc_errors::{pluralize, struct_span_err, Applicability, PResult};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2020-01-02 23:50:18 +00:00
|
|
|
use rustc_span::symbol::{kw, sym};
|
2019-08-11 17:59:27 +00:00
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
/// Any `?` or `~const` modifiers that appear at the start of a bound.
|
2020-01-02 23:50:18 +00:00
|
|
|
struct BoundModifiers {
|
|
|
|
/// `?Trait`.
|
|
|
|
maybe: Option<Span>,
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
/// `~const Trait`.
|
2020-01-02 23:50:18 +00:00
|
|
|
maybe_const: Option<Span>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BoundModifiers {
|
2020-01-20 09:20:45 +00:00
|
|
|
fn to_trait_bound_modifier(&self) -> TraitBoundModifier {
|
|
|
|
match (self.maybe, self.maybe_const) {
|
2020-01-14 04:30:27 +00:00
|
|
|
(None, None) => TraitBoundModifier::None,
|
|
|
|
(Some(_), None) => TraitBoundModifier::Maybe,
|
|
|
|
(None, Some(_)) => TraitBoundModifier::MaybeConst,
|
2020-01-20 09:20:45 +00:00
|
|
|
(Some(_), Some(_)) => TraitBoundModifier::MaybeConstMaybe,
|
2020-01-02 23:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2020-01-29 00:57:24 +00:00
|
|
|
pub(super) enum AllowPlus {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
#[derive(PartialEq)]
|
2020-01-29 00:57:24 +00:00
|
|
|
pub(super) enum RecoverQPath {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2022-03-04 02:03:55 +00:00
|
|
|
pub(super) enum RecoverQuestionMark {
|
2022-01-10 22:02:19 +00:00
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:44:08 +00:00
|
|
|
/// Signals whether parsing a type should recover `->`.
|
|
|
|
///
|
|
|
|
/// More specifically, when parsing a function like:
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```compile_fail
|
2020-12-17 12:44:08 +00:00
|
|
|
/// fn foo() => u8 { 0 }
|
|
|
|
/// fn bar(): u8 { 0 }
|
|
|
|
/// ```
|
|
|
|
/// The compiler will try to recover interpreting `foo() => u8` as `foo() -> u8` when calling
|
|
|
|
/// `parse_ty` with anything except `RecoverReturnSign::No`, and it will try to recover `bar(): u8`
|
|
|
|
/// as `bar() -> u8` when passing `RecoverReturnSign::Yes` to `parse_ty`
|
2020-10-15 19:21:45 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub(super) enum RecoverReturnSign {
|
2020-10-14 20:27:48 +00:00
|
|
|
Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
OnlyFatArrow,
|
2020-10-14 20:27:48 +00:00
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:21:45 +00:00
|
|
|
impl RecoverReturnSign {
|
2020-12-17 12:44:08 +00:00
|
|
|
/// [RecoverReturnSign::Yes] allows for recovering `fn foo() => u8` and `fn foo(): u8`,
|
|
|
|
/// [RecoverReturnSign::OnlyFatArrow] allows for recovering only `fn foo() => u8` (recovering
|
|
|
|
/// colons can cause problems when parsing where clauses), and
|
|
|
|
/// [RecoverReturnSign::No] doesn't allow for any recovery of the return type arrow
|
2020-10-15 19:21:45 +00:00
|
|
|
fn can_recover(self, token: &TokenKind) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Yes => matches!(token, token::FatArrow | token::Colon),
|
|
|
|
Self::OnlyFatArrow => matches!(token, token::FatArrow),
|
|
|
|
Self::No => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 00:57:24 +00:00
|
|
|
// Is `...` (`CVarArgs`) legal at this level of type parsing?
|
2020-01-29 23:18:54 +00:00
|
|
|
#[derive(PartialEq)]
|
2020-01-29 00:57:24 +00:00
|
|
|
enum AllowCVariadic {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
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>> {
|
2020-10-15 19:21:45 +00:00
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::Yes,
|
|
|
|
AllowCVariadic::No,
|
2020-12-17 12:44:08 +00:00
|
|
|
RecoverQPath::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
RecoverReturnSign::Yes,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2021-05-15 21:56:28 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_ty_with_generics_recovery(
|
|
|
|
&mut self,
|
|
|
|
ty_params: &Generics,
|
|
|
|
) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::Yes,
|
|
|
|
AllowCVariadic::No,
|
|
|
|
RecoverQPath::Yes,
|
|
|
|
RecoverReturnSign::Yes,
|
|
|
|
Some(ty_params),
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
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 `...`
|
2020-09-21 03:14:28 +00:00
|
|
|
/// (`CVarArgs`) at the top level of the type.
|
2019-12-02 01:38:33 +00:00
|
|
|
pub(super) fn parse_ty_for_param(&mut self) -> PResult<'a, P<Ty>> {
|
2020-10-15 19:21:45 +00:00
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::Yes,
|
|
|
|
AllowCVariadic::Yes,
|
2020-12-17 12:44:08 +00:00
|
|
|
RecoverQPath::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
RecoverReturnSign::Yes,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)
|
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>> {
|
2020-10-15 19:21:45 +00:00
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::No,
|
|
|
|
AllowCVariadic::No,
|
2020-12-17 12:44:08 +00:00
|
|
|
RecoverQPath::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
RecoverReturnSign::Yes,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:02:19 +00:00
|
|
|
/// Parses a type following an `as` cast. Similar to `parse_ty_no_plus`, but signaling origin
|
|
|
|
/// for better diagnostics involving `?`.
|
|
|
|
pub(super) fn parse_as_cast_ty(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::No,
|
|
|
|
AllowCVariadic::No,
|
|
|
|
RecoverQPath::Yes,
|
|
|
|
RecoverReturnSign::Yes,
|
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::No,
|
2022-01-10 22:02:19 +00:00
|
|
|
)
|
|
|
|
}
|
2022-03-04 02:03:55 +00:00
|
|
|
|
|
|
|
pub(super) fn parse_no_question_mark_recover(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::Yes,
|
|
|
|
AllowCVariadic::No,
|
|
|
|
RecoverQPath::Yes,
|
|
|
|
RecoverReturnSign::Yes,
|
|
|
|
None,
|
|
|
|
RecoverQuestionMark::No,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:21:45 +00:00
|
|
|
/// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
|
|
|
|
pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
|
|
|
|
self.parse_ty_common(
|
|
|
|
AllowPlus::Yes,
|
|
|
|
AllowCVariadic::Yes,
|
2020-12-17 12:44:08 +00:00
|
|
|
RecoverQPath::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
RecoverReturnSign::OnlyFatArrow,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2020-01-29 00:57:24 +00:00
|
|
|
allow_plus: AllowPlus,
|
|
|
|
recover_qpath: RecoverQPath,
|
2020-10-15 19:21:45 +00:00
|
|
|
recover_return_sign: RecoverReturnSign,
|
2020-02-15 03:10:59 +00:00
|
|
|
) -> PResult<'a, FnRetTy> {
|
2019-12-01 11:52:59 +00:00
|
|
|
Ok(if self.eat(&token::RArrow) {
|
|
|
|
// FIXME(Centril): Can we unconditionally `allow_plus`?
|
2020-10-15 19:21:45 +00:00
|
|
|
let ty = self.parse_ty_common(
|
|
|
|
allow_plus,
|
|
|
|
AllowCVariadic::No,
|
2020-12-17 12:44:08 +00:00
|
|
|
recover_qpath,
|
2020-10-15 19:21:45 +00:00
|
|
|
recover_return_sign,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)?;
|
2020-02-15 03:10:59 +00:00
|
|
|
FnRetTy::Ty(ty)
|
2020-10-15 19:21:45 +00:00
|
|
|
} else if recover_return_sign.can_recover(&self.token.kind) {
|
2020-10-14 20:27:48 +00:00
|
|
|
// Don't `eat` to prevent `=>` from being added as an expected token which isn't
|
|
|
|
// actually expected and could only confuse users
|
|
|
|
self.bump();
|
|
|
|
self.struct_span_err(self.prev_token.span, "return types are denoted using `->`")
|
|
|
|
.span_suggestion_short(
|
|
|
|
self.prev_token.span,
|
|
|
|
"use `->` instead",
|
|
|
|
"->".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
2020-10-15 19:21:45 +00:00
|
|
|
let ty = self.parse_ty_common(
|
|
|
|
allow_plus,
|
|
|
|
AllowCVariadic::No,
|
2020-12-17 12:44:08 +00:00
|
|
|
recover_qpath,
|
2020-10-15 19:21:45 +00:00
|
|
|
recover_return_sign,
|
2021-05-15 21:56:28 +00:00
|
|
|
None,
|
2022-03-04 02:03:55 +00:00
|
|
|
RecoverQuestionMark::Yes,
|
2020-10-15 19:21:45 +00:00
|
|
|
)?;
|
2020-10-14 20:27:48 +00:00
|
|
|
FnRetTy::Ty(ty)
|
2019-08-11 17:59:27 +00:00
|
|
|
} else {
|
2020-02-15 03:10:59 +00:00
|
|
|
FnRetTy::Default(self.token.span.shrink_to_lo())
|
2019-12-01 11:52:59 +00:00
|
|
|
})
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 15:00:08 +00:00
|
|
|
fn parse_ty_common(
|
|
|
|
&mut self,
|
2020-01-29 00:57:24 +00:00
|
|
|
allow_plus: AllowPlus,
|
|
|
|
allow_c_variadic: AllowCVariadic,
|
2020-12-17 12:44:08 +00:00
|
|
|
recover_qpath: RecoverQPath,
|
2020-10-15 19:21:45 +00:00
|
|
|
recover_return_sign: RecoverReturnSign,
|
2021-05-15 21:56:28 +00:00
|
|
|
ty_generics: Option<&Generics>,
|
2022-03-04 02:03:55 +00:00
|
|
|
recover_question_mark: RecoverQuestionMark,
|
2019-12-01 15:00:08 +00:00
|
|
|
) -> PResult<'a, P<Ty>> {
|
2020-01-29 23:18:54 +00:00
|
|
|
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
|
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;
|
2022-04-26 12:40:14 +00:00
|
|
|
let kind = if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
|
2019-12-08 06:55:38 +00:00
|
|
|
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()?
|
2022-04-26 12:40:14 +00:00
|
|
|
} else if self.eat(&token::OpenDelim(Delimiter::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
|
2020-12-07 16:32:13 +00:00
|
|
|
} else if self.check_fn_front_matter(false) {
|
2019-08-11 17:59:27 +00:00
|
|
|
// Function pointer type
|
2020-10-15 19:21:45 +00:00
|
|
|
self.parse_ty_bare_fn(lo, Vec::new(), recover_return_sign)?
|
2019-08-11 17:59:27 +00:00
|
|
|
} 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()?;
|
2020-12-07 16:32:13 +00:00
|
|
|
if self.check_fn_front_matter(false) {
|
2020-10-15 19:21:45 +00:00
|
|
|
self.parse_ty_bare_fn(lo, lifetime_defs, recover_return_sign)?
|
2019-08-11 17:59:27 +00:00
|
|
|
} else {
|
|
|
|
let path = self.parse_path(PathStyle::Type)?;
|
2020-01-29 23:18:54 +00:00
|
|
|
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
|
2020-03-07 11:54:31 +00:00
|
|
|
self.parse_remaining_bounds_path(lifetime_defs, path, lo, parse_plus)?
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
} 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)?
|
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)
|
2020-03-07 12:15:58 +00:00
|
|
|
} else if self.check_path() {
|
2021-05-15 21:56:28 +00:00
|
|
|
self.parse_path_start_ty(lo, allow_plus, ty_generics)?
|
2020-03-07 12:52:55 +00:00
|
|
|
} else if self.can_begin_bound() {
|
|
|
|
self.parse_bare_trait_object(lo, allow_plus)?
|
2019-12-08 05:32:58 +00:00
|
|
|
} else if self.eat(&token::DotDotDot) {
|
2020-01-29 23:18:54 +00:00
|
|
|
if allow_c_variadic == AllowCVariadic::Yes {
|
2019-08-11 17:59:27 +00:00
|
|
|
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 {
|
2019-12-07 02:07:35 +00:00
|
|
|
let msg = format!("expected type, found {}", super::token_descr(&self.token));
|
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);
|
|
|
|
};
|
|
|
|
|
2020-02-29 11:56:15 +00:00
|
|
|
let span = lo.to(self.prev_token.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)?;
|
2022-03-04 02:03:55 +00:00
|
|
|
let ty = self.maybe_recover_from_question_mark(ty, recover_question_mark);
|
2022-05-19 05:51:49 +00:00
|
|
|
if allow_qpath_recovery { self.maybe_recover_from_bad_qpath(ty) } else { Ok(ty) }
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 05:44:24 +00:00
|
|
|
/// Parses either:
|
|
|
|
/// - `(TYPE)`, a parenthesized type.
|
|
|
|
/// - `(TYPE,)`, a tuple with a single field of type TYPE.
|
2020-01-29 00:57:24 +00:00
|
|
|
fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
|
2019-12-08 06:55:38 +00:00
|
|
|
let mut trailing_plus = false;
|
|
|
|
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
|
|
|
|
let ty = p.parse_ty()?;
|
2020-02-10 17:20:01 +00:00
|
|
|
trailing_plus = p.prev_token.kind == TokenKind::BinOp(token::Plus);
|
2019-12-08 06:55:38 +00:00
|
|
|
Ok(ty)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if ts.len() == 1 && !trailing {
|
2020-03-03 00:19:00 +00:00
|
|
|
let ty = ts.into_iter().next().unwrap().into_inner();
|
2020-01-29 23:18:54 +00:00
|
|
|
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
|
2019-12-08 05:44:24 +00:00
|
|
|
match ty.kind {
|
|
|
|
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
|
2019-12-08 06:55:38 +00:00
|
|
|
TyKind::Path(None, path) if maybe_bounds => {
|
2020-03-07 11:54:31 +00:00
|
|
|
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
|
2019-12-08 05:44:24 +00:00
|
|
|
}
|
2020-03-07 11:54:31 +00:00
|
|
|
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
|
2019-12-08 06:55:38 +00:00
|
|
|
if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
|
|
|
|
{
|
2020-03-07 11:54:31 +00:00
|
|
|
self.parse_remaining_bounds(bounds, true)
|
2019-12-08 05:44:24 +00:00
|
|
|
}
|
|
|
|
// `(TYPE)`
|
|
|
|
_ => Ok(TyKind::Paren(P(ty))),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(TyKind::Tup(ts))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:52:55 +00:00
|
|
|
fn parse_bare_trait_object(&mut self, lo: Span, allow_plus: AllowPlus) -> PResult<'a, TyKind> {
|
|
|
|
let lt_no_plus = self.check_lifetime() && !self.look_ahead(1, |t| t.is_like_plus());
|
|
|
|
let bounds = self.parse_generic_bounds_common(allow_plus, None)?;
|
|
|
|
if lt_no_plus {
|
2022-01-27 09:44:25 +00:00
|
|
|
self.struct_span_err(lo, "lifetime in trait object type must be followed by `+`")
|
|
|
|
.emit();
|
2020-03-07 12:52:55 +00:00
|
|
|
}
|
|
|
|
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None))
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:54:31 +00:00
|
|
|
fn parse_remaining_bounds_path(
|
2019-12-08 08:10:17 +00:00
|
|
|
&mut self,
|
|
|
|
generic_params: Vec<GenericParam>,
|
|
|
|
path: ast::Path,
|
|
|
|
lo: Span,
|
|
|
|
parse_plus: bool,
|
|
|
|
) -> PResult<'a, TyKind> {
|
2020-02-29 11:56:15 +00:00
|
|
|
let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_token.span));
|
2020-03-07 11:54:31 +00:00
|
|
|
let bounds = vec![GenericBound::Trait(poly_trait_ref, TraitBoundModifier::None)];
|
|
|
|
self.parse_remaining_bounds(bounds, parse_plus)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse the remainder of a bare trait object type given an already parsed list.
|
|
|
|
fn parse_remaining_bounds(
|
|
|
|
&mut self,
|
|
|
|
mut bounds: GenericBounds,
|
|
|
|
plus: bool,
|
|
|
|
) -> PResult<'a, TyKind> {
|
|
|
|
if plus {
|
2019-08-11 17:59:27 +00:00
|
|
|
self.eat_plus(); // `+`, or `+=` gets split and `+` is discarded
|
2020-02-29 11:56:15 +00:00
|
|
|
bounds.append(&mut self.parse_generic_bounds(Some(self.prev_token.span))?);
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
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(|| {
|
2020-02-29 11:56:15 +00:00
|
|
|
let span = self.prev_token.span;
|
2019-08-11 17:59:27 +00:00
|
|
|
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();
|
2019-12-16 16:28:40 +00:00
|
|
|
Mutability::Not
|
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> {
|
2020-10-25 21:20:44 +00:00
|
|
|
let elt_ty = match self.parse_ty() {
|
|
|
|
Ok(ty) => ty,
|
|
|
|
Err(mut err)
|
2022-04-26 12:40:14 +00:00
|
|
|
if self.look_ahead(1, |t| t.kind == token::CloseDelim(Delimiter::Bracket))
|
2020-10-25 21:20:44 +00:00
|
|
|
| self.look_ahead(1, |t| t.kind == token::Semi) =>
|
|
|
|
{
|
|
|
|
// Recover from `[LIT; EXPR]` and `[LIT]`
|
|
|
|
self.bump();
|
|
|
|
err.emit();
|
|
|
|
self.mk_ty(self.prev_token.span, TyKind::Err)
|
|
|
|
}
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
};
|
2021-02-27 10:37:50 +00:00
|
|
|
|
2019-12-08 07:19:53 +00:00
|
|
|
let ty = if self.eat(&token::Semi) {
|
2021-02-27 10:37:50 +00:00
|
|
|
let mut length = self.parse_anon_const_expr()?;
|
2022-04-26 12:40:14 +00:00
|
|
|
if let Err(e) = self.expect(&token::CloseDelim(Delimiter::Bracket)) {
|
2021-02-27 10:37:50 +00:00
|
|
|
// Try to recover from `X<Y, ...>` when `X::<Y, ...>` works
|
|
|
|
self.check_mistyped_turbofish_with_multiple_type_params(e, &mut length.value)?;
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Bracket))?;
|
2021-02-27 10:37:50 +00:00
|
|
|
}
|
|
|
|
TyKind::Array(elt_ty, length)
|
2019-08-11 17:59:27 +00:00
|
|
|
} else {
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Bracket))?;
|
2019-12-08 07:19:53 +00:00
|
|
|
TyKind::Slice(elt_ty)
|
|
|
|
};
|
2021-02-27 10:37:50 +00:00
|
|
|
|
2019-12-08 07:19:53 +00:00
|
|
|
Ok(ty)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_borrowed_pointee(&mut self) -> PResult<'a, TyKind> {
|
2020-06-21 20:37:17 +00:00
|
|
|
let and_span = self.prev_token.span;
|
|
|
|
let mut opt_lifetime =
|
|
|
|
if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
|
2021-07-02 15:07:32 +00:00
|
|
|
let mut mutbl = self.parse_mutability();
|
2020-06-21 20:37:17 +00:00
|
|
|
if self.token.is_lifetime() && mutbl == Mutability::Mut && opt_lifetime.is_none() {
|
|
|
|
// A lifetime is invalid here: it would be part of a bare trait bound, which requires
|
|
|
|
// it to be followed by a plus, but we disallow plus in the pointee type.
|
|
|
|
// So we can handle this case as an error here, and suggest `'a mut`.
|
|
|
|
// If there *is* a plus next though, handling the error later provides better suggestions
|
|
|
|
// (like adding parentheses)
|
|
|
|
if !self.look_ahead(1, |t| t.is_like_plus()) {
|
|
|
|
let lifetime_span = self.token.span;
|
|
|
|
let span = and_span.to(lifetime_span);
|
|
|
|
|
|
|
|
let mut err = self.struct_span_err(span, "lifetime must precede `mut`");
|
|
|
|
if let Ok(lifetime_src) = self.span_to_snippet(lifetime_span) {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"place the lifetime before `mut`",
|
|
|
|
format!("&{} mut", lifetime_src),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
|
|
|
|
opt_lifetime = Some(self.expect_lifetime());
|
|
|
|
}
|
2021-07-02 15:07:32 +00:00
|
|
|
} else if self.token.is_keyword(kw::Dyn)
|
|
|
|
&& mutbl == Mutability::Not
|
|
|
|
&& self.look_ahead(1, |t| t.is_keyword(kw::Mut))
|
|
|
|
{
|
|
|
|
// We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
|
|
|
|
let span = and_span.to(self.look_ahead(1, |t| t.span));
|
|
|
|
let mut err = self.struct_span_err(span, "`mut` must precede `dyn`");
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"place `mut` before `dyn`",
|
|
|
|
"&mut dyn".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
|
|
|
|
// Recovery
|
|
|
|
mutbl = Mutability::Mut;
|
|
|
|
let (dyn_tok, dyn_tok_sp) = (self.token.clone(), self.token_spacing);
|
|
|
|
self.bump();
|
|
|
|
self.bump_with((dyn_tok, dyn_tok_sp));
|
2020-06-21 20:37:17 +00:00
|
|
|
}
|
2019-08-11 17:59:27 +00:00
|
|
|
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)`.
|
2021-10-17 10:04:01 +00:00
|
|
|
// To avoid ambiguity, the type is surrounded by parentheses.
|
2019-12-08 07:23:10 +00:00
|
|
|
fn parse_typeof_ty(&mut self) -> PResult<'a, TyKind> {
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
|
2019-12-08 07:23:10 +00:00
|
|
|
let expr = self.parse_anon_const_expr()?;
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
2019-12-08 07:23:10 +00:00
|
|
|
Ok(TyKind::Typeof(expr))
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 11:40:20 +00:00
|
|
|
/// Parses a function pointer type (`TyKind::BareFn`).
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ignore (illustrative)
|
|
|
|
/// [unsafe] [extern "ABI"] fn (S) -> T
|
|
|
|
/// // ^~~~~^ ^~~~^ ^~^ ^
|
|
|
|
/// // | | | |
|
|
|
|
/// // | | | Return type
|
|
|
|
/// // Function Style ABI Parameter types
|
2019-12-08 11:40:20 +00:00
|
|
|
/// ```
|
2020-03-26 08:31:29 +00:00
|
|
|
/// We actually parse `FnHeader FnDecl`, but we error on `const` and `async` qualifiers.
|
2020-10-15 19:21:45 +00:00
|
|
|
fn parse_ty_bare_fn(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
params: Vec<GenericParam>,
|
|
|
|
recover_return_sign: RecoverReturnSign,
|
|
|
|
) -> PResult<'a, TyKind> {
|
2021-12-13 21:41:42 +00:00
|
|
|
let inherited_vis = rustc_ast::Visibility {
|
|
|
|
span: rustc_span::DUMMY_SP,
|
|
|
|
kind: rustc_ast::VisibilityKind::Inherited,
|
|
|
|
tokens: None,
|
|
|
|
};
|
2021-08-09 23:05:11 +00:00
|
|
|
let ast::FnHeader { ext, unsafety, constness, asyncness } =
|
2021-12-13 21:41:42 +00:00
|
|
|
self.parse_fn_front_matter(&inherited_vis)?;
|
2020-10-15 19:21:45 +00:00
|
|
|
let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
|
2020-03-26 08:31:29 +00:00
|
|
|
let whole_span = lo.to(self.prev_token.span);
|
|
|
|
if let ast::Const::Yes(span) = constness {
|
2022-03-26 22:23:32 +00:00
|
|
|
// If we ever start to allow `const fn()`, then update
|
|
|
|
// feature gating for `#![feature(const_extern_fn)]` to
|
|
|
|
// cover it.
|
2020-03-26 08:31:29 +00:00
|
|
|
self.error_fn_ptr_bad_qualifier(whole_span, span, "const");
|
|
|
|
}
|
|
|
|
if let ast::Async::Yes { span, .. } = asyncness {
|
|
|
|
self.error_fn_ptr_bad_qualifier(whole_span, span, "async");
|
|
|
|
}
|
|
|
|
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params: params, decl })))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error for the given bad function pointer qualifier.
|
|
|
|
fn error_fn_ptr_bad_qualifier(&self, span: Span, qual_span: Span, qual: &str) {
|
|
|
|
self.struct_span_err(span, &format!("an `fn` pointer type cannot be `{}`", qual))
|
|
|
|
.span_label(qual_span, format!("`{}` because of this", qual))
|
|
|
|
.span_suggestion_short(
|
|
|
|
qual_span,
|
|
|
|
&format!("remove the `{}` qualifier", qual),
|
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
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)?;
|
2020-02-10 17:20:01 +00:00
|
|
|
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
|
2019-12-08 07:29:12 +00:00
|
|
|
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)
|
2021-05-04 06:48:56 +00:00
|
|
|
&& (!self.token.uninterpolated_span().rust_2015()
|
2019-12-08 07:38:23 +00:00
|
|
|
|| 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)?;
|
2020-02-10 17:20:01 +00:00
|
|
|
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
|
2019-12-08 07:38:23 +00:00
|
|
|
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`.
|
2021-05-15 21:56:28 +00:00
|
|
|
fn parse_path_start_ty(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
allow_plus: AllowPlus,
|
|
|
|
ty_generics: Option<&Generics>,
|
|
|
|
) -> PResult<'a, TyKind> {
|
2019-12-08 07:49:20 +00:00
|
|
|
// Simple path
|
2021-05-15 21:56:28 +00:00
|
|
|
let path = self.parse_path_inner(PathStyle::Type, ty_generics)?;
|
2019-12-08 07:49:20 +00:00
|
|
|
if self.eat(&token::Not) {
|
|
|
|
// Macro invocation in type position
|
2020-02-29 16:32:20 +00:00
|
|
|
Ok(TyKind::MacCall(MacCall {
|
2019-12-08 07:49:20 +00:00
|
|
|
path,
|
|
|
|
args: self.parse_mac_args()?,
|
|
|
|
prior_type_ascription: self.last_type_ascription,
|
|
|
|
}))
|
2020-01-29 23:18:54 +00:00
|
|
|
} else if allow_plus == AllowPlus::Yes && self.check_plus() {
|
2019-12-08 07:49:20 +00:00
|
|
|
// `Trait1 + Trait2 + 'a`
|
2020-03-07 11:54:31 +00:00
|
|
|
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
|
2019-12-08 07:49:20 +00:00
|
|
|
} 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,
|
2020-02-29 11:56:15 +00:00
|
|
|
lo.to(self.prev_token.span),
|
2019-12-08 08:01:26 +00:00
|
|
|
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> {
|
2020-01-29 00:57:24 +00:00
|
|
|
self.parse_generic_bounds_common(AllowPlus::Yes, colon_span)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
|
|
|
|
///
|
2019-12-08 09:32:38 +00:00
|
|
|
/// See `parse_generic_bound` for the `BOUND` grammar.
|
2019-12-08 08:10:17 +00:00
|
|
|
fn parse_generic_bounds_common(
|
|
|
|
&mut self,
|
2020-01-29 00:57:24 +00:00
|
|
|
allow_plus: AllowPlus,
|
2019-12-08 08:10:17 +00:00
|
|
|
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();
|
2021-05-04 06:48:56 +00:00
|
|
|
|
|
|
|
while self.can_begin_bound() || self.token.is_keyword(kw::Dyn) {
|
|
|
|
if self.token.is_keyword(kw::Dyn) {
|
|
|
|
// Account for `&dyn Trait + dyn Other`.
|
|
|
|
self.struct_span_err(self.token.span, "invalid `dyn` keyword")
|
|
|
|
.help("`dyn` is only needed at the start of a trait `+`-separated list")
|
|
|
|
.span_suggestion(
|
|
|
|
self.token.span,
|
|
|
|
"remove this keyword",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
self.bump();
|
|
|
|
}
|
2019-12-08 10:49:25 +00:00
|
|
|
match self.parse_generic_bound()? {
|
2019-12-08 10:04:26 +00:00
|
|
|
Ok(bound) => bounds.push(bound),
|
2019-12-08 10:49:25 +00:00
|
|
|
Err(neg_sp) => negative_bounds.push(neg_sp),
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
2020-01-29 23:18:54 +00:00
|
|
|
if allow_plus == AllowPlus::No || !self.eat_plus() {
|
2019-08-11 17:59:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 10:49:25 +00:00
|
|
|
if !negative_bounds.is_empty() {
|
2019-12-08 11:29:05 +00:00
|
|
|
self.error_negative_bounds(colon_span, &bounds, negative_bounds);
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 09:32:38 +00:00
|
|
|
Ok(bounds)
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 09:04:31 +00:00
|
|
|
/// Can the current token begin a bound?
|
|
|
|
fn can_begin_bound(&mut self) -> bool {
|
|
|
|
// This needs to be synchronized with `TokenKind::can_begin_bound`.
|
|
|
|
self.check_path()
|
|
|
|
|| self.check_lifetime()
|
|
|
|
|| self.check(&token::Not) // Used for error reporting only.
|
|
|
|
|| self.check(&token::Question)
|
2021-08-25 11:53:16 +00:00
|
|
|
|| self.check(&token::Tilde)
|
2019-12-08 09:04:31 +00:00
|
|
|
|| self.check_keyword(kw::For)
|
2022-04-26 12:40:14 +00:00
|
|
|
|| self.check(&token::OpenDelim(Delimiter::Parenthesis))
|
2019-12-08 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 11:29:05 +00:00
|
|
|
fn error_negative_bounds(
|
|
|
|
&self,
|
|
|
|
colon_span: Option<Span>,
|
|
|
|
bounds: &[GenericBound],
|
|
|
|
negative_bounds: Vec<Span>,
|
|
|
|
) {
|
|
|
|
let negative_bounds_len = negative_bounds.len();
|
2019-12-21 02:28:22 +00:00
|
|
|
let last_span = *negative_bounds.last().expect("no negative bounds, but still error?");
|
2019-12-08 11:29:05 +00:00
|
|
|
let mut err = self.struct_span_err(negative_bounds, "negative bounds are not supported");
|
|
|
|
err.span_label(last_span, "negative bounds are not supported");
|
|
|
|
if let Some(bound_list) = colon_span {
|
2020-02-29 11:56:15 +00:00
|
|
|
let bound_list = bound_list.to(self.prev_token.span);
|
2019-12-08 11:29:05 +00:00
|
|
|
let mut new_bound_list = String::new();
|
|
|
|
if !bounds.is_empty() {
|
|
|
|
let mut snippets = bounds.iter().map(|bound| self.span_to_snippet(bound.span()));
|
|
|
|
while let Some(Ok(snippet)) = snippets.next() {
|
|
|
|
new_bound_list.push_str(" + ");
|
|
|
|
new_bound_list.push_str(&snippet);
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
2019-12-08 11:29:05 +00:00
|
|
|
new_bound_list = new_bound_list.replacen(" +", ":", 1);
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
2019-12-21 02:32:54 +00:00
|
|
|
err.tool_only_span_suggestion(
|
2019-12-08 11:29:05 +00:00
|
|
|
bound_list,
|
|
|
|
&format!("remove the bound{}", pluralize!(negative_bounds_len)),
|
|
|
|
new_bound_list,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
2019-12-08 11:29:05 +00:00
|
|
|
err.emit();
|
|
|
|
}
|
2019-08-11 17:59:27 +00:00
|
|
|
|
2019-12-08 09:32:38 +00:00
|
|
|
/// Parses a bound according to the grammar:
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ebnf
|
2019-12-08 09:32:38 +00:00
|
|
|
/// BOUND = TY_BOUND | LT_BOUND
|
|
|
|
/// ```
|
2019-12-08 11:19:53 +00:00
|
|
|
fn parse_generic_bound(&mut self) -> PResult<'a, Result<GenericBound, Span>> {
|
2020-02-29 11:56:15 +00:00
|
|
|
let anchor_lo = self.prev_token.span;
|
2019-12-08 09:32:38 +00:00
|
|
|
let lo = self.token.span;
|
2022-04-26 12:40:14 +00:00
|
|
|
let has_parens = self.eat(&token::OpenDelim(Delimiter::Parenthesis));
|
2019-12-08 09:32:38 +00:00
|
|
|
let inner_lo = self.token.span;
|
|
|
|
let is_negative = self.eat(&token::Not);
|
2020-01-02 23:50:18 +00:00
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
let modifiers = self.parse_ty_bound_modifiers()?;
|
2019-12-08 11:19:53 +00:00
|
|
|
let bound = if self.token.is_lifetime() {
|
2020-01-02 23:50:18 +00:00
|
|
|
self.error_lt_bound_with_modifiers(modifiers);
|
|
|
|
self.parse_generic_lt_bound(lo, inner_lo, has_parens)?
|
2019-12-08 09:32:38 +00:00
|
|
|
} else {
|
2020-01-02 23:50:18 +00:00
|
|
|
self.parse_generic_ty_bound(lo, has_parens, modifiers)?
|
2019-12-08 11:19:53 +00:00
|
|
|
};
|
2020-01-02 23:50:18 +00:00
|
|
|
|
2020-02-29 11:56:15 +00:00
|
|
|
Ok(if is_negative { Err(anchor_lo.to(self.prev_token.span)) } else { Ok(bound) })
|
2019-12-08 09:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 10:28:57 +00:00
|
|
|
/// Parses a lifetime ("outlives") bound, e.g. `'a`, according to:
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ebnf
|
2019-12-08 10:28:57 +00:00
|
|
|
/// LT_BOUND = LIFETIME
|
|
|
|
/// ```
|
|
|
|
fn parse_generic_lt_bound(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
inner_lo: Span,
|
|
|
|
has_parens: bool,
|
|
|
|
) -> PResult<'a, GenericBound> {
|
|
|
|
let bound = GenericBound::Outlives(self.expect_lifetime());
|
|
|
|
if has_parens {
|
|
|
|
// FIXME(Centril): Consider not erroring here and accepting `('lt)` instead,
|
|
|
|
// possibly introducing `GenericBound::Paren(P<GenericBound>)`?
|
|
|
|
self.recover_paren_lifetime(lo, inner_lo)?;
|
|
|
|
}
|
|
|
|
Ok(bound)
|
|
|
|
}
|
|
|
|
|
2020-01-02 23:50:18 +00:00
|
|
|
/// Emits an error if any trait bound modifiers were present.
|
|
|
|
fn error_lt_bound_with_modifiers(&self, modifiers: BoundModifiers) {
|
|
|
|
if let Some(span) = modifiers.maybe_const {
|
|
|
|
self.struct_span_err(
|
|
|
|
span,
|
2021-08-25 11:53:16 +00:00
|
|
|
"`~const` may only modify trait bounds, not lifetime bounds",
|
2020-01-02 23:50:18 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(span) = modifiers.maybe {
|
2019-12-08 08:18:34 +00:00
|
|
|
self.struct_span_err(span, "`?` may only modify trait bounds, not lifetime bounds")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 08:29:17 +00:00
|
|
|
/// Recover on `('lifetime)` with `(` already eaten.
|
|
|
|
fn recover_paren_lifetime(&mut self, lo: Span, inner_lo: Span) -> PResult<'a, ()> {
|
2020-02-29 11:56:15 +00:00
|
|
|
let inner_span = inner_lo.to(self.prev_token.span);
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
2019-12-08 08:29:17 +00:00
|
|
|
let mut err = self.struct_span_err(
|
2020-02-29 11:56:15 +00:00
|
|
|
lo.to(self.prev_token.span),
|
2019-12-08 08:29:17 +00:00
|
|
|
"parenthesized lifetime bounds are not supported",
|
|
|
|
);
|
|
|
|
if let Ok(snippet) = self.span_to_snippet(inner_span) {
|
|
|
|
err.span_suggestion_short(
|
2020-02-29 11:56:15 +00:00
|
|
|
lo.to(self.prev_token.span),
|
2019-12-08 08:29:17 +00:00
|
|
|
"remove the parentheses",
|
2020-01-15 14:00:25 +00:00
|
|
|
snippet,
|
2019-12-08 08:29:17 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
/// Parses the modifiers that may precede a trait in a bound, e.g. `?Trait` or `~const Trait`.
|
2020-01-02 23:50:18 +00:00
|
|
|
///
|
|
|
|
/// If no modifiers are present, this does not consume any tokens.
|
|
|
|
///
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ebnf
|
2021-08-25 11:53:16 +00:00
|
|
|
/// TY_BOUND_MODIFIERS = ["~const"] ["?"]
|
2020-01-02 23:50:18 +00:00
|
|
|
/// ```
|
2021-08-25 11:53:16 +00:00
|
|
|
fn parse_ty_bound_modifiers(&mut self) -> PResult<'a, BoundModifiers> {
|
|
|
|
let maybe_const = if self.eat(&token::Tilde) {
|
|
|
|
let tilde = self.prev_token.span;
|
|
|
|
self.expect_keyword(kw::Const)?;
|
|
|
|
let span = tilde.to(self.prev_token.span);
|
|
|
|
self.sess.gated_spans.gate(sym::const_trait_impl, span);
|
|
|
|
Some(span)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-01-02 23:50:18 +00:00
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
let maybe = if self.eat(&token::Question) { Some(self.prev_token.span) } else { None };
|
2020-01-02 23:50:18 +00:00
|
|
|
|
2021-08-25 11:53:16 +00:00
|
|
|
Ok(BoundModifiers { maybe, maybe_const })
|
2020-01-02 23:50:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 10:19:24 +00:00
|
|
|
/// Parses a type bound according to:
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ebnf
|
2019-12-08 10:19:24 +00:00
|
|
|
/// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
|
2020-01-02 23:50:18 +00:00
|
|
|
/// TY_BOUND_NOPAREN = [TY_BOUND_MODIFIERS] [for<LT_PARAM_DEFS>] SIMPLE_PATH
|
2019-12-08 10:19:24 +00:00
|
|
|
/// ```
|
2020-01-02 23:50:18 +00:00
|
|
|
///
|
2021-08-25 11:53:16 +00:00
|
|
|
/// For example, this grammar accepts `~const ?for<'a: 'b> m::Trait<'a>`.
|
2019-12-08 10:19:24 +00:00
|
|
|
fn parse_generic_ty_bound(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
has_parens: bool,
|
2020-01-02 23:50:18 +00:00
|
|
|
modifiers: BoundModifiers,
|
2019-12-08 11:19:53 +00:00
|
|
|
) -> PResult<'a, GenericBound> {
|
2019-12-08 10:19:24 +00:00
|
|
|
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
|
|
|
|
let path = self.parse_path(PathStyle::Type)?;
|
|
|
|
if has_parens {
|
2021-05-04 06:48:56 +00:00
|
|
|
if self.token.is_like_plus() {
|
|
|
|
// Someone has written something like `&dyn (Trait + Other)`. The correct code
|
|
|
|
// would be `&(dyn Trait + Other)`, but we don't have access to the appropriate
|
|
|
|
// span to suggest that. When written as `&dyn Trait + Other`, an appropriate
|
|
|
|
// suggestion is given.
|
|
|
|
let bounds = vec![];
|
|
|
|
self.parse_remaining_bounds(bounds, true)?;
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
2021-05-04 06:48:56 +00:00
|
|
|
let sp = vec![lo, self.prev_token.span];
|
|
|
|
let sugg: Vec<_> = sp.iter().map(|sp| (*sp, String::new())).collect();
|
|
|
|
self.struct_span_err(sp, "incorrect braces around trait bounds")
|
|
|
|
.multipart_suggestion(
|
|
|
|
"remove the parentheses",
|
|
|
|
sugg,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
} else {
|
2022-04-26 12:40:14 +00:00
|
|
|
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
|
2021-05-04 06:48:56 +00:00
|
|
|
}
|
2019-12-08 10:19:24 +00:00
|
|
|
}
|
2020-01-02 23:50:18 +00:00
|
|
|
|
2020-01-20 09:20:45 +00:00
|
|
|
let modifier = modifiers.to_trait_bound_modifier();
|
2020-02-29 11:56:15 +00:00
|
|
|
let poly_trait = PolyTraitRef::new(lifetime_defs, path, lo.to(self.prev_token.span));
|
2020-01-14 04:30:27 +00:00
|
|
|
Ok(GenericBound::Trait(poly_trait, modifier))
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 11:29:55 +00:00
|
|
|
/// Optionally parses `for<$generic_params>`.
|
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
|
|
|
|
2020-07-27 12:04:54 +00:00
|
|
|
pub(super) 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.
|
2020-07-27 12:04:54 +00:00
|
|
|
pub(super) fn expect_lifetime(&mut self) -> Lifetime {
|
2019-08-11 18:46:34 +00:00
|
|
|
if let Some(ident) = self.token.lifetime() {
|
|
|
|
self.bump();
|
2020-02-24 10:04:13 +00:00
|
|
|
Lifetime { ident, id: ast::DUMMY_NODE_ID }
|
2019-08-11 18:46:34 +00:00
|
|
|
} 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> {
|
2020-08-21 22:18:04 +00:00
|
|
|
P(Ty { kind, span, id: ast::DUMMY_NODE_ID, tokens: None })
|
2019-10-08 12:39:58 +00:00
|
|
|
}
|
2019-08-11 17:59:27 +00:00
|
|
|
}
|