2020-10-14 22:27:48 +02:00
|
|
|
use super::ty::{AllowPlus, RecoverFatArrow, RecoverQPath};
|
2019-10-11 13:06:36 +02:00
|
|
|
use super::{Parser, TokenType};
|
2019-10-15 22:48:13 +02:00
|
|
|
use crate::maybe_whole;
|
2020-03-22 06:09:24 +01:00
|
|
|
use rustc_ast::ptr::P;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::token::{self, Token};
|
2020-11-19 18:28:38 +01:00
|
|
|
use rustc_ast::{self as ast, AngleBracketedArg, AngleBracketedArgs, ParenthesizedArgs};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{AnonConst, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode};
|
2020-11-19 18:28:38 +01:00
|
|
|
use rustc_ast::{GenericArg, GenericArgs};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{Path, PathSegment, QSelf};
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_errors::{pluralize, Applicability, PResult};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::source_map::{BytePos, Span};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident};
|
2019-08-11 19:59:27 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::mem;
|
2020-08-13 23:05:01 -07:00
|
|
|
use tracing::debug;
|
2019-08-11 19:59:27 +02:00
|
|
|
|
|
|
|
/// Specifies how to parse a path.
|
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2019-10-16 10:59:30 +02:00
|
|
|
pub enum PathStyle {
|
2019-08-11 19:59:27 +02:00
|
|
|
/// In some contexts, notably in expressions, paths with generic arguments are ambiguous
|
|
|
|
/// with something else. For example, in expressions `segment < ....` can be interpreted
|
|
|
|
/// as a comparison and `segment ( ....` can be interpreted as a function call.
|
|
|
|
/// In all such contexts the non-path interpretation is preferred by default for practical
|
|
|
|
/// reasons, but the path interpretation can be forced by the disambiguator `::`, e.g.
|
|
|
|
/// `x<y>` - comparisons, `x::<y>` - unambiguously a path.
|
|
|
|
Expr,
|
|
|
|
/// In other contexts, notably in types, no ambiguity exists and paths can be written
|
|
|
|
/// without the disambiguator, e.g., `x<y>` - unambiguously a path.
|
|
|
|
/// Paths with disambiguators are still accepted, `x::<Y>` - unambiguously a path too.
|
|
|
|
Type,
|
|
|
|
/// A path with generic arguments disallowed, e.g., `foo::bar::Baz`, used in imports,
|
|
|
|
/// visibilities or attributes.
|
|
|
|
/// Technically, this variant is unnecessary and e.g., `Expr` can be used instead
|
|
|
|
/// (paths in "mod" contexts have to be checked later for absence of generic arguments
|
|
|
|
/// anyway, due to macros), but it is used to avoid weird suggestions about expected
|
|
|
|
/// tokens when something goes wrong.
|
|
|
|
Mod,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
/// Parses a qualified path.
|
|
|
|
/// Assumes that the leading `<` has been parsed already.
|
|
|
|
///
|
|
|
|
/// `qualified_path = <type [as trait_ref]>::path`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// `<T>::default`
|
|
|
|
/// `<T as U>::a`
|
|
|
|
/// `<T as U>::F::a<S>` (without disambiguator)
|
|
|
|
/// `<T as U>::F::a::<S>` (with disambiguator)
|
|
|
|
pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Path)> {
|
2020-02-29 14:56:15 +03:00
|
|
|
let lo = self.prev_token.span;
|
2019-08-11 19:59:27 +02:00
|
|
|
let ty = self.parse_ty()?;
|
|
|
|
|
|
|
|
// `path` will contain the prefix of the path up to the `>`,
|
|
|
|
// if any (e.g., `U` in the `<T as U>::*` examples
|
|
|
|
// above). `path_span` has the span of that path, or an empty
|
|
|
|
// span in the case of something like `<T>::Bar`.
|
|
|
|
let (mut path, path_span);
|
|
|
|
if self.eat_keyword(kw::As) {
|
|
|
|
let path_lo = self.token.span;
|
|
|
|
path = self.parse_path(PathStyle::Type)?;
|
2020-02-29 14:56:15 +03:00
|
|
|
path_span = path_lo.to(self.prev_token.span);
|
2019-08-11 19:59:27 +02:00
|
|
|
} else {
|
|
|
|
path_span = self.token.span.to(self.token.span);
|
2020-08-21 18:51:23 -04:00
|
|
|
path = ast::Path { segments: Vec::new(), span: path_span, tokens: None };
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// See doc comment for `unmatched_angle_bracket_count`.
|
|
|
|
self.expect(&token::Gt)?;
|
|
|
|
if self.unmatched_angle_bracket_count > 0 {
|
|
|
|
self.unmatched_angle_bracket_count -= 1;
|
|
|
|
debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
|
|
|
|
}
|
|
|
|
|
2020-02-01 19:10:42 +00:00
|
|
|
if !self.recover_colon_before_qpath_proj() {
|
2020-01-29 20:34:28 +00:00
|
|
|
self.expect(&token::ModSep)?;
|
|
|
|
}
|
2019-08-11 19:59:27 +02:00
|
|
|
|
|
|
|
let qself = QSelf { ty, path_span, position: path.segments.len() };
|
|
|
|
self.parse_path_segments(&mut path.segments, style)?;
|
|
|
|
|
2020-08-21 18:51:23 -04:00
|
|
|
Ok((
|
|
|
|
qself,
|
|
|
|
Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
|
|
|
|
))
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 19:21:54 +00:00
|
|
|
/// Recover from an invalid single colon, when the user likely meant a qualified path.
|
2020-02-01 19:24:51 +00:00
|
|
|
/// We avoid emitting this if not followed by an identifier, as our assumption that the user
|
|
|
|
/// intended this to be a qualified path may not be correct.
|
2020-02-01 19:21:54 +00:00
|
|
|
///
|
|
|
|
/// ```ignore (diagnostics)
|
|
|
|
/// <Bar as Baz<T>>:Qux
|
|
|
|
/// ^ help: use double colon
|
|
|
|
/// ```
|
2020-02-01 19:10:42 +00:00
|
|
|
fn recover_colon_before_qpath_proj(&mut self) -> bool {
|
2020-02-01 19:24:51 +00:00
|
|
|
if self.token.kind != token::Colon
|
|
|
|
|| self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident())
|
|
|
|
{
|
2020-02-01 19:10:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-01 19:21:54 +00:00
|
|
|
self.bump(); // colon
|
2020-02-01 19:10:42 +00:00
|
|
|
|
|
|
|
self.diagnostic()
|
2020-02-01 19:21:54 +00:00
|
|
|
.struct_span_err(
|
2020-02-29 14:56:15 +03:00
|
|
|
self.prev_token.span,
|
2020-02-01 19:21:54 +00:00
|
|
|
"found single colon before projection in qualified path",
|
|
|
|
)
|
2020-02-01 19:10:42 +00:00
|
|
|
.span_suggestion(
|
2020-02-29 14:56:15 +03:00
|
|
|
self.prev_token.span,
|
2020-02-01 19:10:42 +00:00
|
|
|
"use double colon",
|
|
|
|
"::".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-08-11 19:59:27 +02:00
|
|
|
/// Parses simple paths.
|
|
|
|
///
|
|
|
|
/// `path = [::] segment+`
|
|
|
|
/// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// `a::b::C<D>` (without disambiguator)
|
|
|
|
/// `a::b::C::<D>` (with disambiguator)
|
|
|
|
/// `Fn(Args)` (without disambiguator)
|
|
|
|
/// `Fn::(Args)` (with disambiguator)
|
2020-07-27 14:04:54 +02:00
|
|
|
pub(super) fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
|
2019-08-11 19:59:27 +02:00
|
|
|
maybe_whole!(self, NtPath, |path| {
|
2019-12-22 17:42:04 -05:00
|
|
|
if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
|
|
|
|
{
|
2019-12-30 15:09:42 +01:00
|
|
|
self.struct_span_err(path.span, "unexpected generic arguments in path").emit();
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
path
|
|
|
|
});
|
|
|
|
|
2020-02-24 13:04:13 +03:00
|
|
|
let lo = self.token.span;
|
2019-08-11 19:59:27 +02:00
|
|
|
let mut segments = Vec::new();
|
|
|
|
let mod_sep_ctxt = self.token.span.ctxt();
|
|
|
|
if self.eat(&token::ModSep) {
|
|
|
|
segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
|
|
|
|
}
|
|
|
|
self.parse_path_segments(&mut segments, style)?;
|
|
|
|
|
2020-08-21 18:51:23 -04:00
|
|
|
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:35:34 +02:00
|
|
|
pub(super) fn parse_path_segments(
|
2019-09-04 17:36:01 -07:00
|
|
|
&mut self,
|
|
|
|
segments: &mut Vec<PathSegment>,
|
|
|
|
style: PathStyle,
|
|
|
|
) -> PResult<'a, ()> {
|
2019-08-11 19:59:27 +02:00
|
|
|
loop {
|
|
|
|
let segment = self.parse_path_segment(style)?;
|
|
|
|
if style == PathStyle::Expr {
|
|
|
|
// In order to check for trailing angle brackets, we must have finished
|
|
|
|
// recursing (`parse_path_segment` can indirectly call this function),
|
|
|
|
// that is, the next token must be the highlighted part of the below example:
|
|
|
|
//
|
|
|
|
// `Foo::<Bar as Baz<T>>::Qux`
|
|
|
|
// ^ here
|
|
|
|
//
|
|
|
|
// As opposed to the below highlight (if we had only finished the first
|
|
|
|
// recursion):
|
|
|
|
//
|
|
|
|
// `Foo::<Bar as Baz<T>>::Qux`
|
|
|
|
// ^ here
|
|
|
|
//
|
|
|
|
// `PathStyle::Expr` is only provided at the root invocation and never in
|
|
|
|
// `parse_path_segment` to recurse and therefore can be checked to maintain
|
|
|
|
// this invariant.
|
2020-06-27 11:35:12 -04:00
|
|
|
self.check_trailing_angle_brackets(&segment, &[&token::ModSep]);
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
segments.push(segment);
|
|
|
|
|
|
|
|
if self.is_import_coupler() || !self.eat(&token::ModSep) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_path_segment(&mut self, style: PathStyle) -> PResult<'a, PathSegment> {
|
|
|
|
let ident = self.parse_path_segment_ident()?;
|
|
|
|
|
2020-10-26 21:02:48 -04:00
|
|
|
let is_args_start = |token: &Token| {
|
|
|
|
matches!(
|
|
|
|
token.kind,
|
|
|
|
token::Lt
|
|
|
|
| token::BinOp(token::Shl)
|
|
|
|
| token::OpenDelim(token::Paren)
|
|
|
|
| token::LArrow
|
|
|
|
)
|
2019-08-11 19:59:27 +02:00
|
|
|
};
|
|
|
|
let check_args_start = |this: &mut Self| {
|
2019-12-22 17:42:04 -05:00
|
|
|
this.expected_tokens.extend_from_slice(&[
|
|
|
|
TokenType::Token(token::Lt),
|
|
|
|
TokenType::Token(token::OpenDelim(token::Paren)),
|
|
|
|
]);
|
2019-08-11 19:59:27 +02:00
|
|
|
is_args_start(&this.token)
|
|
|
|
};
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(
|
|
|
|
if style == PathStyle::Type && check_args_start(self)
|
|
|
|
|| style != PathStyle::Mod
|
|
|
|
&& self.check(&token::ModSep)
|
|
|
|
&& self.look_ahead(1, |t| is_args_start(t))
|
|
|
|
{
|
|
|
|
// We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
|
|
|
|
// it isn't, then we reset the unmatched angle bracket count as we're about to start
|
|
|
|
// parsing a new path.
|
|
|
|
if style == PathStyle::Expr {
|
|
|
|
self.unmatched_angle_bracket_count = 0;
|
|
|
|
self.max_angle_bracket_count = 0;
|
|
|
|
}
|
2019-08-11 19:59:27 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
|
|
|
|
self.eat(&token::ModSep);
|
|
|
|
let lo = self.token.span;
|
|
|
|
let args = if self.eat_lt() {
|
|
|
|
// `<'a, T, A = U>`
|
2020-03-22 04:40:05 +01:00
|
|
|
let args =
|
|
|
|
self.parse_angle_args_with_leading_angle_bracket_recovery(style, lo)?;
|
2019-12-22 17:42:04 -05:00
|
|
|
self.expect_gt()?;
|
2020-02-29 14:56:15 +03:00
|
|
|
let span = lo.to(self.prev_token.span);
|
2020-03-22 04:40:05 +01:00
|
|
|
AngleBracketedArgs { args, span }.into()
|
2019-12-22 17:42:04 -05:00
|
|
|
} else {
|
|
|
|
// `(T, U) -> R`
|
|
|
|
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
|
2020-02-29 14:56:15 +03:00
|
|
|
let span = ident.span.to(self.prev_token.span);
|
2020-10-14 22:27:48 +02:00
|
|
|
let output =
|
|
|
|
self.parse_ret_ty(AllowPlus::No, RecoverQPath::No, RecoverFatArrow::No)?;
|
2019-12-22 17:42:04 -05:00
|
|
|
ParenthesizedArgs { inputs, output, span }.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
|
2019-08-11 19:59:27 +02:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
// Generic arguments are not found.
|
|
|
|
PathSegment::from_ident(ident)
|
|
|
|
},
|
|
|
|
)
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
|
2020-03-04 23:37:52 +03:00
|
|
|
match self.token.ident() {
|
|
|
|
Some((ident, false)) if ident.is_path_segment_keyword() => {
|
2019-08-11 19:59:27 +02:00
|
|
|
self.bump();
|
2020-03-04 23:37:52 +03:00
|
|
|
Ok(ident)
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
_ => self.parse_ident(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses generic args (within a path segment) with recovery for extra leading angle brackets.
|
|
|
|
/// For the purposes of understanding the parsing logic of generic arguments, this function
|
2020-03-22 04:40:05 +01:00
|
|
|
/// can be thought of being the same as just calling `self.parse_angle_args()` if the source
|
2019-08-11 19:59:27 +02:00
|
|
|
/// had the correct amount of leading angle brackets.
|
|
|
|
///
|
|
|
|
/// ```ignore (diagnostics)
|
|
|
|
/// bar::<<<<T as Foo>::Output>();
|
|
|
|
/// ^^ help: remove extra angle brackets
|
|
|
|
/// ```
|
2020-03-22 04:40:05 +01:00
|
|
|
fn parse_angle_args_with_leading_angle_bracket_recovery(
|
2019-08-11 19:59:27 +02:00
|
|
|
&mut self,
|
|
|
|
style: PathStyle,
|
|
|
|
lo: Span,
|
2020-03-22 04:40:05 +01:00
|
|
|
) -> PResult<'a, Vec<AngleBracketedArg>> {
|
2019-08-11 19:59:27 +02:00
|
|
|
// We need to detect whether there are extra leading left angle brackets and produce an
|
|
|
|
// appropriate error and suggestion. This cannot be implemented by looking ahead at
|
|
|
|
// upcoming tokens for a matching `>` character - if there are unmatched `<` tokens
|
|
|
|
// then there won't be matching `>` tokens to find.
|
|
|
|
//
|
|
|
|
// To explain how this detection works, consider the following example:
|
|
|
|
//
|
|
|
|
// ```ignore (diagnostics)
|
|
|
|
// bar::<<<<T as Foo>::Output>();
|
|
|
|
// ^^ help: remove extra angle brackets
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Parsing of the left angle brackets starts in this function. We start by parsing the
|
|
|
|
// `<` token (incrementing the counter of unmatched angle brackets on `Parser` via
|
|
|
|
// `eat_lt`):
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `<<<<T as Foo>::Output>;`
|
|
|
|
// *Unmatched count:* 1
|
|
|
|
// *`parse_path_segment` calls deep:* 0
|
|
|
|
//
|
|
|
|
// This has the effect of recursing as this function is called if a `<` character
|
|
|
|
// is found within the expected generic arguments:
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `<<<T as Foo>::Output>;`
|
|
|
|
// *Unmatched count:* 2
|
|
|
|
// *`parse_path_segment` calls deep:* 1
|
|
|
|
//
|
|
|
|
// Eventually we will have recursed until having consumed all of the `<` tokens and
|
|
|
|
// this will be reflected in the count:
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `T as Foo>::Output>;`
|
|
|
|
// *Unmatched count:* 4
|
|
|
|
// `parse_path_segment` calls deep:* 3
|
|
|
|
//
|
|
|
|
// The parser will continue until reaching the first `>` - this will decrement the
|
|
|
|
// unmatched angle bracket count and return to the parent invocation of this function
|
|
|
|
// having succeeded in parsing:
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `::Output>;`
|
|
|
|
// *Unmatched count:* 3
|
|
|
|
// *`parse_path_segment` calls deep:* 2
|
|
|
|
//
|
|
|
|
// This will continue until the next `>` character which will also return successfully
|
|
|
|
// to the parent invocation of this function and decrement the count:
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `;`
|
|
|
|
// *Unmatched count:* 2
|
|
|
|
// *`parse_path_segment` calls deep:* 1
|
|
|
|
//
|
|
|
|
// At this point, this function will expect to find another matching `>` character but
|
|
|
|
// won't be able to and will return an error. This will continue all the way up the
|
|
|
|
// call stack until the first invocation:
|
|
|
|
//
|
|
|
|
// *Upcoming tokens:* `;`
|
|
|
|
// *Unmatched count:* 2
|
|
|
|
// *`parse_path_segment` calls deep:* 0
|
|
|
|
//
|
|
|
|
// In doing this, we have managed to work out how many unmatched leading left angle
|
|
|
|
// brackets there are, but we cannot recover as the unmatched angle brackets have
|
|
|
|
// already been consumed. To remedy this, we keep a snapshot of the parser state
|
|
|
|
// before we do the above. We can then inspect whether we ended up with a parsing error
|
|
|
|
// and unmatched left angle brackets and if so, restore the parser state before we
|
|
|
|
// consumed any `<` characters to emit an error and consume the erroneous tokens to
|
|
|
|
// recover by attempting to parse again.
|
|
|
|
//
|
|
|
|
// In practice, the recursion of this function is indirect and there will be other
|
|
|
|
// locations that consume some `<` characters - as long as we update the count when
|
|
|
|
// this happens, it isn't an issue.
|
|
|
|
|
|
|
|
let is_first_invocation = style == PathStyle::Expr;
|
|
|
|
// Take a snapshot before attempting to parse - we can restore this later.
|
2019-12-22 17:42:04 -05:00
|
|
|
let snapshot = if is_first_invocation { Some(self.clone()) } else { None };
|
2019-08-11 19:59:27 +02:00
|
|
|
|
|
|
|
debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
|
2020-03-22 04:40:05 +01:00
|
|
|
match self.parse_angle_args() {
|
|
|
|
Ok(args) => Ok(args),
|
2019-08-11 19:59:27 +02:00
|
|
|
Err(ref mut e) if is_first_invocation && self.unmatched_angle_bracket_count > 0 => {
|
|
|
|
// Cancel error from being unable to find `>`. We know the error
|
|
|
|
// must have been this due to a non-zero unmatched angle bracket
|
|
|
|
// count.
|
|
|
|
e.cancel();
|
|
|
|
|
|
|
|
// Swap `self` with our backup of the parser state before attempting to parse
|
|
|
|
// generic arguments.
|
|
|
|
let snapshot = mem::replace(self, snapshot.unwrap());
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"parse_generic_args_with_leading_angle_bracket_recovery: (snapshot failure) \
|
|
|
|
snapshot.count={:?}",
|
|
|
|
snapshot.unmatched_angle_bracket_count,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Eat the unmatched angle brackets.
|
|
|
|
for _ in 0..snapshot.unmatched_angle_bracket_count {
|
|
|
|
self.eat_lt();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a span over ${unmatched angle bracket count} characters.
|
2019-12-22 17:42:04 -05:00
|
|
|
let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
|
2019-12-30 14:56:57 +01:00
|
|
|
self.struct_span_err(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"unmatched angle bracket{}",
|
|
|
|
pluralize!(snapshot.unmatched_angle_bracket_count)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.span_suggestion(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"remove extra angle bracket{}",
|
|
|
|
pluralize!(snapshot.unmatched_angle_bracket_count)
|
|
|
|
),
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
|
|
|
.emit();
|
2019-08-11 19:59:27 +02:00
|
|
|
|
|
|
|
// Try again without unmatched angle bracket characters.
|
2020-03-22 04:40:05 +01:00
|
|
|
self.parse_angle_args()
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-08-11 19:59:27 +02:00
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 04:40:05 +01:00
|
|
|
/// Parses (possibly empty) list of generic arguments / associated item constraints,
|
2019-08-11 19:59:27 +02:00
|
|
|
/// possibly including trailing comma.
|
2020-07-23 09:34:07 -07:00
|
|
|
pub(super) fn parse_angle_args(&mut self) -> PResult<'a, Vec<AngleBracketedArg>> {
|
2019-08-11 19:59:27 +02:00
|
|
|
let mut args = Vec::new();
|
2020-03-22 04:54:46 +01:00
|
|
|
while let Some(arg) = self.parse_angle_arg()? {
|
|
|
|
args.push(arg);
|
2019-08-11 19:59:27 +02:00
|
|
|
if !self.eat(&token::Comma) {
|
2020-10-03 19:30:32 +01:00
|
|
|
if !self.token.kind.should_end_const_arg() {
|
|
|
|
if self.handle_ambiguous_unbraced_const_arg(&mut args)? {
|
|
|
|
// We've managed to (partially) recover, so continue trying to parse
|
|
|
|
// arguments.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
break;
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 04:40:05 +01:00
|
|
|
Ok(args)
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|
2020-03-22 04:54:46 +01:00
|
|
|
|
|
|
|
/// Parses a single argument in the angle arguments `<...>` of a path segment.
|
|
|
|
fn parse_angle_arg(&mut self) -> PResult<'a, Option<AngleBracketedArg>> {
|
2020-11-19 18:28:38 +01:00
|
|
|
let lo = self.token.span;
|
|
|
|
let arg = self.parse_generic_arg()?;
|
|
|
|
match arg {
|
|
|
|
Some(arg) => {
|
|
|
|
if self.check(&token::Colon) | self.check(&token::Eq) {
|
|
|
|
let (ident, gen_args) = self.get_ident_from_generic_arg(arg, lo)?;
|
|
|
|
let kind = if self.eat(&token::Colon) {
|
|
|
|
// Parse associated type constraint bound.
|
|
|
|
|
|
|
|
let bounds = self.parse_generic_bounds(Some(self.prev_token.span))?;
|
|
|
|
AssocTyConstraintKind::Bound { bounds }
|
|
|
|
} else if self.eat(&token::Eq) {
|
|
|
|
// Parse associated type equality constraint
|
|
|
|
|
|
|
|
let ty = self.parse_assoc_equality_term(ident, self.prev_token.span)?;
|
|
|
|
AssocTyConstraintKind::Equality { ty }
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
2020-03-22 04:54:46 +01:00
|
|
|
|
2020-11-19 18:28:38 +01:00
|
|
|
let span = lo.to(self.prev_token.span);
|
2020-03-22 04:54:46 +01:00
|
|
|
|
2020-11-19 18:28:38 +01:00
|
|
|
// Gate associated type bounds, e.g., `Iterator<Item: Ord>`.
|
|
|
|
if let AssocTyConstraintKind::Bound { .. } = kind {
|
|
|
|
self.sess.gated_spans.gate(sym::associated_type_bounds, span);
|
|
|
|
}
|
|
|
|
let constraint =
|
|
|
|
AssocTyConstraint { id: ast::DUMMY_NODE_ID, ident, gen_args, kind, span };
|
|
|
|
Ok(Some(AngleBracketedArg::Constraint(constraint)))
|
|
|
|
} else {
|
|
|
|
Ok(Some(AngleBracketedArg::Arg(arg)))
|
|
|
|
}
|
2020-03-22 04:54:46 +01:00
|
|
|
}
|
2020-11-19 18:28:38 +01:00
|
|
|
_ => Ok(None),
|
2020-03-22 05:12:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 06:09:24 +01:00
|
|
|
/// Parse the term to the right of an associated item equality constraint.
|
|
|
|
/// That is, parse `<term>` in `Item = <term>`.
|
|
|
|
/// Right now, this only admits types in `<term>`.
|
|
|
|
fn parse_assoc_equality_term(&mut self, ident: Ident, eq: Span) -> PResult<'a, P<ast::Ty>> {
|
|
|
|
let arg = self.parse_generic_arg()?;
|
|
|
|
let span = ident.span.to(self.prev_token.span);
|
|
|
|
match arg {
|
|
|
|
Some(GenericArg::Type(ty)) => return Ok(ty),
|
|
|
|
Some(GenericArg::Const(expr)) => {
|
|
|
|
self.struct_span_err(span, "cannot constrain an associated constant to a value")
|
2020-03-27 07:39:10 +01:00
|
|
|
.span_label(ident.span, "this associated constant...")
|
|
|
|
.span_label(expr.value.span, "...cannot be constrained to this value")
|
2020-03-22 06:09:24 +01:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
Some(GenericArg::Lifetime(lt)) => {
|
|
|
|
self.struct_span_err(span, "associated lifetimes are not supported")
|
|
|
|
.span_label(lt.ident.span, "the lifetime is given here")
|
|
|
|
.help("if you meant to specify a trait object, write `dyn Trait + 'lifetime`")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
None => {
|
2020-03-27 07:39:10 +01:00
|
|
|
let after_eq = eq.shrink_to_hi();
|
|
|
|
let before_next = self.token.span.shrink_to_lo();
|
|
|
|
self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`")
|
2020-03-22 06:09:24 +01:00
|
|
|
.span_suggestion(
|
2020-03-27 07:39:10 +01:00
|
|
|
self.sess.source_map().next_point(eq).to(before_next),
|
2020-03-22 06:09:24 +01:00
|
|
|
"to constrain the associated type, add a type after `=`",
|
2020-03-27 07:39:10 +01:00
|
|
|
" TheType".to_string(),
|
2020-03-22 06:09:24 +01:00
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
|
|
|
.span_suggestion(
|
2020-03-27 07:39:10 +01:00
|
|
|
eq.to(before_next),
|
2020-03-22 06:09:24 +01:00
|
|
|
&format!("remove the `=` if `{}` is a type", ident),
|
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(self.mk_ty(span, ast::TyKind::Err))
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:30:32 +01:00
|
|
|
/// We do not permit arbitrary expressions as const arguments. They must be one of:
|
|
|
|
/// - An expression surrounded in `{}`.
|
|
|
|
/// - A literal.
|
|
|
|
/// - A numeric literal prefixed by `-`.
|
2020-11-18 12:49:39 +00:00
|
|
|
/// - A single-segment path.
|
2020-10-03 19:30:32 +01:00
|
|
|
pub(super) fn expr_is_valid_const_arg(&self, expr: &P<rustc_ast::Expr>) -> bool {
|
|
|
|
match &expr.kind {
|
|
|
|
ast::ExprKind::Block(_, _) | ast::ExprKind::Lit(_) => true,
|
|
|
|
ast::ExprKind::Unary(ast::UnOp::Neg, expr) => match &expr.kind {
|
|
|
|
ast::ExprKind::Lit(_) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
2020-11-18 12:49:39 +00:00
|
|
|
// We can only resolve single-segment paths at the moment, because multi-segment paths
|
|
|
|
// require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
|
|
|
|
ast::ExprKind::Path(None, path)
|
|
|
|
if path.segments.len() == 1 && path.segments[0].args.is_none() =>
|
|
|
|
{
|
|
|
|
true
|
|
|
|
}
|
2020-10-03 19:30:32 +01:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 05:12:51 +01:00
|
|
|
/// Parse a generic argument in a path segment.
|
|
|
|
/// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
|
|
|
|
fn parse_generic_arg(&mut self) -> PResult<'a, Option<GenericArg>> {
|
2020-10-03 19:30:32 +01:00
|
|
|
let start = self.token.span;
|
2020-03-22 05:12:51 +01:00
|
|
|
let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
|
2020-03-22 05:01:38 +01:00
|
|
|
// Parse lifetime argument.
|
2020-03-22 05:12:51 +01:00
|
|
|
GenericArg::Lifetime(self.expect_lifetime())
|
2020-03-22 04:54:46 +01:00
|
|
|
} else if self.check_const_arg() {
|
|
|
|
// Parse const argument.
|
2020-10-03 19:30:32 +01:00
|
|
|
let value = if let token::OpenDelim(token::Brace) = self.token.kind {
|
2020-03-22 04:54:46 +01:00
|
|
|
self.parse_block_expr(
|
|
|
|
None,
|
|
|
|
self.token.span,
|
|
|
|
BlockCheckMode::Default,
|
|
|
|
ast::AttrVec::new(),
|
|
|
|
)?
|
|
|
|
} else {
|
2020-10-03 19:30:32 +01:00
|
|
|
self.handle_unambiguous_unbraced_const_arg()?
|
2020-03-22 04:54:46 +01:00
|
|
|
};
|
2020-10-03 19:30:32 +01:00
|
|
|
GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })
|
2020-03-22 04:54:46 +01:00
|
|
|
} else if self.check_type() {
|
|
|
|
// Parse type argument.
|
2020-10-03 19:30:32 +01:00
|
|
|
match self.parse_ty() {
|
|
|
|
Ok(ty) => GenericArg::Type(ty),
|
|
|
|
Err(err) => {
|
|
|
|
// Try to recover from possible `const` arg without braces.
|
|
|
|
return self.recover_const_arg(start, err).map(Some);
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 04:54:46 +01:00
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
Ok(Some(arg))
|
|
|
|
}
|
2020-11-19 18:28:38 +01:00
|
|
|
|
|
|
|
fn get_ident_from_generic_arg(
|
|
|
|
&self,
|
|
|
|
gen_arg: GenericArg,
|
|
|
|
lo: Span,
|
|
|
|
) -> PResult<'a, (Ident, Option<GenericArgs>)> {
|
|
|
|
let gen_arg_span = gen_arg.span();
|
|
|
|
match gen_arg {
|
|
|
|
GenericArg::Type(t) => match t.into_inner().kind {
|
|
|
|
ast::TyKind::Path(qself, mut path) => {
|
|
|
|
if let Some(qself) = qself {
|
|
|
|
let mut err = self.struct_span_err(
|
|
|
|
gen_arg_span,
|
|
|
|
"qualified paths cannot be used in associated type constraints",
|
|
|
|
);
|
|
|
|
err.span_label(
|
|
|
|
qself.path_span,
|
|
|
|
"not allowed in associated type constraints",
|
|
|
|
);
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
if path.segments.len() == 1 {
|
|
|
|
let path_seg = path.segments.remove(0);
|
|
|
|
let ident = path_seg.ident;
|
|
|
|
let gen_args = path_seg.args.map(|args| args.into_inner());
|
|
|
|
return Ok((ident, gen_args));
|
|
|
|
}
|
|
|
|
let err = self.struct_span_err(
|
|
|
|
path.span,
|
|
|
|
"paths with multiple segments cannot be used in associated type constraints",
|
|
|
|
);
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let span = lo.to(self.prev_token.span);
|
|
|
|
let err = self.struct_span_err(
|
|
|
|
span,
|
|
|
|
"only path types can be used in associated type constraints",
|
|
|
|
);
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let span = lo.to(self.prev_token.span);
|
|
|
|
let err = self
|
|
|
|
.struct_span_err(span, "only types can be used in associated type constraints");
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 19:59:27 +02:00
|
|
|
}
|