rust/src/libsyntax/parse/parser.rs

2427 lines
91 KiB
Rust
Raw Normal View History

2019-08-11 11:14:30 +00:00
mod expr;
use expr::LhsExpr;
2019-08-11 13:24:37 +00:00
mod pat;
2019-08-11 16:34:42 +00:00
mod item;
pub use item::AliasKind;
mod module;
pub use module::{ModulePath, ModulePathSuccess};
2019-08-11 17:59:27 +00:00
mod ty;
mod path;
pub use path::PathStyle;
2019-08-11 16:34:42 +00:00
2019-08-11 17:59:27 +00:00
use crate::ast::{self, AttrStyle};
use crate::ast::{Arg, Attribute, BindingMode};
2019-08-11 16:34:42 +00:00
use crate::ast::{Block, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind};
2019-08-11 17:59:27 +00:00
use crate::ast::{FnDecl};
2019-08-11 16:34:42 +00:00
use crate::ast::{Ident, IsAsync, Local, Lifetime};
use crate::ast::{MacStmtStyle, Mac_, MacDelimiter};
2019-08-11 17:59:27 +00:00
use crate::ast::{Mutability};
2019-02-06 17:33:01 +00:00
use crate::ast::StrStyle;
use crate::ast::SelfKind;
2019-08-11 17:59:27 +00:00
use crate::ast::{GenericParam, GenericParamKind, WhereClause};
use crate::ast::{TyKind, GenericBounds};
2019-08-11 16:34:42 +00:00
use crate::ast::{Visibility, VisibilityKind, Unsafety, CrateSugar};
2019-02-06 17:33:01 +00:00
use crate::ext::base::DummyResult;
use crate::ext::hygiene::SyntaxContext;
2019-08-11 16:34:42 +00:00
use crate::source_map::{self, respan};
use crate::parse::{SeqSep, classify, literal, token};
use crate::parse::lexer::UnmatchedBrace;
2019-02-06 17:33:01 +00:00
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2019-06-05 11:17:56 +00:00
use crate::parse::token::{Token, TokenKind, DelimToken};
2019-08-11 16:34:42 +00:00
use crate::parse::{ParseSess, Directory, DirectoryOwnership};
2019-02-06 17:33:01 +00:00
use crate::print::pprust;
use crate::ptr::P;
use crate::parse::PResult;
use crate::ThinVec;
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
2019-05-11 14:41:37 +00:00
use crate::symbol::{kw, sym, Symbol};
use crate::parse::diagnostics::{Error, dummy_arg};
2019-02-06 17:33:01 +00:00
2019-08-11 16:34:42 +00:00
use errors::{Applicability, DiagnosticId, FatalError};
use rustc_target::spec::abi::{self, Abi};
use syntax_pos::{Span, BytePos, DUMMY_SP, FileName};
use log::debug;
use std::borrow::Cow;
2019-08-11 16:34:42 +00:00
use std::{cmp, mem, slice};
use std::path::PathBuf;
2018-07-03 17:38:14 +00:00
2019-02-06 17:33:01 +00:00
bitflags::bitflags! {
struct Restrictions: u8 {
const STMT_EXPR = 1 << 0;
const NO_STRUCT_LITERAL = 1 << 1;
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
crate enum SemiColonMode {
2016-02-10 03:11:27 +00:00
Break,
Ignore,
Comma,
2016-02-10 03:11:27 +00:00
}
#[derive(Clone, Copy, PartialEq, Debug)]
crate enum BlockMode {
Break,
Ignore,
}
2014-06-09 20:12:30 +00:00
/// As maybe_whole_expr, but for things other than expressions
2019-08-11 13:24:37 +00:00
#[macro_export]
macro_rules! maybe_whole {
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
2019-06-04 22:17:07 +00:00
if let token::Interpolated(nt) = &$p.token.kind {
if let token::$constructor(x) = &**nt {
let $x = x.clone();
$p.bump();
return Ok($e);
2013-03-02 21:02:27 +00:00
}
2013-07-02 19:47:32 +00:00
}
};
}
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
2019-08-11 11:14:30 +00:00
#[macro_export]
macro_rules! maybe_recover_from_interpolated_ty_qpath {
($self: expr, $allow_qpath_recovery: expr) => {
if $allow_qpath_recovery && $self.look_ahead(1, |t| t == &token::ModSep) {
2019-06-04 22:17:07 +00:00
if let token::Interpolated(nt) = &$self.token.kind {
if let token::NtTy(ty) = &**nt {
let ty = ty.clone();
$self.bump();
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_span, ty);
}
}
}
}
}
fn maybe_append(mut lhs: Vec<Attribute>, mut rhs: Option<Vec<Attribute>>) -> Vec<Attribute> {
if let Some(ref mut rhs) = rhs {
lhs.append(rhs);
}
lhs
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum PrevTokenKind {
DocComment,
Comma,
Plus,
Interpolated,
Eof,
Ident,
BitOr,
Other,
}
// NOTE: `Ident`s are handled by `common.rs`.
#[derive(Clone)]
2014-03-09 14:54:34 +00:00
pub struct Parser<'a> {
pub sess: &'a ParseSess,
2019-06-05 19:04:52 +00:00
/// The current normalized token.
/// "Normalized" means that some interpolated tokens
/// (`$i: ident` and `$l: lifetime` meta-variables) are replaced
/// with non-interpolated identifier and lifetime tokens they refer to.
/// Perhaps the normalized / non-normalized setup can be simplified somehow.
2019-06-04 22:17:07 +00:00
pub token: Token,
2019-06-05 19:04:52 +00:00
/// Span of the current non-normalized token.
meta_var_span: Option<Span>,
2019-06-05 19:04:52 +00:00
/// Span of the previous non-normalized token.
pub prev_span: Span,
2019-06-05 19:04:52 +00:00
/// Kind of the previous normalized token (in simplified form).
prev_token_kind: PrevTokenKind,
restrictions: Restrictions,
/// Used to determine the path to externally loaded source files.
crate directory: Directory<'a>,
/// `true` to parse sub-modules in other files.
pub recurse_into_file_modules: bool,
/// Name of the root module this parser originated from. If `None`, then the
/// name is not known. This does not change while the parser is descending
/// into modules, and sub-parsers have new values for this name.
pub root_module_name: Option<String>,
crate expected_tokens: Vec<TokenType>,
crate token_cursor: TokenCursor,
desugar_doc_comments: bool,
/// `true` we should configure out of line modules as we parse.
pub cfg_mods: bool,
/// This field is used to keep track of how many left angle brackets we have seen. This is
/// required in order to detect extra leading left angle brackets (`<` characters) and error
/// appropriately.
///
/// See the comments in the `parse_path_segment` function for more details.
crate unmatched_angle_bracket_count: u32,
crate max_angle_bracket_count: u32,
/// List of all unclosed delimiters found by the lexer. If an entry is used for error recovery
/// it gets removed from here. Every entry left at the end gets emitted as an independent
/// error.
crate unclosed_delims: Vec<UnmatchedBrace>,
crate last_unexpected_token_span: Option<Span>,
crate last_type_ascription: Option<(Span, bool /* likely path typo */)>,
2019-05-22 05:17:53 +00:00
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
crate subparser_name: Option<&'static str>,
}
impl<'a> Drop for Parser<'a> {
fn drop(&mut self) {
2019-03-04 20:59:43 +00:00
let diag = self.diagnostic();
emit_unclosed_delims(&mut self.unclosed_delims, diag);
}
}
#[derive(Clone)]
crate struct TokenCursor {
crate frame: TokenCursorFrame,
crate stack: Vec<TokenCursorFrame>,
}
#[derive(Clone)]
crate struct TokenCursorFrame {
crate delim: token::DelimToken,
crate span: DelimSpan,
crate open_delim: bool,
crate tree_cursor: tokenstream::Cursor,
crate close_delim: bool,
crate last_token: LastToken,
}
/// This is used in `TokenCursorFrame` above to track tokens that are consumed
/// by the parser, and then that's transitively used to record the tokens that
/// each parse AST item is created with.
///
/// Right now this has two states, either collecting tokens or not collecting
/// tokens. If we're collecting tokens we just save everything off into a local
/// `Vec`. This should eventually though likely save tokens from the original
/// token stream and just use slicing of token streams to avoid creation of a
/// whole new vector.
///
/// The second state is where we're passively not recording tokens, but the last
/// token is still tracked for when we want to start recording tokens. This
/// "last token" means that when we start recording tokens we'll want to ensure
/// that this, the first token, is included in the output.
///
/// You can find some more example usage of this in the `collect_tokens` method
/// on the parser.
#[derive(Clone)]
crate enum LastToken {
Collecting(Vec<TreeAndJoint>),
Was(Option<TreeAndJoint>),
}
impl TokenCursorFrame {
2019-06-25 21:22:45 +00:00
fn new(span: DelimSpan, delim: DelimToken, tts: &TokenStream) -> Self {
TokenCursorFrame {
2019-06-25 21:22:45 +00:00
delim,
span,
open_delim: delim == token::NoDelim,
tree_cursor: tts.clone().into_trees(),
close_delim: delim == token::NoDelim,
last_token: LastToken::Was(None),
}
}
}
impl TokenCursor {
fn next(&mut self) -> Token {
loop {
let tree = if !self.frame.open_delim {
self.frame.open_delim = true;
TokenTree::open_tt(self.frame.span.open, self.frame.delim)
} else if let Some(tree) = self.frame.tree_cursor.next() {
tree
} else if !self.frame.close_delim {
self.frame.close_delim = true;
TokenTree::close_tt(self.frame.span.close, self.frame.delim)
} else if let Some(frame) = self.stack.pop() {
self.frame = frame;
continue
} else {
return Token::new(token::Eof, DUMMY_SP);
};
match self.frame.last_token {
LastToken::Collecting(ref mut v) => v.push(tree.clone().into()),
LastToken::Was(ref mut t) => *t = Some(tree.clone().into()),
}
match tree {
TokenTree::Token(token) => return token,
TokenTree::Delimited(sp, delim, tts) => {
let frame = TokenCursorFrame::new(sp, delim, &tts);
self.stack.push(mem::replace(&mut self.frame, frame));
}
}
}
}
fn next_desugared(&mut self) -> Token {
2019-06-05 11:17:56 +00:00
let (name, sp) = match self.next() {
Token { kind: token::DocComment(name), span } => (name, span),
tok => return tok,
};
let stripped = strip_doc_comment_decoration(&name.as_str());
// Searches for the occurrences of `"#*` and returns the minimum number of `#`s
// required to wrap the text.
let mut num_of_hashes = 0;
let mut count = 0;
for ch in stripped.chars() {
count = match ch {
'"' => 1,
'#' if count > 0 => count + 1,
_ => 0,
};
num_of_hashes = cmp::max(num_of_hashes, count);
}
let delim_span = DelimSpan::from_single(sp);
let body = TokenTree::Delimited(
delim_span,
token::Bracket,
[
TokenTree::token(token::Ident(sym::doc, false), sp),
TokenTree::token(token::Eq, sp),
2019-06-05 11:17:56 +00:00
TokenTree::token(TokenKind::lit(
token::StrRaw(num_of_hashes), Symbol::intern(&stripped), None
), sp),
]
.iter().cloned().collect::<TokenStream>().into(),
);
self.stack.push(mem::replace(&mut self.frame, TokenCursorFrame::new(
delim_span,
token::NoDelim,
&if doc_comment_style(&name.as_str()) == AttrStyle::Inner {
[TokenTree::token(token::Pound, sp), TokenTree::token(token::Not, sp), body]
2017-02-21 05:05:59 +00:00
.iter().cloned().collect::<TokenStream>().into()
} else {
[TokenTree::token(token::Pound, sp), body]
2017-02-21 05:05:59 +00:00
.iter().cloned().collect::<TokenStream>().into()
},
)));
self.next()
}
}
#[derive(Clone, PartialEq)]
crate enum TokenType {
2019-06-05 11:17:56 +00:00
Token(TokenKind),
2019-05-11 14:41:37 +00:00
Keyword(Symbol),
Operator,
Lifetime,
Ident,
Path,
Type,
Const,
}
impl TokenType {
crate fn to_string(&self) -> String {
match *self {
TokenType::Token(ref t) => format!("`{}`", pprust::token_kind_to_string(t)),
2019-05-11 14:41:37 +00:00
TokenType::Keyword(kw) => format!("`{}`", kw),
TokenType::Operator => "an operator".to_string(),
TokenType::Lifetime => "lifetime".to_string(),
TokenType::Ident => "identifier".to_string(),
TokenType::Path => "path".to_string(),
TokenType::Type => "type".to_string(),
TokenType::Const => "const".to_string(),
}
}
}
#[derive(Copy, Clone, Debug)]
crate enum TokenExpectType {
Expect,
NoExpect,
}
2014-03-09 14:54:34 +00:00
impl<'a> Parser<'a> {
pub fn new(
sess: &'a ParseSess,
tokens: TokenStream,
directory: Option<Directory<'a>>,
recurse_into_file_modules: bool,
desugar_doc_comments: bool,
2019-05-22 05:17:53 +00:00
subparser_name: Option<&'static str>,
) -> Self {
let mut parser = Parser {
sess,
token: Token::dummy(),
prev_span: DUMMY_SP,
2017-03-29 07:17:18 +00:00
meta_var_span: None,
prev_token_kind: PrevTokenKind::Other,
restrictions: Restrictions::empty(),
recurse_into_file_modules,
2017-11-28 02:14:24 +00:00
directory: Directory {
path: Cow::from(PathBuf::new()),
2017-11-28 02:14:24 +00:00
ownership: DirectoryOwnership::Owned { relative: None }
},
root_module_name: None,
expected_tokens: Vec::new(),
token_cursor: TokenCursor {
frame: TokenCursorFrame::new(
DelimSpan::dummy(),
token::NoDelim,
&tokens.into(),
),
stack: Vec::new(),
},
desugar_doc_comments,
cfg_mods: true,
unmatched_angle_bracket_count: 0,
max_angle_bracket_count: 0,
unclosed_delims: Vec::new(),
last_unexpected_token_span: None,
last_type_ascription: None,
2019-05-22 05:17:53 +00:00
subparser_name,
};
2019-06-04 22:17:07 +00:00
parser.token = parser.next_tok();
if let Some(directory) = directory {
parser.directory = directory;
} else if !parser.token.span.is_dummy() {
if let FileName::Real(mut path) =
sess.source_map().span_to_unmapped_path(parser.token.span) {
path.pop();
parser.directory.path = Cow::from(path);
}
}
2017-03-29 07:17:18 +00:00
parser.process_potential_macro_variable();
parser
}
fn next_tok(&mut self) -> Token {
let mut next = if self.desugar_doc_comments {
self.token_cursor.next_desugared()
} else {
self.token_cursor.next()
2017-02-21 12:04:45 +00:00
};
if next.span.is_dummy() {
// Tweak the location for better diagnostics, but keep syntactic context intact.
next.span = self.prev_span.with_ctxt(next.span.ctxt());
}
2017-02-21 12:04:45 +00:00
next
}
2014-06-09 20:12:30 +00:00
2019-02-08 13:53:55 +00:00
/// Converts the current token to a string using `self`'s reader.
2015-01-21 11:44:49 +00:00
pub fn this_token_to_string(&self) -> String {
pprust::token_to_string(&self.token)
}
crate fn token_descr(&self) -> Option<&'static str> {
2019-06-04 22:17:07 +00:00
Some(match &self.token.kind {
_ if self.token.is_special_ident() => "reserved identifier",
_ if self.token.is_used_keyword() => "keyword",
_ if self.token.is_unused_keyword() => "reserved keyword",
token::DocComment(..) => "doc comment",
_ => return None,
})
}
crate fn this_token_descr(&self) -> String {
if let Some(prefix) = self.token_descr() {
format!("{} `{}`", prefix, self.this_token_to_string())
} else {
format!("`{}`", self.this_token_to_string())
}
}
crate fn unexpected<T>(&mut self) -> PResult<'a, T> {
match self.expect_one_of(&[], &[]) {
Err(e) => Err(e),
Ok(_) => unreachable!(),
}
}
2019-02-08 13:53:55 +00:00
/// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
2019-06-05 11:17:56 +00:00
pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, bool /* recovered */> {
if self.expected_tokens.is_empty() {
if self.token == *t {
self.bump();
Ok(false)
} else {
2019-05-22 05:17:53 +00:00
self.unexpected_try_recover(t)
}
} else {
self.expect_one_of(slice::from_ref(t), &[])
}
}
2014-06-09 20:12:30 +00:00
/// Expect next token to be edible or inedible token. If edible,
/// then consume it; if inedible, then return without consuming
/// anything. Signal a fatal error if next token is unexpected.
pub fn expect_one_of(
&mut self,
2019-06-05 11:17:56 +00:00
edible: &[TokenKind],
inedible: &[TokenKind],
) -> PResult<'a, bool /* recovered */> {
if edible.contains(&self.token.kind) {
self.bump();
Ok(false)
} else if inedible.contains(&self.token.kind) {
// leave it in the input
Ok(false)
} else if self.last_unexpected_token_span == Some(self.token.span) {
FatalError.raise();
} else {
self.expected_one_of_not_found(edible, inedible)
}
}
2018-06-11 14:04:11 +00:00
pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
self.parse_ident_common(true)
}
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
2019-06-04 22:17:07 +00:00
match self.token.kind {
token::Ident(name, _) => {
if self.token.is_reserved_ident() {
2018-01-23 03:03:51 +00:00
let mut err = self.expected_ident_found();
if recover {
err.emit();
} else {
return Err(err);
}
}
let span = self.token.span;
self.bump();
Ok(Ident::new(name, span))
}
_ => {
Err(if self.prev_token_kind == PrevTokenKind::DocComment {
2019-06-03 19:06:49 +00:00
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
} else {
self.expected_ident_found()
})
}
}
}
2019-02-08 13:53:55 +00:00
/// Checks if the next token is `tok`, and returns `true` if so.
///
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
/// encountered.
2019-06-05 11:17:56 +00:00
crate fn check(&mut self, tok: &TokenKind) -> bool {
let is_present = self.token == *tok;
if !is_present { self.expected_tokens.push(TokenType::Token(tok.clone())); }
is_present
}
2019-02-08 13:53:55 +00:00
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
2019-06-05 11:17:56 +00:00
pub fn eat(&mut self, tok: &TokenKind) -> bool {
let is_present = self.check(tok);
if is_present { self.bump() }
is_present
}
2019-05-11 14:41:37 +00:00
fn check_keyword(&mut self, kw: Symbol) -> bool {
self.expected_tokens.push(TokenType::Keyword(kw));
self.token.is_keyword(kw)
}
2019-02-08 13:53:55 +00:00
/// If the next token is the given keyword, eats it and returns
/// `true`. Otherwise, returns `false`.
2019-05-11 14:41:37 +00:00
pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
if self.check_keyword(kw) {
self.bump();
true
} else {
false
}
}
2019-05-11 14:41:37 +00:00
fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
if self.token.is_keyword(kw) {
self.bump();
true
2014-08-28 04:34:03 +00:00
} else {
false
2014-08-28 04:34:03 +00:00
}
}
2019-02-08 13:53:55 +00:00
/// If the given word is not a keyword, signals an error.
/// If the next token is not the given word, signals an error.
/// Otherwise, eats it.
2019-05-11 14:41:37 +00:00
fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
if !self.eat_keyword(kw) {
self.unexpected()
} else {
Ok(())
}
}
crate fn check_ident(&mut self) -> bool {
if self.token.is_ident() {
true
} else {
self.expected_tokens.push(TokenType::Ident);
false
}
}
fn check_path(&mut self) -> bool {
if self.token.is_path_start() {
true
} else {
self.expected_tokens.push(TokenType::Path);
false
}
}
fn check_type(&mut self) -> bool {
if self.token.can_begin_type() {
true
} else {
self.expected_tokens.push(TokenType::Type);
false
}
}
fn check_const_arg(&mut self) -> bool {
if self.token.can_begin_const_arg() {
true
} else {
self.expected_tokens.push(TokenType::Const);
false
}
}
2019-02-08 13:53:55 +00:00
/// Expects and consumes a `+`. if `+=` is seen, replaces it with a `=`
/// and continues. If a `+` is not seen, returns `false`.
///
2019-02-08 13:53:55 +00:00
/// This is used when token-splitting `+=` into `+`.
/// See issue #47856 for an example of when this may occur.
fn eat_plus(&mut self) -> bool {
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
2019-06-04 22:17:07 +00:00
match self.token.kind {
token::BinOp(token::Plus) => {
self.bump();
true
}
token::BinOpEq(token::Plus) => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
self.bump_with(token::Eq, span);
true
}
_ => false,
}
}
2018-05-25 21:36:23 +00:00
/// Checks to see if the next token is either `+` or `+=`.
2019-02-08 13:53:55 +00:00
/// Otherwise returns `false`.
fn check_plus(&mut self) -> bool {
if self.token.is_like_plus() {
true
}
else {
2018-06-01 13:52:51 +00:00
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
false
}
}
2019-02-08 13:53:55 +00:00
/// Expects and consumes an `&`. If `&&` is seen, replaces it with a single
/// `&` and continues. If an `&` is not seen, signals an error.
2015-12-20 21:00:43 +00:00
fn expect_and(&mut self) -> PResult<'a, ()> {
self.expected_tokens.push(TokenType::Token(token::BinOp(token::And)));
2019-06-04 22:17:07 +00:00
match self.token.kind {
token::BinOp(token::And) => {
self.bump();
Ok(())
}
2014-10-27 08:22:52 +00:00
token::AndAnd => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
2017-07-31 20:04:34 +00:00
Ok(self.bump_with(token::BinOp(token::And), span))
}
_ => self.unexpected()
}
}
2019-02-08 13:53:55 +00:00
/// Expects and consumes an `|`. If `||` is seen, replaces it with a single
/// `|` and continues. If an `|` is not seen, signals an error.
fn expect_or(&mut self) -> PResult<'a, ()> {
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Or)));
2019-06-04 22:17:07 +00:00
match self.token.kind {
token::BinOp(token::Or) => {
self.bump();
Ok(())
}
token::OrOr => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
Ok(self.bump_with(token::BinOp(token::Or), span))
}
_ => self.unexpected()
}
}
fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>) {
literal::expect_no_suffix(&self.sess.span_diagnostic, sp, kind, suffix)
}
2019-02-08 13:53:55 +00:00
/// Attempts to consume a `<`. If `<<` is seen, replaces it with a single
/// `<` and continue. If `<-` is seen, replaces it with a single `<`
/// and continue. If a `<` is not seen, returns false.
2014-06-09 20:12:30 +00:00
///
/// This is meant to be used when parsing generics on a path to get the
/// starting token.
fn eat_lt(&mut self) -> bool {
self.expected_tokens.push(TokenType::Token(token::Lt));
2019-06-04 22:17:07 +00:00
let ate = match self.token.kind {
token::Lt => {
self.bump();
true
}
2014-10-27 08:22:52 +00:00
token::BinOp(token::Shl) => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
2017-07-31 20:04:34 +00:00
self.bump_with(token::Lt, span);
true
}
2019-02-07 09:10:11 +00:00
token::LArrow => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
2019-02-07 09:10:11 +00:00
self.bump_with(token::BinOp(token::Minus), span);
true
}
_ => false,
};
if ate {
// See doc comment for `unmatched_angle_bracket_count`.
self.unmatched_angle_bracket_count += 1;
self.max_angle_bracket_count += 1;
debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
}
ate
}
2015-12-20 21:00:43 +00:00
fn expect_lt(&mut self) -> PResult<'a, ()> {
if !self.eat_lt() {
self.unexpected()
} else {
Ok(())
}
}
2019-02-08 13:53:55 +00:00
/// Expects and consumes a single `>` token. if a `>>` is seen, replaces it
/// with a single `>` and continues. If a `>` is not seen, signals an error.
fn expect_gt(&mut self) -> PResult<'a, ()> {
self.expected_tokens.push(TokenType::Token(token::Gt));
2019-06-04 22:17:07 +00:00
let ate = match self.token.kind {
token::Gt => {
self.bump();
Some(())
}
2014-10-27 08:22:52 +00:00
token::BinOp(token::Shr) => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
Some(self.bump_with(token::Gt, span))
2013-12-30 23:17:53 +00:00
}
2014-10-27 08:22:52 +00:00
token::BinOpEq(token::Shr) => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
Some(self.bump_with(token::Ge, span))
}
2014-10-27 08:22:52 +00:00
token::Ge => {
let span = self.token.span.with_lo(self.token.span.lo() + BytePos(1));
Some(self.bump_with(token::Eq, span))
}
_ => None,
};
match ate {
Some(_) => {
// See doc comment for `unmatched_angle_bracket_count`.
if self.unmatched_angle_bracket_count > 0 {
self.unmatched_angle_bracket_count -= 1;
debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
}
Ok(())
},
None => self.unexpected(),
}
}
2019-02-08 13:53:55 +00:00
/// Parses a sequence, including the closing delimiter. The function
/// `f` must consume tokens until reaching the next separator or
2014-06-09 20:12:30 +00:00
/// closing bracket.
pub fn parse_seq_to_end<T>(
&mut self,
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, Vec<T>> {
let (val, _, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
if !recovered {
self.bump();
}
Ok(val)
}
2019-02-08 13:53:55 +00:00
/// Parses a sequence, not including the closing delimiter. The function
/// `f` must consume tokens until reaching the next separator or
2014-06-09 20:12:30 +00:00
/// closing bracket.
pub fn parse_seq_to_before_end<T>(
&mut self,
2019-06-05 11:17:56 +00:00
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (Vec<T>, bool, bool)> {
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
}
fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
kets.iter().any(|k| {
match expect {
TokenExpectType::Expect => self.check(k),
TokenExpectType::NoExpect => self.token == **k,
}
})
}
crate fn parse_seq_to_before_tokens<T>(
&mut self,
2019-06-05 11:17:56 +00:00
kets: &[&TokenKind],
sep: SeqSep,
expect: TokenExpectType,
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (Vec<T>, bool /* trailing */, bool /* recovered */)> {
let mut first = true;
let mut recovered = false;
let mut trailing = false;
let mut v = vec![];
while !self.expect_any_with_type(kets, expect) {
if let token::CloseDelim(..) | token::Eof = self.token.kind {
break
}
if let Some(ref t) = sep.sep {
if first {
first = false;
} else {
match self.expect(t) {
Ok(false) => {}
Ok(true) => {
recovered = true;
break;
}
Err(mut e) => {
// Attempt to keep parsing if it was a similar separator
if let Some(ref tokens) = t.similar_tokens() {
if tokens.contains(&self.token.kind) {
self.bump();
}
}
e.emit();
// Attempt to keep parsing if it was an omitted separator
match f(self) {
Ok(t) => {
v.push(t);
continue;
},
Err(mut e) => {
e.cancel();
break;
}
}
}
}
}
}
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
trailing = true;
break;
}
let t = f(self)?;
v.push(t);
}
Ok((v, trailing, recovered))
}
2019-02-08 13:53:55 +00:00
/// Parses a sequence, including the closing delimiter. The function
/// `f` must consume tokens until reaching the next separator or
2014-06-09 20:12:30 +00:00
/// closing bracket.
fn parse_unspanned_seq<T>(
&mut self,
2019-06-05 11:17:56 +00:00
bra: &TokenKind,
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (Vec<T>, bool)> {
self.expect(bra)?;
let (result, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
if !recovered {
self.eat(ket);
}
Ok((result, trailing))
}
fn parse_delim_comma_seq<T>(
&mut self,
delim: DelimToken,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (Vec<T>, bool)> {
self.parse_unspanned_seq(
&token::OpenDelim(delim),
&token::CloseDelim(delim),
SeqSep::trailing_allowed(token::Comma),
f,
)
}
fn parse_paren_comma_seq<T>(
&mut self,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (Vec<T>, bool)> {
self.parse_delim_comma_seq(token::Paren, f)
}
2014-06-09 20:12:30 +00:00
/// Advance the parser by one token
pub fn bump(&mut self) {
if self.prev_token_kind == PrevTokenKind::Eof {
// Bumping after EOF is a bad sign, usually an infinite loop.
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
}
self.prev_span = self.meta_var_span.take().unwrap_or(self.token.span);
// Record last token kind for possible error recovery.
2019-06-04 22:17:07 +00:00
self.prev_token_kind = match self.token.kind {
token::DocComment(..) => PrevTokenKind::DocComment,
token::Comma => PrevTokenKind::Comma,
token::BinOp(token::Plus) => PrevTokenKind::Plus,
token::BinOp(token::Or) => PrevTokenKind::BitOr,
token::Interpolated(..) => PrevTokenKind::Interpolated,
token::Eof => PrevTokenKind::Eof,
token::Ident(..) => PrevTokenKind::Ident,
_ => PrevTokenKind::Other,
};
2019-06-04 22:17:07 +00:00
self.token = self.next_tok();
self.expected_tokens.clear();
// check after each token
2017-03-29 07:17:18 +00:00
self.process_potential_macro_variable();
}
2013-07-02 19:47:32 +00:00
/// Advance the parser using provided token as a next one. Use this when
/// consuming a part of a token. For example a single `<` from `<<`.
2019-06-05 11:17:56 +00:00
fn bump_with(&mut self, next: TokenKind, span: Span) {
self.prev_span = self.token.span.with_hi(span.lo());
// It would be incorrect to record the kind of the current token, but
// fortunately for tokens currently using `bump_with`, the
// prev_token_kind will be of no use anyway.
self.prev_token_kind = PrevTokenKind::Other;
self.token = Token::new(next, span);
self.expected_tokens.clear();
}
pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
F: FnOnce(&Token) -> R,
{
if dist == 0 {
2019-06-04 22:17:07 +00:00
return f(&self.token);
}
2019-06-04 22:17:07 +00:00
let frame = &self.token_cursor.frame;
f(&match frame.tree_cursor.look_ahead(dist - 1) {
Some(tree) => match tree {
2019-06-04 22:17:07 +00:00
TokenTree::Token(token) => token,
2019-06-05 11:17:56 +00:00
TokenTree::Delimited(dspan, delim, _) =>
Token::new(token::OpenDelim(delim), dspan.open),
2019-06-04 22:17:07 +00:00
}
None => Token::new(token::CloseDelim(frame.delim), frame.span.close)
})
}
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
}
2019-02-08 13:53:55 +00:00
/// Parses asyncness: `async` or nothing.
2018-06-19 04:18:10 +00:00
fn parse_asyncness(&mut self) -> IsAsync {
2019-05-11 14:41:37 +00:00
if self.eat_keyword(kw::Async) {
IsAsync::Async {
closure_id: ast::DUMMY_NODE_ID,
return_impl_trait_id: ast::DUMMY_NODE_ID,
}
2018-06-19 04:18:10 +00:00
} else {
IsAsync::NotAsync
}
}
2019-02-08 13:53:55 +00:00
/// Parses unsafety: `unsafe` or nothing.
fn parse_unsafety(&mut self) -> Unsafety {
2019-05-11 14:41:37 +00:00
if self.eat_keyword(kw::Unsafe) {
Unsafety::Unsafe
} else {
Unsafety::Normal
}
}
fn is_named_argument(&self) -> bool {
2019-06-04 22:17:07 +00:00
let offset = match self.token.kind {
token::Interpolated(ref nt) => match **nt {
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
_ => 0,
}
token::BinOp(token::And) | token::AndAnd => 1,
2019-05-11 14:41:37 +00:00
_ if self.token.is_keyword(kw::Mut) => 1,
_ => 0,
2013-07-18 03:04:37 +00:00
};
2018-03-08 11:27:23 +00:00
self.look_ahead(offset, |t| t.is_ident()) &&
self.look_ahead(offset + 1, |t| t == &token::Colon)
}
2019-02-08 13:53:55 +00:00
/// Skips unexpected attributes and doc comments in this position and emits an appropriate
/// error.
/// This version of parse arg doesn't necessarily require identifier names.
fn parse_arg_general<F>(
&mut self,
is_trait_item: bool,
allow_c_variadic: bool,
is_name_required: F,
) -> PResult<'a, Arg>
where
F: Fn(&token::Token) -> bool
{
2019-07-26 22:52:37 +00:00
let lo = self.token.span;
let attrs = self.parse_arg_attributes()?;
if let Some(mut arg) = self.parse_self_arg()? {
arg.attrs = attrs.into();
return self.recover_bad_self_arg(arg, is_trait_item);
}
let is_name_required = is_name_required(&self.token);
let (pat, ty) = if is_name_required || self.is_named_argument() {
debug!("parse_arg_general parse_pat (is_name_required:{})", is_name_required);
2018-08-09 22:23:08 +00:00
let pat = self.parse_pat(Some("argument name"))?;
if let Err(mut err) = self.expect(&token::Colon) {
if let Some(ident) = self.argument_without_type(
&mut err,
pat,
is_name_required,
is_trait_item,
) {
err.emit();
return Ok(dummy_arg(ident));
} else {
return Err(err);
}
}
self.eat_incorrect_doc_comment_for_arg_type();
(pat, self.parse_ty_common(true, true, allow_c_variadic)?)
2018-08-09 22:23:08 +00:00
} else {
debug!("parse_arg_general ident_to_pat");
let parser_snapshot_before_ty = self.clone();
self.eat_incorrect_doc_comment_for_arg_type();
let mut ty = self.parse_ty_common(true, true, allow_c_variadic);
if ty.is_ok() && self.token != token::Comma &&
self.token != token::CloseDelim(token::Paren) {
// This wasn't actually a type, but a pattern looking like a type,
// so we are going to rollback and re-parse for recovery.
ty = self.unexpected();
2018-08-20 00:51:02 +00:00
}
match ty {
Ok(ty) => {
2019-05-11 14:41:37 +00:00
let ident = Ident::new(kw::Invalid, self.prev_span);
2019-08-11 13:24:37 +00:00
let bm = BindingMode::ByValue(Mutability::Immutable);
let pat = self.mk_pat_ident(ty.span, bm, ident);
(pat, ty)
}
Err(mut err) => {
// If this is a C-variadic argument and we hit an error, return the
// error.
if self.token == token::DotDotDot {
return Err(err);
}
// Recover from attempting to parse the argument as a type without pattern.
err.cancel();
mem::replace(self, parser_snapshot_before_ty);
self.recover_arg_parse()?
2018-08-09 22:23:08 +00:00
}
}
2018-08-09 22:23:08 +00:00
};
2019-07-26 22:52:37 +00:00
let span = lo.to(self.token.span);
Ok(Arg { attrs: attrs.into(), id: ast::DUMMY_NODE_ID, pat, span, ty })
}
crate fn check_lifetime(&mut self) -> bool {
2017-03-16 21:47:32 +00:00
self.expected_tokens.push(TokenType::Lifetime);
self.token.is_lifetime()
}
2019-02-08 13:53:55 +00:00
/// Parses a single lifetime `'a` or panics.
crate fn expect_lifetime(&mut self) -> Lifetime {
if let Some(ident) = self.token.lifetime() {
let span = self.token.span;
2017-12-31 02:47:45 +00:00
self.bump();
Lifetime { ident: Ident::new(ident.name, span), id: ast::DUMMY_NODE_ID }
2017-12-31 02:47:45 +00:00
} else {
self.span_bug(self.token.span, "not a lifetime")
}
}
2019-02-08 13:53:55 +00:00
/// Parses mutability (`mut` or nothing).
2017-03-16 21:47:32 +00:00
fn parse_mutability(&mut self) -> Mutability {
2019-05-11 14:41:37 +00:00
if self.eat_keyword(kw::Mut) {
2017-03-16 21:47:32 +00:00
Mutability::Mutable
} else {
2017-03-16 21:47:32 +00:00
Mutability::Immutable
}
}
fn parse_field_name(&mut self) -> PResult<'a, Ident> {
2019-06-05 11:17:56 +00:00
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) =
self.token.kind {
self.expect_no_suffix(self.token.span, "a tuple index", suffix);
self.bump();
Ok(Ident::new(symbol, self.prev_span))
} else {
self.parse_ident_common(false)
}
}
fn expect_delimited_token_tree(&mut self) -> PResult<'a, (MacDelimiter, TokenStream)> {
2019-06-04 22:17:07 +00:00
let delim = match self.token.kind {
token::OpenDelim(delim) => delim,
_ => {
let msg = "expected open delimiter";
let mut err = self.fatal(msg);
err.span_label(self.token.span, msg);
return Err(err)
}
};
let tts = match self.parse_token_tree() {
TokenTree::Delimited(_, _, tts) => tts,
_ => unreachable!(),
};
let delim = match delim {
token::Paren => MacDelimiter::Parenthesis,
token::Bracket => MacDelimiter::Bracket,
token::Brace => MacDelimiter::Brace,
token::NoDelim => self.bug("unexpected no delimiter"),
};
Ok((delim, tts.into()))
}
2019-08-11 11:14:30 +00:00
fn parse_or_use_outer_attributes(&mut self,
already_parsed_attrs: Option<ThinVec<Attribute>>)
-> PResult<'a, ThinVec<Attribute>> {
if let Some(attrs) = already_parsed_attrs {
Ok(attrs)
} else {
self.parse_outer_attributes().map(|a| a.into())
}
2019-08-11 11:14:30 +00:00
}
2019-08-11 11:14:30 +00:00
crate fn process_potential_macro_variable(&mut self) {
self.token = match self.token.kind {
token::Dollar if self.token.span.ctxt() != SyntaxContext::empty() &&
self.look_ahead(1, |t| t.is_ident()) => {
self.bump();
2019-08-11 11:14:30 +00:00
let name = match self.token.kind {
token::Ident(name, _) => name,
_ => unreachable!()
};
2019-08-11 11:14:30 +00:00
let span = self.prev_span.to(self.token.span);
self.diagnostic()
.struct_span_fatal(span, &format!("unknown macro variable `{}`", name))
.span_label(span, "unknown macro variable")
.emit();
self.bump();
2019-08-11 11:14:30 +00:00
return
}
2019-08-11 11:14:30 +00:00
token::Interpolated(ref nt) => {
self.meta_var_span = Some(self.token.span);
// Interpolated identifier and lifetime tokens are replaced with usual identifier
// and lifetime tokens, so the former are never encountered during normal parsing.
match **nt {
token::NtIdent(ident, is_raw) =>
Token::new(token::Ident(ident.name, is_raw), ident.span),
token::NtLifetime(ident) =>
Token::new(token::Lifetime(ident.name), ident.span),
_ => return,
}
2019-08-11 11:14:30 +00:00
}
_ => return,
};
}
2019-08-11 11:14:30 +00:00
/// Parses a single token tree from the input.
crate fn parse_token_tree(&mut self) -> TokenTree {
match self.token.kind {
token::OpenDelim(..) => {
let frame = mem::replace(&mut self.token_cursor.frame,
self.token_cursor.stack.pop().unwrap());
self.token.span = frame.span.entire();
self.bump();
TokenTree::Delimited(
frame.span,
frame.delim,
frame.tree_cursor.stream.into(),
)
},
token::CloseDelim(_) | token::Eof => unreachable!(),
_ => {
let token = self.token.take();
self.bump();
TokenTree::Token(token)
2014-07-06 21:29:29 +00:00
}
}
}
2019-08-11 11:14:30 +00:00
/// Parses a stream of tokens into a list of `TokenTree`s, up to EOF.
pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
let mut tts = Vec::new();
while self.token != token::Eof {
tts.push(self.parse_token_tree());
}
Ok(tts)
}
2019-08-11 11:14:30 +00:00
pub fn parse_tokens(&mut self) -> TokenStream {
let mut result = Vec::new();
loop {
match self.token.kind {
token::Eof | token::CloseDelim(..) => break,
_ => result.push(self.parse_token_tree().into()),
}
}
2019-08-11 11:14:30 +00:00
TokenStream::new(result)
}
2019-08-11 11:14:30 +00:00
/// Evaluates the closure with restrictions in place.
///
/// Afters the closure is evaluated, restrictions are reset.
fn with_res<F, T>(&mut self, r: Restrictions, f: F) -> T
where F: FnOnce(&mut Self) -> T
{
let old = self.restrictions;
self.restrictions = r;
let r = f(self);
self.restrictions = old;
return r;
2016-09-22 04:45:29 +00:00
2019-08-11 11:14:30 +00:00
}
2019-02-08 13:53:55 +00:00
/// Parses the RHS of a local variable declaration (e.g., '= 14;').
fn parse_initializer(&mut self, skip_eq: bool) -> PResult<'a, Option<P<Expr>>> {
2018-09-02 06:13:29 +00:00
if self.eat(&token::Eq) {
Ok(Some(self.parse_expr()?))
} else if skip_eq {
Ok(Some(self.parse_expr()?))
} else {
Ok(None)
}
2010-10-12 01:20:25 +00:00
}
2019-02-08 13:53:55 +00:00
/// Parses a local variable declaration.
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
let lo = self.prev_span;
let pat = self.parse_top_level_pat()?;
let (err, ty) = if self.eat(&token::Colon) {
// Save the state of the parser before parsing type normally, in case there is a `:`
// instead of an `=` typo.
let parser_snapshot_before_type = self.clone();
let colon_sp = self.prev_span;
match self.parse_ty() {
Ok(ty) => (None, Some(ty)),
Err(mut err) => {
// Rewind to before attempting to parse the type and continue parsing
let parser_snapshot_after_type = self.clone();
mem::replace(self, parser_snapshot_before_type);
2019-07-24 09:01:30 +00:00
let snippet = self.span_to_snippet(pat.span).unwrap();
err.span_label(pat.span, format!("while parsing the type for `{}`", snippet));
(Some((parser_snapshot_after_type, colon_sp, err)), None)
}
}
} else {
(None, None)
};
let init = match (self.parse_initializer(err.is_some()), err) {
(Ok(init), None) => { // init parsed, ty parsed
init
}
(Ok(init), Some((_, colon_sp, mut err))) => { // init parsed, ty error
// Could parse the type as if it were the initializer, it is likely there was a
// typo in the code: `:` instead of `=`. Add suggestion and emit the error.
err.span_suggestion_short(
suggestion applicabilities for libsyntax and librustc, run-rustfix tests Consider this a down payment on #50723. To recap, an `Applicability` enum was recently (#50204) added, to convey to Rustfix and other tools whether we think it's OK for them to blindly apply the suggestion, or whether to prompt a human for guidance (because the suggestion might contain placeholders that we can't infer, or because we think it has a sufficiently high probability of being wrong even though it's— presumably—right often enough to be worth emitting in the first place). When a suggestion is marked as `MaybeIncorrect`, we try to use comments to indicate precisely why (although there are a few places where we just say `// speculative` because the present author's subjective judgement balked at the idea that the suggestion has no false positives). The `run-rustfix` directive is opporunistically set on some relevant UI tests (and a couple tests that were in the `test/ui/suggestions` directory, even if the suggestions didn't originate in librustc or libsyntax). This is less trivial than it sounds, because a surprising number of test files aren't equipped to be tested as fixed even when they contain successfully fixable errors, because, e.g., there are more, not-directly-related errors after fixing. Some test files need an attribute or underscore to avoid unused warnings tripping up the "fixed code is still producing diagnostics" check despite the fixes being correct; this is an interesting contrast-to/inconsistency-with the behavior of UI tests (which secretly pass `-A unused`), a behavior which we probably ought to resolve one way or the other (filed issue #50926). A few suggestion labels are reworded (e.g., to avoid phrasing it as a question, which which is discouraged by the style guidelines listed in `.span_suggestion`'s doc-comment).
2018-05-19 21:52:24 +00:00
colon_sp,
"use `=` if you meant to assign",
"=".to_string(),
Applicability::MachineApplicable
);
err.emit();
2018-02-16 14:56:50 +00:00
// As this was parsed successfully, continue as if the code has been fixed for the
// rest of the file. It will still fail due to the emitted error, but we avoid
// extra noise.
init
}
(Err(mut init_err), Some((snapshot, _, ty_err))) => { // init error, ty error
init_err.cancel();
// Couldn't parse the type nor the initializer, only raise the type error and
// return to the parser state before parsing the type as the initializer.
// let x: <parse_error>;
mem::replace(self, snapshot);
return Err(ty_err);
}
(Err(err), None) => { // init error, ty parsed
// Couldn't parse the initializer and we're not attempting to recover a failed
// parse of the type, return the error.
return Err(err);
}
};
2017-10-06 10:16:16 +00:00
let hi = if self.token == token::Semi {
self.token.span
2017-10-06 10:16:16 +00:00
} else {
self.prev_span
};
Ok(P(ast::Local {
ty,
pat,
init,
id: ast::DUMMY_NODE_ID,
2017-10-06 10:16:16 +00:00
span: lo.to(hi),
attrs,
}))
}
/// Parse a statement. This stops just before trailing semicolons on everything but items.
/// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
2016-07-16 20:41:43 +00:00
Ok(self.parse_stmt_(true))
2016-02-10 03:11:27 +00:00
}
2016-09-06 05:57:58 +00:00
fn parse_stmt_(&mut self, macro_legacy_warnings: bool) -> Option<Stmt> {
self.parse_stmt_without_recovery(macro_legacy_warnings).unwrap_or_else(|mut e| {
2016-02-10 03:11:27 +00:00
e.emit();
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
2016-02-10 03:11:27 +00:00
None
})
}
fn is_async_fn(&self) -> bool {
2019-05-11 14:41:37 +00:00
self.token.is_keyword(kw::Async) &&
self.is_keyword_ahead(1, &[kw::Fn])
}
2017-11-04 20:56:45 +00:00
fn is_crate_vis(&self) -> bool {
2019-05-11 14:41:37 +00:00
self.token.is_keyword(kw::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
2017-11-04 20:56:45 +00:00
}
fn is_auto_trait_item(&self) -> bool {
// auto trait
(self.token.is_keyword(kw::Auto) &&
self.is_keyword_ahead(1, &[kw::Trait]))
|| // unsafe auto trait
2019-05-11 14:41:37 +00:00
(self.token.is_keyword(kw::Unsafe) &&
self.is_keyword_ahead(1, &[kw::Auto]) &&
self.is_keyword_ahead(2, &[kw::Trait]))
}
fn parse_stmt_without_recovery(
&mut self,
macro_legacy_warnings: bool,
) -> PResult<'a, Option<Stmt>> {
maybe_whole!(self, NtStmt, |x| Some(x));
2012-08-01 21:34:35 +00:00
let attrs = self.parse_outer_attributes()?;
let lo = self.token.span;
2019-05-11 14:41:37 +00:00
Ok(Some(if self.eat_keyword(kw::Let) {
2016-06-17 02:30:01 +00:00
Stmt {
id: ast::DUMMY_NODE_ID,
node: StmtKind::Local(self.parse_local(attrs.into())?),
span: lo.to(self.prev_span),
2016-06-17 02:30:01 +00:00
}
2018-01-29 05:12:09 +00:00
} else if let Some(macro_def) = self.eat_macro_def(
&attrs,
2018-08-18 10:14:03 +00:00
&source_map::respan(lo, VisibilityKind::Inherited),
2018-01-29 05:12:09 +00:00
lo,
)? {
Stmt {
id: ast::DUMMY_NODE_ID,
node: StmtKind::Item(macro_def),
span: lo.to(self.prev_span),
}
// Starts like a simple path, being careful to avoid contextual keywords
// such as a union items, item with `crate` visibility or auto trait items.
2017-11-04 20:56:45 +00:00
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
// like a path (1 token), but it fact not a path.
// `union::b::c` - path, `union U { ... }` - not a path.
// `crate::b::c` - path, `crate struct S;` - not a path.
2016-10-19 20:33:41 +00:00
} else if self.token.is_path_start() &&
!self.token.is_qpath_start() &&
2017-11-04 20:56:45 +00:00
!self.is_union_item() &&
2018-01-01 14:42:32 +00:00
!self.is_crate_vis() &&
!self.is_auto_trait_item() &&
!self.is_async_fn() {
2019-07-01 09:20:44 +00:00
let path = self.parse_path(PathStyle::Expr)?;
if !self.eat(&token::Not) {
let expr = if self.check(&token::OpenDelim(token::Brace)) {
2019-07-01 09:20:44 +00:00
self.parse_struct_expr(lo, path, ThinVec::new())?
} else {
let hi = self.prev_span;
2019-07-01 09:20:44 +00:00
self.mk_expr(lo.to(hi), ExprKind::Path(None, path), ThinVec::new())
};
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
})?;
return Ok(Some(Stmt {
id: ast::DUMMY_NODE_ID,
node: StmtKind::Expr(expr),
span: lo.to(self.prev_span),
}));
}
let (delim, tts) = self.expect_delimited_token_tree()?;
let hi = self.prev_span;
let style = if delim == MacDelimiter::Brace {
MacStmtStyle::Braces
} else {
MacStmtStyle::NoBraces
};
let mac = respan(lo.to(hi), Mac_ {
path,
tts,
delim,
prior_type_ascription: self.last_type_ascription,
});
let node = if delim == MacDelimiter::Brace ||
self.token == token::Semi || self.token == token::Eof {
StmtKind::Mac(P((mac, style, attrs.into())))
}
// We used to incorrectly stop parsing macro-expanded statements here.
// If the next token will be an error anyway but could have parsed with the
// earlier behavior, stop parsing here and emit a warning to avoid breakage.
else if macro_legacy_warnings &&
self.token.can_begin_expr() &&
match self.token.kind {
// These can continue an expression, so we can't stop parsing and warn.
token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
token::BinOp(token::Minus) | token::BinOp(token::Star) |
token::BinOp(token::And) | token::BinOp(token::Or) |
token::AndAnd | token::OrOr |
token::DotDot | token::DotDotDot | token::DotDotEq => false,
_ => true,
} {
self.warn_missing_semicolon();
StmtKind::Mac(P((mac, style, attrs.into())))
} else {
let e = self.mk_expr(mac.span, ExprKind::Mac(mac), ThinVec::new());
let e = self.maybe_recover_from_bad_qpath(e, true)?;
let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?;
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
StmtKind::Expr(e)
};
Stmt {
id: ast::DUMMY_NODE_ID,
span: lo.to(hi),
node,
}
} else {
// FIXME: Bad copy of attrs
let old_directory_ownership =
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
let item = self.parse_item_(attrs.clone(), false, true)?;
self.directory.ownership = old_directory_ownership;
match item {
2016-06-17 02:30:01 +00:00
Some(i) => Stmt {
id: ast::DUMMY_NODE_ID,
span: lo.to(i.span),
2016-06-17 02:30:01 +00:00
node: StmtKind::Item(i),
},
None => {
let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
if !attrs.is_empty() {
if s.prev_token_kind == PrevTokenKind::DocComment {
2017-02-12 14:18:41 +00:00
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
s.span_err(
s.token.span, "expected statement after outer attribute"
);
}
}
};
// Do not attempt to parse an expression if we're done here.
if self.token == token::Semi {
unused_attrs(&attrs, self);
self.bump();
return Ok(None);
}
if self.token == token::CloseDelim(token::Brace) {
unused_attrs(&attrs, self);
return Ok(None);
}
2014-09-13 16:06:01 +00:00
// Remainder are line-expr stmts.
let e = self.parse_expr_res(
Restrictions::STMT_EXPR, Some(attrs.into()))?;
2016-06-17 02:30:01 +00:00
Stmt {
id: ast::DUMMY_NODE_ID,
span: lo.to(e.span),
2016-06-17 02:30:01 +00:00
node: StmtKind::Expr(e),
}
2014-09-13 16:06:01 +00:00
}
}
}))
}
2019-02-08 13:53:55 +00:00
/// Parses a block. No inner attributes are allowed.
pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
maybe_whole!(self, NtBlock, |x| x);
let lo = self.token.span;
if !self.eat(&token::OpenDelim(token::Brace)) {
let sp = self.token.span;
let tok = self.this_token_descr();
let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok));
2018-09-19 23:23:21 +00:00
let do_not_suggest_help =
2019-05-11 14:41:37 +00:00
self.token.is_keyword(kw::In) || self.token == token::Colon;
if self.token.is_ident_named(sym::and) {
e.span_suggestion_short(
self.token.span,
"use `&&` instead of `and` for the boolean operator",
"&&".to_string(),
Applicability::MaybeIncorrect,
);
}
if self.token.is_ident_named(sym::or) {
e.span_suggestion_short(
self.token.span,
"use `||` instead of `or` for the boolean operator",
"||".to_string(),
Applicability::MaybeIncorrect,
);
}
// Check to see if the user has written something like
//
// if (cond)
// bar;
//
// Which is valid in other languages, but not Rust.
match self.parse_stmt_without_recovery(false) {
Ok(Some(stmt)) => {
2018-08-14 19:05:27 +00:00
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
|| do_not_suggest_help {
// if the next token is an open brace (e.g., `if a b {`), the place-
// inside-a-block suggestion would be more likely wrong than right
2018-09-19 23:23:21 +00:00
e.span_label(sp, "expected `{`");
return Err(e);
}
let mut stmt_span = stmt.span;
// expand the span to include the semicolon, if it exists
if self.eat(&token::Semi) {
2017-07-31 20:04:34 +00:00
stmt_span = stmt_span.with_hi(self.prev_span.hi());
}
2019-07-24 09:01:30 +00:00
if let Ok(snippet) = self.span_to_snippet(stmt_span) {
e.span_suggestion(
stmt_span,
"try placing this code inside a block",
format!("{{ {} }}", snippet),
// speculative, has been misleading in the past (#46836)
Applicability::MaybeIncorrect,
);
}
}
Err(mut e) => {
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
2016-09-15 19:34:21 +00:00
self.cancel(&mut e);
}
_ => ()
}
e.span_label(sp, "expected `{`");
return Err(e);
}
self.parse_block_tail(lo, BlockCheckMode::Default)
}
2019-02-08 13:53:55 +00:00
/// Parses a block. Inner attributes are allowed.
crate fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
2012-08-01 21:34:35 +00:00
let lo = self.token.span;
self.expect(&token::OpenDelim(token::Brace))?;
Ok((self.parse_inner_attributes()?,
self.parse_block_tail(lo, BlockCheckMode::Default)?))
}
2013-04-02 23:44:01 +00:00
2019-02-08 13:53:55 +00:00
/// Parses the rest of a block expression or function body.
/// Precondition: already parsed the '{'.
fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P<Block>> {
let mut stmts = vec![];
while !self.eat(&token::CloseDelim(token::Brace)) {
2019-07-12 03:02:54 +00:00
if self.token == token::Eof {
break;
}
let stmt = match self.parse_full_stmt(false) {
Err(mut err) => {
err.emit();
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
2018-12-19 23:33:56 +00:00
Some(Stmt {
id: ast::DUMMY_NODE_ID,
node: StmtKind::Expr(DummyResult::raw_expr(self.token.span, true)),
span: self.token.span,
2018-12-19 23:33:56 +00:00
})
}
Ok(stmt) => stmt,
};
if let Some(stmt) = stmt {
stmts.push(stmt);
} else {
// Found only `;` or `}`.
continue;
};
}
Ok(P(ast::Block {
stmts,
id: ast::DUMMY_NODE_ID,
2013-01-15 03:35:08 +00:00
rules: s,
span: lo.to(self.prev_span),
}))
}
2019-02-08 13:53:55 +00:00
/// Parses a statement, including the trailing semicolon.
crate fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option<Stmt>> {
// skip looking for a trailing semicolon when we have an interpolated statement
maybe_whole!(self, NtStmt, |x| Some(x));
let mut stmt = match self.parse_stmt_without_recovery(macro_legacy_warnings)? {
Some(stmt) => stmt,
None => return Ok(None),
};
match stmt.node {
StmtKind::Expr(ref expr) if self.token != token::Eof => {
// expression without semicolon
if classify::expr_requires_semi_to_be_stmt(expr) {
// Just check for errors and recover; do not eat semicolon yet.
if let Err(mut e) =
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
{
e.emit();
self.recover_stmt();
// Don't complain about type errors in body tail after parse error (#57383).
let sp = expr.span.to(self.prev_span);
stmt.node = StmtKind::Expr(DummyResult::raw_expr(sp, true));
}
}
2016-02-10 03:11:27 +00:00
}
StmtKind::Local(..) => {
2016-07-12 03:56:19 +00:00
// We used to incorrectly allow a macro-expanded let statement to lack a semicolon.
2016-09-06 05:57:58 +00:00
if macro_legacy_warnings && self.token != token::Semi {
2016-07-12 03:56:19 +00:00
self.warn_missing_semicolon();
} else {
self.expect_one_of(&[], &[token::Semi])?;
2016-07-12 03:56:19 +00:00
}
}
_ => {}
}
if self.eat(&token::Semi) {
stmt = stmt.add_trailing_semicolon();
}
stmt.span = stmt.span.to(self.prev_span);
2016-07-16 20:41:43 +00:00
Ok(Some(stmt))
}
2016-07-12 03:56:19 +00:00
fn warn_missing_semicolon(&self) {
self.diagnostic().struct_span_warn(self.token.span, {
&format!("expected `;`, found {}", self.this_token_descr())
2016-07-12 03:56:19 +00:00
}).note({
"This was erroneously allowed and will become a hard error in a future release"
}).emit();
}
2019-02-08 13:53:55 +00:00
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
///
/// ```
/// BOUND = LT_BOUND (e.g., `'a`)
/// ```
fn parse_lt_param_bounds(&mut self) -> GenericBounds {
let mut lifetimes = Vec::new();
2017-03-16 21:47:32 +00:00
while self.check_lifetime() {
lifetimes.push(ast::GenericBound::Outlives(self.expect_lifetime()));
if !self.eat_plus() {
break
}
}
lifetimes
}
2019-02-08 13:53:55 +00:00
/// Matches `typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?`.
2018-05-26 18:16:21 +00:00
fn parse_ty_param(&mut self,
preceding_attrs: Vec<Attribute>)
2018-05-27 19:07:09 +00:00
-> PResult<'a, GenericParam> {
let ident = self.parse_ident()?;
// Parse optional colon and param bounds.
let bounds = if self.eat(&token::Colon) {
self.parse_generic_bounds(Some(self.prev_span))?
} else {
Vec::new()
};
let default = if self.eat(&token::Eq) {
Some(self.parse_ty()?)
} else {
None
};
2018-05-27 19:07:09 +00:00
Ok(GenericParam {
ident,
id: ast::DUMMY_NODE_ID,
2018-05-28 12:33:28 +00:00
attrs: preceding_attrs.into(),
bounds,
2018-05-27 19:07:09 +00:00
kind: GenericParamKind::Type {
2018-05-26 18:16:21 +00:00
default,
}
})
}
fn parse_const_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, GenericParam> {
2019-05-11 14:41:37 +00:00
self.expect_keyword(kw::Const)?;
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
Ok(GenericParam {
ident,
id: ast::DUMMY_NODE_ID,
attrs: preceding_attrs.into(),
bounds: Vec::new(),
kind: GenericParamKind::Const {
ty,
}
})
}
2019-02-08 13:53:55 +00:00
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
/// a trailing comma and erroneous trailing attributes.
2018-05-27 19:07:09 +00:00
crate fn parse_generic_params(&mut self) -> PResult<'a, Vec<ast::GenericParam>> {
let mut params = Vec::new();
loop {
let attrs = self.parse_outer_attributes()?;
2017-03-16 21:47:32 +00:00
if self.check_lifetime() {
let lifetime = self.expect_lifetime();
// Parse lifetime parameter.
let bounds = if self.eat(&token::Colon) {
self.parse_lt_param_bounds()
} else {
Vec::new()
};
params.push(ast::GenericParam {
2018-05-26 18:16:21 +00:00
ident: lifetime.ident,
id: lifetime.id,
attrs: attrs.into(),
2018-05-28 12:33:28 +00:00
bounds,
kind: ast::GenericParamKind::Lifetime,
2018-05-26 18:16:21 +00:00
});
2019-05-11 14:41:37 +00:00
} else if self.check_keyword(kw::Const) {
// Parse const parameter.
params.push(self.parse_const_param(attrs)?);
2017-03-16 21:47:32 +00:00
} else if self.check_ident() {
// Parse type parameter.
2018-05-26 18:16:21 +00:00
params.push(self.parse_ty_param(attrs)?);
} else {
// Check for trailing attributes and stop parsing.
if !attrs.is_empty() {
if !params.is_empty() {
self.struct_span_err(
attrs[0].span,
&format!("trailing attribute after generic parameter"),
)
.span_label(attrs[0].span, "attributes must go before parameters")
.emit();
} else {
self.struct_span_err(
attrs[0].span,
&format!("attribute without generic parameters"),
)
.span_label(
attrs[0].span,
"attributes are only permitted when preceding parameters",
)
.emit();
}
}
break
2017-03-16 21:47:32 +00:00
}
if !self.eat(&token::Comma) {
break
}
}
Ok(params)
}
2019-02-08 13:53:55 +00:00
/// Parses a set of optional generic type parameter declarations. Where
/// clauses are not parsed here, and must be added later via
/// `parse_where_clause()`.
///
2014-06-09 20:12:30 +00:00
/// matches generics = ( ) | ( < > ) | ( < typaramseq ( , )? > ) | ( < lifetimes ( , )? > )
/// | ( < lifetimes , typaramseq ( , )? > )
/// where typaramseq = ( typaram ) | ( typaram , typaramseq )
fn parse_generics(&mut self) -> PResult<'a, ast::Generics> {
let span_lo = self.token.span;
2019-06-01 05:19:30 +00:00
let (params, span) = if self.eat_lt() {
let params = self.parse_generic_params()?;
self.expect_gt()?;
2019-06-01 05:19:30 +00:00
(params, span_lo.to(self.prev_span))
} else {
(vec![], self.prev_span.between(self.token.span))
2019-06-01 05:19:30 +00:00
};
Ok(ast::Generics {
params,
where_clause: WhereClause {
predicates: Vec::new(),
span: DUMMY_SP,
},
span,
})
}
2019-02-08 13:53:55 +00:00
/// Parses an optional where-clause and places it in `generics`.
///
/// ```ignore (only-for-syntax-highlight)
/// where T : Trait<U, V> + 'b, 'a : 'b
/// ```
fn parse_where_clause(&mut self) -> PResult<'a, WhereClause> {
let mut where_clause = WhereClause {
predicates: Vec::new(),
2019-06-03 19:06:49 +00:00
span: self.prev_span.to(self.prev_span),
};
2019-05-11 14:41:37 +00:00
if !self.eat_keyword(kw::Where) {
return Ok(where_clause);
}
2017-07-27 04:37:35 +00:00
let lo = self.prev_span;
// We are considering adding generics to the `where` keyword as an alternative higher-rank
// parameter syntax (as in `where<'a>` or `where<T>`. To avoid that being a breaking
// change we parse those generics now, but report an error.
if self.choose_generics_over_qpath() {
let generics = self.parse_generics()?;
self.struct_span_err(
generics.span,
"generic parameters on `where` clauses are reserved for future use",
)
.span_label(generics.span, "currently unsupported")
.emit();
}
loop {
let lo = self.token.span;
if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
2017-03-16 21:47:32 +00:00
let lifetime = self.expect_lifetime();
// Bounds starting with a colon are mandatory, but possibly empty.
self.expect(&token::Colon)?;
let bounds = self.parse_lt_param_bounds();
where_clause.predicates.push(ast::WherePredicate::RegionPredicate(
ast::WhereRegionPredicate {
span: lo.to(self.prev_span),
lifetime,
bounds,
}
));
2017-03-16 21:47:32 +00:00
} else if self.check_type() {
// Parse optional `for<'a, 'b>`.
// This `for` is parsed greedily and applies to the whole predicate,
// the bounded type can have its own `for` applying only to it.
// Examples:
// * `for<'a> Trait1<'a>: Trait2<'a /* ok */>`
// * `(for<'a> Trait1<'a>): Trait2<'a /* not ok */>`
// * `for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /* ok */, 'b /* not ok */>`
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
// Parse type with mandatory colon and (possibly empty) bounds,
// or with mandatory equality sign and the second type.
let ty = self.parse_ty()?;
if self.eat(&token::Colon) {
let bounds = self.parse_generic_bounds(Some(self.prev_span))?;
where_clause.predicates.push(ast::WherePredicate::BoundPredicate(
ast::WhereBoundPredicate {
span: lo.to(self.prev_span),
bound_generic_params: lifetime_defs,
bounded_ty: ty,
bounds,
}
));
// FIXME: Decide what should be used here, `=` or `==`.
2018-03-06 10:33:26 +00:00
// FIXME: We are just dropping the binders in lifetime_defs on the floor here.
} else if self.eat(&token::Eq) || self.eat(&token::EqEq) {
let rhs_ty = self.parse_ty()?;
where_clause.predicates.push(ast::WherePredicate::EqPredicate(
ast::WhereEqPredicate {
span: lo.to(self.prev_span),
lhs_ty: ty,
rhs_ty,
id: ast::DUMMY_NODE_ID,
}
));
} else {
return self.unexpected();
}
} else {
break
2017-03-16 21:47:32 +00:00
}
if !self.eat(&token::Comma) {
break
}
}
2017-07-27 04:37:35 +00:00
where_clause.span = lo.to(self.prev_span);
Ok(where_clause)
}
fn parse_fn_args(&mut self, named_args: bool, allow_c_variadic: bool)
2015-12-20 21:00:43 +00:00
-> PResult<'a, (Vec<Arg> , bool)> {
let sp = self.token.span;
let mut c_variadic = false;
let (args, _): (Vec<Option<Arg>>, _) = self.parse_paren_comma_seq(|p| {
let do_not_enforce_named_arguments_for_c_variadic =
|token: &token::Token| -> bool {
if token == &token::DotDotDot {
false
} else {
named_args
}
};
match p.parse_arg_general(
false,
allow_c_variadic,
do_not_enforce_named_arguments_for_c_variadic
) {
Ok(arg) => {
if let TyKind::CVarArgs = arg.ty.node {
c_variadic = true;
if p.token != token::CloseDelim(token::Paren) {
let span = p.token.span;
p.span_err(span,
"`...` must be the last argument of a C-variadic function");
Ok(None)
} else {
Ok(Some(arg))
}
} else {
Ok(Some(arg))
}
},
Err(mut e) => {
e.emit();
let lo = p.prev_span;
// Skip every token until next possible arg or end.
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
// Create a placeholder argument for proper arg count (issue #34264).
let span = lo.to(p.prev_span);
Ok(Some(dummy_arg(Ident::new(kw::Invalid, span))))
}
}
})?;
2018-11-16 12:54:49 +00:00
2016-02-10 03:11:27 +00:00
let args: Vec<_> = args.into_iter().filter_map(|x| x).collect();
if c_variadic && args.is_empty() {
self.span_err(sp,
"C-variadic function must be declared with at least one named argument");
}
Ok((args, c_variadic))
}
2016-03-06 12:54:44 +00:00
/// Returns the parsed optional self argument and whether a self shortcut was used.
///
/// See `parse_self_arg_with_attrs` to collect attributes.
fn parse_self_arg(&mut self) -> PResult<'a, Option<Arg>> {
2019-06-04 22:17:07 +00:00
let expect_ident = |this: &mut Self| match this.token.kind {
2016-03-06 12:54:44 +00:00
// Preserve hygienic context.
token::Ident(name, _) =>
{ let span = this.token.span; this.bump(); Ident::new(name, span) }
_ => unreachable!()
};
2016-10-19 20:33:41 +00:00
let isolated_self = |this: &mut Self, n| {
2019-05-11 14:41:37 +00:00
this.look_ahead(n, |t| t.is_keyword(kw::SelfLower)) &&
2016-10-19 20:33:41 +00:00
this.look_ahead(n + 1, |t| t != &token::ModSep)
};
// Parse optional `self` parameter of a method.
// Only a limited set of initial token sequences is considered `self` parameters; anything
// else is parsed as a normal function parameter list, so some lookahead is required.
let eself_lo = self.token.span;
2019-06-04 22:17:07 +00:00
let (eself, eself_ident, eself_hi) = match self.token.kind {
2014-10-27 08:22:52 +00:00
token::BinOp(token::And) => {
// `&self`
// `&mut self`
// `&'lt self`
// `&'lt mut self`
// `&not_self`
(if isolated_self(self, 1) {
self.bump();
SelfKind::Region(None, Mutability::Immutable)
} else if self.is_keyword_ahead(1, &[kw::Mut]) &&
2016-10-19 20:33:41 +00:00
isolated_self(self, 2) {
self.bump();
self.bump();
SelfKind::Region(None, Mutability::Mutable)
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
2016-10-19 20:33:41 +00:00
isolated_self(self, 2) {
self.bump();
2017-03-16 21:47:32 +00:00
let lt = self.expect_lifetime();
SelfKind::Region(Some(lt), Mutability::Immutable)
} else if self.look_ahead(1, |t| t.is_lifetime()) &&
self.is_keyword_ahead(2, &[kw::Mut]) &&
2016-10-19 20:33:41 +00:00
isolated_self(self, 3) {
self.bump();
2017-03-16 21:47:32 +00:00
let lt = self.expect_lifetime();
self.bump();
SelfKind::Region(Some(lt), Mutability::Mutable)
} else {
return Ok(None);
}, expect_ident(self), self.prev_span)
}
2014-10-27 08:22:52 +00:00
token::BinOp(token::Star) => {
// `*self`
// `*const self`
// `*mut self`
// `*not_self`
// Emit special error for `self` cases.
2019-01-12 06:04:54 +00:00
let msg = "cannot pass `self` by raw pointer";
(if isolated_self(self, 1) {
self.bump();
self.struct_span_err(self.token.span, msg)
.span_label(self.token.span, msg)
2019-01-12 06:04:54 +00:00
.emit();
SelfKind::Value(Mutability::Immutable)
} else if self.look_ahead(1, |t| t.is_mutability()) &&
2016-10-19 20:33:41 +00:00
isolated_self(self, 2) {
self.bump();
self.bump();
self.struct_span_err(self.token.span, msg)
.span_label(self.token.span, msg)
2019-01-12 06:04:54 +00:00
.emit();
SelfKind::Value(Mutability::Immutable)
} else {
return Ok(None);
}, expect_ident(self), self.prev_span)
}
2014-10-27 08:22:52 +00:00
token::Ident(..) => {
2016-10-19 20:33:41 +00:00
if isolated_self(self, 0) {
// `self`
// `self: TYPE`
let eself_ident = expect_ident(self);
let eself_hi = self.prev_span;
(if self.eat(&token::Colon) {
let ty = self.parse_ty()?;
SelfKind::Explicit(ty, Mutability::Immutable)
} else {
SelfKind::Value(Mutability::Immutable)
}, eself_ident, eself_hi)
2019-05-11 14:41:37 +00:00
} else if self.token.is_keyword(kw::Mut) &&
2016-10-19 20:33:41 +00:00
isolated_self(self, 1) {
// `mut self`
// `mut self: TYPE`
self.bump();
let eself_ident = expect_ident(self);
let eself_hi = self.prev_span;
(if self.eat(&token::Colon) {
let ty = self.parse_ty()?;
SelfKind::Explicit(ty, Mutability::Mutable)
} else {
SelfKind::Value(Mutability::Mutable)
}, eself_ident, eself_hi)
} else {
return Ok(None);
}
}
_ => return Ok(None),
};
2016-03-06 12:54:44 +00:00
2018-08-18 10:14:03 +00:00
let eself = source_map::respan(eself_lo.to(eself_hi), eself);
Ok(Some(Arg::from_self(ThinVec::default(), eself, eself_ident)))
}
/// Returns the parsed optional self argument with attributes and whether a self
/// shortcut was used.
fn parse_self_arg_with_attrs(&mut self) -> PResult<'a, Option<Arg>> {
let attrs = self.parse_arg_attributes()?;
let arg_opt = self.parse_self_arg()?;
Ok(arg_opt.map(|mut arg| {
arg.attrs = attrs.into();
arg
}))
2016-03-06 12:54:44 +00:00
}
2019-02-08 13:53:55 +00:00
/// Parses the parameter list and result type of a function that may have a `self` parameter.
fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> PResult<'a, P<FnDecl>>
2016-03-06 12:54:44 +00:00
where F: FnMut(&mut Parser<'a>) -> PResult<'a, Arg>,
{
self.expect(&token::OpenDelim(token::Paren))?;
// Parse optional self argument.
let self_arg = self.parse_self_arg_with_attrs()?;
2012-07-30 23:33:02 +00:00
// Parse the rest of the function parameter list.
let sep = SeqSep::trailing_allowed(token::Comma);
let (mut fn_inputs, recovered) = if let Some(self_arg) = self_arg {
2016-03-06 12:54:44 +00:00
if self.check(&token::CloseDelim(token::Paren)) {
(vec![self_arg], false)
2016-03-06 12:54:44 +00:00
} else if self.eat(&token::Comma) {
let mut fn_inputs = vec![self_arg];
let (mut input, _, recovered) = self.parse_seq_to_before_end(
&token::CloseDelim(token::Paren), sep, parse_arg_fn)?;
fn_inputs.append(&mut input);
(fn_inputs, recovered)
2016-03-06 12:54:44 +00:00
} else {
match self.expect_one_of(&[], &[]) {
Err(err) => return Err(err),
Ok(recovered) => (vec![self_arg], recovered),
}
2012-07-30 23:33:02 +00:00
}
2016-03-06 12:54:44 +00:00
} else {
let (input, _, recovered) =
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)?;
(input, recovered)
};
2012-07-30 23:33:02 +00:00
if !recovered {
// Parse closing paren and return type.
self.expect(&token::CloseDelim(token::Paren))?;
}
// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
self.deduplicate_recovered_arg_names(&mut fn_inputs);
Ok(P(FnDecl {
inputs: fn_inputs,
output: self.parse_ret_ty(true)?,
c_variadic: false
}))
2012-07-30 23:33:02 +00:00
}
fn choose_generics_over_qpath(&self) -> bool {
// There's an ambiguity between generic parameters and qualified paths in impls.
// If we see `<` it may start both, so we have to inspect some following tokens.
// The following combinations can only start generics,
// but not qualified paths (with one exception):
// `<` `>` - empty generic parameters
// `<` `#` - generic parameters with attributes
// `<` (LIFETIME|IDENT) `>` - single generic parameter
// `<` (LIFETIME|IDENT) `,` - first generic parameter in a list
// `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
// `<` (LIFETIME|IDENT) `=` - generic parameter with a default
2019-02-07 09:10:11 +00:00
// `<` const - generic const parameter
// The only truly ambiguous case is
// `<` IDENT `>` `::` IDENT ...
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
// because this is what almost always expected in practice, qualified paths in impls
// (`impl <Type>::AssocTy { ... }`) aren't even allowed by type checker at the moment.
self.token == token::Lt &&
(self.look_ahead(1, |t| t == &token::Pound || t == &token::Gt) ||
self.look_ahead(1, |t| t.is_lifetime() || t.is_ident()) &&
self.look_ahead(2, |t| t == &token::Gt || t == &token::Comma ||
t == &token::Colon || t == &token::Eq) ||
self.is_keyword_ahead(1, &[kw::Const]))
}
2019-08-11 16:34:42 +00:00
/// Parses `pub`, `pub(crate)` and `pub(in path)` plus shortcuts `crate` for `pub(crate)`,
/// `pub(self)` for `pub(in self)` and `pub(super)` for `pub(in super)`.
/// If the following element can't be a tuple (i.e., it's a function definition), then
/// it's not a tuple struct field), and the contents within the parentheses isn't valid,
/// so emit a proper diagnostic.
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
maybe_whole!(self, NtVis, |x| x);
2019-08-11 16:34:42 +00:00
self.expected_tokens.push(TokenType::Keyword(kw::Crate));
if self.is_crate_vis() {
self.bump(); // `crate`
return Ok(respan(self.prev_span, VisibilityKind::Crate(CrateSugar::JustCrate)));
}
2019-08-11 16:34:42 +00:00
if !self.eat_keyword(kw::Pub) {
// We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
// keyword to grab a span from for inherited visibility; an empty span at the
// beginning of the current token would seem to be the "Schelling span".
return Ok(respan(self.token.span.shrink_to_lo(), VisibilityKind::Inherited))
}
let lo = self.prev_span;
2017-03-07 23:50:13 +00:00
if self.check(&token::OpenDelim(token::Paren)) {
// We don't `self.bump()` the `(` yet because this might be a struct definition where
// `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
// Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
// by the following tokens.
if self.is_keyword_ahead(1, &[kw::Crate]) &&
self.look_ahead(2, |t| t != &token::ModSep) // account for `pub(crate::foo)`
{
2017-03-07 23:50:13 +00:00
// `pub(crate)`
self.bump(); // `(`
self.bump(); // `crate`
self.expect(&token::CloseDelim(token::Paren))?; // `)`
2018-01-29 05:12:09 +00:00
let vis = respan(
lo.to(self.prev_span),
VisibilityKind::Crate(CrateSugar::PubCrate),
);
2017-03-07 23:50:13 +00:00
return Ok(vis)
} else if self.is_keyword_ahead(1, &[kw::In]) {
2017-03-07 23:50:13 +00:00
// `pub(in path)`
self.bump(); // `(`
self.bump(); // `in`
let path = self.parse_path(PathStyle::Mod)?; // `path`
2017-03-07 23:50:13 +00:00
self.expect(&token::CloseDelim(token::Paren))?; // `)`
2018-01-29 05:12:09 +00:00
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
path: P(path),
id: ast::DUMMY_NODE_ID,
2018-01-29 05:12:09 +00:00
});
2017-03-07 23:50:13 +00:00
return Ok(vis)
} else if self.look_ahead(2, |t| t == &token::CloseDelim(token::Paren)) &&
self.is_keyword_ahead(1, &[kw::Super, kw::SelfLower])
{
2017-03-07 23:50:13 +00:00
// `pub(self)` or `pub(super)`
self.bump(); // `(`
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
2017-03-07 23:50:13 +00:00
self.expect(&token::CloseDelim(token::Paren))?; // `)`
2018-01-29 05:12:09 +00:00
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
path: P(path),
id: ast::DUMMY_NODE_ID,
2018-01-29 05:12:09 +00:00
});
2017-03-07 23:50:13 +00:00
return Ok(vis)
} else if !can_take_tuple { // Provide this diagnostic if this is not a tuple struct
// `pub(something) fn ...` or `struct X { pub(something) y: Z }`
self.bump(); // `(`
let msg = "incorrect visibility restriction";
let suggestion = r##"some possible visibility restrictions are:
`pub(crate)`: visible only on the current crate
`pub(super)`: visible only in the current module's parent
`pub(in path::to::module)`: visible only on the specified path"##;
let path = self.parse_path(PathStyle::Mod)?;
let sp = path.span;
2017-05-16 13:12:24 +00:00
let help_msg = format!("make this visible only to module `{}` with `in`", path);
self.expect(&token::CloseDelim(token::Paren))?; // `)`
2019-07-24 08:51:20 +00:00
struct_span_err!(self.sess.span_diagnostic, sp, E0704, "{}", msg)
.help(suggestion)
.span_suggestion(
sp,
&help_msg,
format!("in {}", path),
Applicability::MachineApplicable,
)
.emit(); // emit diagnostic, but continue with public visibility
}
2016-04-11 00:39:35 +00:00
}
2017-03-07 23:50:13 +00:00
2018-01-29 05:12:09 +00:00
Ok(respan(lo, VisibilityKind::Public))
}
2014-06-09 20:12:30 +00:00
/// Parses a string as an ABI spec on an extern type or module. Consumes
/// the `extern` keyword, if one is found.
fn parse_opt_abi(&mut self) -> PResult<'a, Option<Abi>> {
2019-06-04 22:17:07 +00:00
match self.token.kind {
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) |
token::Literal(token::Lit { kind: token::StrRaw(..), symbol, suffix }) => {
let sp = self.token.span;
self.expect_no_suffix(sp, "an ABI spec", suffix);
self.bump();
match abi::lookup(&symbol.as_str()) {
Some(abi) => Ok(Some(abi)),
None => {
let prev_span = self.prev_span;
2019-07-24 08:51:20 +00:00
struct_span_err!(
2018-06-09 22:44:32 +00:00
self.sess.span_diagnostic,
prev_span,
2018-06-19 23:22:37 +00:00
E0703,
2018-06-09 22:44:32 +00:00
"invalid ABI: found `{}`",
2019-07-24 08:51:20 +00:00
symbol
)
.span_label(prev_span, "invalid ABI")
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
.emit();
Ok(None)
}
}
}
_ => Ok(None),
}
}
/// We are parsing `async fn`. If we are on Rust 2015, emit an error.
fn ban_async_in_2015(&self, async_span: Span) {
if async_span.rust_2015() {
self.diagnostic()
.struct_span_err_with_code(
async_span,
"`async fn` is not permitted in the 2015 edition",
DiagnosticId::Error("E0670".into())
)
.emit();
}
}
fn collect_tokens<F, R>(&mut self, f: F) -> PResult<'a, (R, TokenStream)>
where F: FnOnce(&mut Self) -> PResult<'a, R>
{
// Record all tokens we parse when parsing this item.
let mut tokens = Vec::new();
let prev_collecting = match self.token_cursor.frame.last_token {
LastToken::Collecting(ref mut list) => {
2019-06-30 18:30:01 +00:00
Some(mem::take(list))
}
LastToken::Was(ref mut last) => {
tokens.extend(last.take());
None
}
};
self.token_cursor.frame.last_token = LastToken::Collecting(tokens);
let prev = self.token_cursor.stack.len();
let ret = f(self);
let last_token = if self.token_cursor.stack.len() == prev {
&mut self.token_cursor.frame.last_token
2019-07-23 18:19:13 +00:00
} else if self.token_cursor.stack.get(prev).is_none() {
// This can happen due to a bad interaction of two unrelated recovery mechanisms with
2019-07-23 19:51:34 +00:00
// mismatched delimiters *and* recovery lookahead on the likely typo `pub ident(`
// (#62881).
return Ok((ret?, TokenStream::new(vec![])));
} else {
&mut self.token_cursor.stack[prev].last_token
};
// Pull out the tokens that we've collected from the call to `f` above.
let mut collected_tokens = match *last_token {
2019-06-30 18:30:01 +00:00
LastToken::Collecting(ref mut v) => mem::take(v),
2019-07-23 18:19:13 +00:00
LastToken::Was(ref was) => {
let msg = format!("our vector went away? - found Was({:?})", was);
debug!("collect_tokens: {}", msg);
self.sess.span_diagnostic.delay_span_bug(self.token.span, &msg);
// This can happen due to a bad interaction of two unrelated recovery mechanisms
2019-07-23 19:51:34 +00:00
// with mismatched delimiters *and* recovery lookahead on the likely typo
// `pub ident(` (#62895, different but similar to the case above).
2019-07-23 18:19:13 +00:00
return Ok((ret?, TokenStream::new(vec![])));
}
};
// If we're not at EOF our current token wasn't actually consumed by
// `f`, but it'll still be in our list that we pulled out. In that case
// put it back.
let extra_token = if self.token != token::Eof {
collected_tokens.pop()
} else {
None
};
// If we were previously collecting tokens, then this was a recursive
// call. In that case we need to record all the tokens we collected in
// our parent list as well. To do that we push a clone of our stream
// onto the previous list.
match prev_collecting {
Some(mut list) => {
list.extend(collected_tokens.iter().cloned());
list.extend(extra_token);
*last_token = LastToken::Collecting(list);
}
None => {
*last_token = LastToken::Was(extra_token);
}
}
Ok((ret?, TokenStream::new(collected_tokens)))
}
/// `::{` or `::*`
fn is_import_coupler(&mut self) -> bool {
self.check(&token::ModSep) &&
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
*t == token::BinOp(token::Star))
2016-04-17 00:48:40 +00:00
}
pub fn parse_optional_str(&mut self) -> Option<(Symbol, ast::StrStyle, Option<ast::Name>)> {
2019-06-04 22:17:07 +00:00
let ret = match self.token.kind {
token::Literal(token::Lit { kind: token::Str, symbol, suffix }) =>
(symbol, ast::StrStyle::Cooked, suffix),
token::Literal(token::Lit { kind: token::StrRaw(n), symbol, suffix }) =>
(symbol, ast::StrStyle::Raw(n), suffix),
_ => return None
};
self.bump();
Some(ret)
}
pub fn parse_str(&mut self) -> PResult<'a, (Symbol, StrStyle)> {
match self.parse_optional_str() {
Some((s, style, suf)) => {
let sp = self.prev_span;
2019-03-26 19:09:13 +00:00
self.expect_no_suffix(sp, "a string literal", suf);
Ok((s, style))
}
_ => {
let msg = "expected string literal";
let mut err = self.fatal(msg);
err.span_label(self.token.span, msg);
Err(err)
}
}
}
fn report_invalid_macro_expansion_item(&self) {
self.struct_span_err(
self.prev_span,
"macros that expand to items must be delimited with braces or followed by a semicolon",
).multipart_suggestion(
"change the delimiters to curly braces",
vec![
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), String::from(" {")),
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
],
Applicability::MaybeIncorrect,
).span_suggestion(
self.sess.source_map.next_point(self.prev_span),
"add a semicolon",
';'.to_string(),
Applicability::MaybeIncorrect,
).emit();
}
}
2019-02-05 09:35:25 +00:00
2019-03-03 20:14:25 +00:00
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {
for unmatched in unclosed_delims.iter() {
2019-02-05 09:35:25 +00:00
let mut err = handler.struct_span_err(unmatched.found_span, &format!(
"incorrect close delimiter: `{}`",
pprust::token_kind_to_string(&token::CloseDelim(unmatched.found_delim)),
2019-02-05 09:35:25 +00:00
));
err.span_label(unmatched.found_span, "incorrect close delimiter");
if let Some(sp) = unmatched.candidate_span {
err.span_label(sp, "close delimiter possibly meant for this");
}
if let Some(sp) = unmatched.unclosed_span {
err.span_label(sp, "un-closed delimiter");
}
err.emit();
}
2019-03-03 20:14:25 +00:00
unclosed_delims.clear();
2019-02-05 10:26:26 +00:00
}