2019-10-16 11:23:46 +00:00
|
|
|
pub mod attr;
|
2021-02-13 17:42:43 +00:00
|
|
|
mod attr_wrapper;
|
2020-08-02 12:05:19 +00:00
|
|
|
mod diagnostics;
|
2019-08-11 11:14:30 +00:00
|
|
|
mod expr;
|
2020-08-02 12:05:19 +00:00
|
|
|
mod generics;
|
2019-08-11 16:34:42 +00:00
|
|
|
mod item;
|
2020-07-27 12:04:54 +00:00
|
|
|
mod nonterminal;
|
2019-08-11 17:59:27 +00:00
|
|
|
mod pat;
|
|
|
|
mod path;
|
2019-08-11 18:32:29 +00:00
|
|
|
mod stmt;
|
2024-12-04 04:55:06 +00:00
|
|
|
pub mod token_type;
|
2020-08-02 12:05:19 +00:00
|
|
|
mod ty;
|
2020-07-27 12:04:54 +00:00
|
|
|
|
2024-08-11 16:10:36 +00:00
|
|
|
use std::assert_matches::debug_assert_matches;
|
2023-02-21 14:51:19 +00:00
|
|
|
use std::ops::Range;
|
2025-02-03 03:44:41 +00:00
|
|
|
use std::sync::Arc;
|
2023-02-21 14:51:19 +00:00
|
|
|
use std::{fmt, mem, slice};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-08-06 07:16:40 +00:00
|
|
|
use attr_wrapper::{AttrWrapper, UsePreAttrPos};
|
2020-08-12 22:39:15 +00:00
|
|
|
pub use diagnostics::AttemptLocalParseRecovery;
|
2023-09-08 10:14:36 +00:00
|
|
|
pub(crate) use expr::ForbiddenLetReason;
|
2021-12-04 18:05:30 +00:00
|
|
|
pub(crate) use item::FnParseMode;
|
2022-01-12 20:43:24 +00:00
|
|
|
pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
|
2024-06-03 05:47:46 +00:00
|
|
|
use path::PathStyle;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2024-04-17 22:58:06 +00:00
|
|
|
use rustc_ast::token::{
|
|
|
|
self, Delimiter, IdentIsRaw, InvisibleOrigin, MetaVarKind, Nonterminal, Token, TokenKind,
|
|
|
|
};
|
2024-12-10 08:18:44 +00:00
|
|
|
use rustc_ast::tokenstream::{AttrsTarget, Spacing, TokenStream, TokenTree};
|
2022-09-15 16:27:23 +00:00
|
|
|
use rustc_ast::util::case::Case;
|
2024-03-24 01:04:45 +00:00
|
|
|
use rustc_ast::{
|
2024-10-16 23:14:01 +00:00
|
|
|
self as ast, AnonConst, AttrArgs, AttrId, ByRef, Const, CoroutineKind, DUMMY_NODE_ID,
|
|
|
|
DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, Mutability, Recovered, Safety, StrLit,
|
|
|
|
Visibility, VisibilityKind,
|
2024-03-24 01:04:45 +00:00
|
|
|
};
|
2020-01-11 16:02:46 +00:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-11-28 23:33:17 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2024-05-09 08:44:40 +00:00
|
|
|
use rustc_errors::{Applicability, Diag, FatalError, MultiSpan, PResult};
|
2024-08-21 04:16:42 +00:00
|
|
|
use rustc_index::interval::IntervalSet;
|
2020-01-11 14:03:15 +00:00
|
|
|
use rustc_session::parse::ParseSess;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
|
2022-11-23 00:55:16 +00:00
|
|
|
use thin_vec::ThinVec;
|
2024-12-04 04:55:06 +00:00
|
|
|
use token_type::TokenTypeSet;
|
|
|
|
pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
|
2022-11-23 00:55:16 +00:00
|
|
|
use tracing::debug;
|
2018-07-03 17:38:14 +00:00
|
|
|
|
2022-09-01 17:29:23 +00:00
|
|
|
use crate::errors::{
|
2023-04-27 00:53:06 +00:00
|
|
|
self, IncorrectVisibilityRestriction, MismatchedClosingDelimiter, NonStringAbiLiteral,
|
2022-09-01 17:29:23 +00:00
|
|
|
};
|
2024-12-04 04:55:06 +00:00
|
|
|
use crate::exp;
|
2023-02-21 14:51:19 +00:00
|
|
|
use crate::lexer::UnmatchedDelim;
|
2022-08-24 20:41:51 +00:00
|
|
|
|
2024-05-02 23:26:34 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
|
|
|
// Ideally, these tests would be in `rustc_ast`. But they depend on having a
|
|
|
|
// parser, so they are here.
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tokenstream {
|
|
|
|
mod tests;
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod mut_visit {
|
|
|
|
mod tests;
|
|
|
|
}
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
bitflags::bitflags! {
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2018-05-31 22:53:30 +00:00
|
|
|
struct Restrictions: u8 {
|
2017-09-08 19:08:01 +00:00
|
|
|
const STMT_EXPR = 1 << 0;
|
|
|
|
const NO_STRUCT_LITERAL = 1 << 1;
|
2020-10-03 18:30:32 +00:00
|
|
|
const CONST_EXPR = 1 << 2;
|
2022-08-01 01:13:16 +00:00
|
|
|
const ALLOW_LET = 1 << 3;
|
2023-10-03 21:21:02 +00:00
|
|
|
const IN_IF_GUARD = 1 << 4;
|
2024-01-28 15:12:21 +00:00
|
|
|
const IS_PAT = 1 << 5;
|
2014-09-16 05:22:12 +00:00
|
|
|
}
|
2011-12-21 04:12:52 +00:00
|
|
|
}
|
2011-01-24 23:26:10 +00:00
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2019-10-08 07:35:34 +00:00
|
|
|
enum SemiColonMode {
|
2016-02-10 03:11:27 +00:00
|
|
|
Break,
|
|
|
|
Ignore,
|
2019-01-20 08:37:06 +00:00
|
|
|
Comma,
|
2016-02-10 03:11:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:58:25 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2019-10-08 07:35:34 +00:00
|
|
|
enum BlockMode {
|
2017-04-13 19:37:05 +00:00
|
|
|
Break,
|
|
|
|
Ignore,
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:47:37 +00:00
|
|
|
/// Whether or not we should force collection of tokens for an AST node,
|
|
|
|
/// regardless of whether or not it has attributes
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2021-01-18 21:47:37 +00:00
|
|
|
pub enum ForceCollect {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2019-08-11 13:24:37 +00:00
|
|
|
#[macro_export]
|
2014-11-14 17:18:10 +00:00
|
|
|
macro_rules! maybe_whole {
|
2016-11-02 03:03:55 +00:00
|
|
|
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
|
2024-03-19 20:08:22 +00:00
|
|
|
if let token::Interpolated(nt) = &$p.token.kind
|
2024-04-22 06:29:27 +00:00
|
|
|
&& let token::$constructor(x) = &**nt
|
2024-03-19 20:08:22 +00:00
|
|
|
{
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut $x = x.clone();
|
|
|
|
$p.bump();
|
|
|
|
return Ok($e);
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2016-11-02 03:03:55 +00:00
|
|
|
};
|
2014-11-14 17:18:10 +00:00
|
|
|
}
|
2012-07-04 01:39:37 +00:00
|
|
|
|
2019-03-09 14:41:01 +00:00
|
|
|
/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
|
2019-08-11 11:14:30 +00:00
|
|
|
#[macro_export]
|
2019-03-09 14:41:01 +00:00
|
|
|
macro_rules! maybe_recover_from_interpolated_ty_qpath {
|
|
|
|
($self: expr, $allow_qpath_recovery: expr) => {
|
2022-02-28 10:49:56 +00:00
|
|
|
if $allow_qpath_recovery
|
2023-11-13 13:24:55 +00:00
|
|
|
&& $self.may_recover()
|
2025-02-21 05:47:07 +00:00
|
|
|
&& let Some(mv_kind) = $self.token.is_metavar_seq()
|
|
|
|
&& let token::MetaVarKind::Ty { .. } = mv_kind
|
2024-04-17 03:17:44 +00:00
|
|
|
&& $self.check_noexpect_past_close_delim(&token::PathSep)
|
2023-11-13 13:24:55 +00:00
|
|
|
{
|
2024-04-17 03:17:44 +00:00
|
|
|
// Reparse the type, then move to recovery.
|
|
|
|
let ty = $self
|
2025-02-21 05:47:07 +00:00
|
|
|
.eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
|
2024-04-17 03:17:44 +00:00
|
|
|
.expect("metavar seq ty");
|
|
|
|
|
2023-11-13 13:24:55 +00:00
|
|
|
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
|
|
|
|
}
|
2019-03-09 14:41:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2022-10-25 19:24:01 +00:00
|
|
|
pub enum Recovery {
|
|
|
|
Allowed,
|
|
|
|
Forbidden,
|
|
|
|
}
|
|
|
|
|
2017-06-15 03:42:24 +00:00
|
|
|
#[derive(Clone)]
|
2014-03-09 14:54:34 +00:00
|
|
|
pub struct Parser<'a> {
|
2024-03-04 05:31:49 +00:00
|
|
|
pub psess: &'a ParseSess,
|
2020-03-07 13:34:29 +00:00
|
|
|
/// The current token.
|
2020-02-24 10:04:13 +00:00
|
|
|
pub token: Token,
|
2023-08-08 01:43:44 +00:00
|
|
|
/// The spacing for the current token.
|
2024-06-03 05:47:46 +00:00
|
|
|
token_spacing: Spacing,
|
2020-03-07 13:34:29 +00:00
|
|
|
/// The previous token.
|
2020-02-24 10:04:13 +00:00
|
|
|
pub prev_token: Token,
|
2020-11-28 23:33:17 +00:00
|
|
|
pub capture_cfg: bool,
|
2018-05-31 22:53:30 +00:00
|
|
|
restrictions: Restrictions,
|
2024-12-04 04:55:06 +00:00
|
|
|
expected_token_types: TokenTypeSet,
|
2019-08-31 13:03:54 +00:00
|
|
|
token_cursor: TokenCursor,
|
2023-07-31 06:15:54 +00:00
|
|
|
// The number of calls to `bump`, i.e. the position in the token stream.
|
2024-07-02 06:31:24 +00:00
|
|
|
num_bump_calls: u32,
|
2024-09-19 09:32:17 +00:00
|
|
|
// During parsing we may sometimes need to "unglue" a glued token into two
|
|
|
|
// or three component tokens (e.g. `>>` into `>` and `>`, or `>>=` into `>`
|
|
|
|
// and `>` and `=`), so the parser can consume them one at a time. This
|
|
|
|
// process bypasses the normal capturing mechanism (e.g. `num_bump_calls`
|
|
|
|
// will not be incremented), since the "unglued" tokens due not exist in
|
|
|
|
// the original `TokenStream`.
|
2023-07-31 06:36:27 +00:00
|
|
|
//
|
2024-09-19 09:32:17 +00:00
|
|
|
// If we end up consuming all the component tokens, this is not an issue,
|
|
|
|
// because we'll end up capturing the single "glued" token.
|
2023-07-31 06:36:27 +00:00
|
|
|
//
|
2024-09-19 09:32:17 +00:00
|
|
|
// However, sometimes we may want to capture not all of the original
|
2023-07-31 06:36:27 +00:00
|
|
|
// token. For example, capturing the `Vec<u8>` in `Option<Vec<u8>>`
|
|
|
|
// requires us to unglue the trailing `>>` token. The `break_last_token`
|
2024-09-19 09:32:17 +00:00
|
|
|
// field is used to track these tokens. They get appended to the captured
|
2023-07-31 06:36:27 +00:00
|
|
|
// stream when we evaluate a `LazyAttrTokenStream`.
|
2024-09-19 09:32:17 +00:00
|
|
|
//
|
|
|
|
// This value is always 0, 1, or 2. It can only reach 2 when splitting
|
|
|
|
// `>>=` or `<<=`.
|
|
|
|
break_last_token: u32,
|
2019-01-23 01:35:13 +00:00
|
|
|
/// 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.
|
2023-10-24 22:22:52 +00:00
|
|
|
unmatched_angle_bracket_count: u16,
|
|
|
|
angle_bracket_nesting: u16,
|
2023-03-03 22:48:21 +00:00
|
|
|
|
2019-10-08 07:35:34 +00:00
|
|
|
last_unexpected_token_span: Option<Span>,
|
2019-05-22 05:17:53 +00:00
|
|
|
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
|
2019-10-08 07:35:34 +00:00
|
|
|
subparser_name: Option<&'static str>,
|
2020-11-28 23:33:17 +00:00
|
|
|
capture_state: CaptureState,
|
2021-08-16 13:22:36 +00:00
|
|
|
/// This allows us to recover when the user forget to add braces around
|
|
|
|
/// multiple statements in the closure body.
|
2024-05-05 19:23:37 +00:00
|
|
|
current_closure: Option<ClosureSpans>,
|
2022-10-26 19:09:28 +00:00
|
|
|
/// Whether the parser is allowed to do recovery.
|
2022-10-25 19:24:01 +00:00
|
|
|
/// This is disabled when parsing macro arguments, see #103534
|
2024-06-03 05:47:46 +00:00
|
|
|
recovery: Recovery,
|
2021-08-16 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 09:06:44 +00:00
|
|
|
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
|
2025-01-22 01:24:29 +00:00
|
|
|
// nonterminals. Make sure it doesn't unintentionally get bigger. We only check a few arches
|
|
|
|
// though, because `TokenTypeSet(u128)` alignment varies on others, changing the total size.
|
|
|
|
#[cfg(all(target_pointer_width = "64", any(target_arch = "aarch64", target_arch = "x86_64")))]
|
2024-08-21 04:16:42 +00:00
|
|
|
rustc_data_structures::static_assert_size!(Parser<'_>, 288);
|
2022-04-19 23:31:34 +00:00
|
|
|
|
2022-03-30 19:14:15 +00:00
|
|
|
/// Stores span information about a closure.
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2024-06-03 05:47:46 +00:00
|
|
|
struct ClosureSpans {
|
|
|
|
whole_closure: Span,
|
|
|
|
closing_pipe: Span,
|
|
|
|
body: Span,
|
2020-11-28 23:33:17 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 20:44:39 +00:00
|
|
|
/// A token range within a `Parser`'s full token stream.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct ParserRange(Range<u32>);
|
|
|
|
|
|
|
|
/// A token range within an individual AST node's (lazy) token stream, i.e.
|
|
|
|
/// relative to that node's first token. Distinct from `ParserRange` so the two
|
|
|
|
/// kinds of range can't be mixed up.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct NodeRange(Range<u32>);
|
|
|
|
|
|
|
|
/// Indicates a range of tokens that should be replaced by an `AttrsTarget`
|
|
|
|
/// (replacement) or be replaced by nothing (deletion). This is used in two
|
|
|
|
/// places during token collection.
|
|
|
|
///
|
|
|
|
/// 1. Replacement. During the parsing of an AST node that may have a
|
|
|
|
/// `#[derive]` attribute, when we parse a nested AST node that has `#[cfg]`
|
|
|
|
/// or `#[cfg_attr]`, we replace the entire inner AST node with
|
|
|
|
/// `FlatToken::AttrsTarget`. This lets us perform eager cfg-expansion on an
|
|
|
|
/// `AttrTokenStream`.
|
2020-11-28 23:33:17 +00:00
|
|
|
///
|
2024-07-31 20:44:39 +00:00
|
|
|
/// 2. Deletion. We delete inner attributes from all collected token streams,
|
|
|
|
/// and instead track them through the `attrs` field on the AST node. This
|
|
|
|
/// lets us manipulate them similarly to outer attributes. When we create a
|
|
|
|
/// `TokenStream`, the inner attributes are inserted into the proper place
|
|
|
|
/// in the token stream.
|
2020-11-28 23:33:17 +00:00
|
|
|
///
|
2024-07-31 20:44:39 +00:00
|
|
|
/// Each replacement starts off in `ParserReplacement` form but is converted to
|
|
|
|
/// `NodeReplacement` form when it is attached to a single AST node, via
|
|
|
|
/// `LazyAttrTokenStreamImpl`.
|
|
|
|
type ParserReplacement = (ParserRange, Option<AttrsTarget>);
|
|
|
|
|
|
|
|
/// See the comment on `ParserReplacement`.
|
|
|
|
type NodeReplacement = (NodeRange, Option<AttrsTarget>);
|
|
|
|
|
|
|
|
impl NodeRange {
|
|
|
|
// Converts a range within a parser's tokens to a range within a
|
|
|
|
// node's tokens beginning at `start_pos`.
|
|
|
|
//
|
|
|
|
// For example, imagine a parser with 50 tokens in its token stream, a
|
|
|
|
// function that spans `ParserRange(20..40)` and an inner attribute within
|
|
|
|
// that function that spans `ParserRange(30..35)`. We would find the inner
|
|
|
|
// attribute's range within the function's tokens by subtracting 20, which
|
|
|
|
// is the position of the function's start token. This gives
|
|
|
|
// `NodeRange(10..15)`.
|
|
|
|
fn new(ParserRange(parser_range): ParserRange, start_pos: u32) -> NodeRange {
|
2024-08-21 20:58:31 +00:00
|
|
|
assert!(!parser_range.is_empty());
|
|
|
|
assert!(parser_range.start >= start_pos);
|
2024-07-31 20:44:39 +00:00
|
|
|
NodeRange((parser_range.start - start_pos)..(parser_range.end - start_pos))
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 23:33:17 +00:00
|
|
|
|
|
|
|
/// Controls how we capture tokens. Capturing can be expensive,
|
|
|
|
/// so we try to avoid performing capturing in cases where
|
2022-09-09 02:44:05 +00:00
|
|
|
/// we will never need an `AttrTokenStream`.
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2024-06-03 05:47:46 +00:00
|
|
|
enum Capturing {
|
2020-11-28 23:33:17 +00:00
|
|
|
/// We aren't performing any capturing - this is the default mode.
|
|
|
|
No,
|
|
|
|
/// We are capturing tokens
|
|
|
|
Yes,
|
|
|
|
}
|
|
|
|
|
2024-08-06 07:16:40 +00:00
|
|
|
// This state is used by `Parser::collect_tokens`.
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-11-28 23:33:17 +00:00
|
|
|
struct CaptureState {
|
|
|
|
capturing: Capturing,
|
2024-07-31 20:44:39 +00:00
|
|
|
parser_replacements: Vec<ParserReplacement>,
|
|
|
|
inner_attr_parser_ranges: FxHashMap<AttrId, ParserRange>,
|
2024-08-21 04:16:42 +00:00
|
|
|
// `IntervalSet` is good for perf because attrs are mostly added to this
|
|
|
|
// set in contiguous ranges.
|
|
|
|
seen_attrs: IntervalSet<AttrId>,
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 08:18:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct TokenTreeCursor {
|
|
|
|
stream: TokenStream,
|
|
|
|
/// Points to the current token tree in the stream. In `TokenCursor::curr`,
|
|
|
|
/// this can be any token tree. In `TokenCursor::stack`, this is always a
|
|
|
|
/// `TokenTree::Delimited`.
|
|
|
|
index: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenTreeCursor {
|
|
|
|
#[inline]
|
|
|
|
fn new(stream: TokenStream) -> Self {
|
|
|
|
TokenTreeCursor { stream, index: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn curr(&self) -> Option<&TokenTree> {
|
|
|
|
self.stream.get(self.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bump(&mut self) {
|
|
|
|
self.index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A `TokenStream` cursor that produces `Token`s. It's a bit odd that
|
2023-02-01 01:58:04 +00:00
|
|
|
/// we (a) lex tokens into a nice tree structure (`TokenStream`), and then (b)
|
|
|
|
/// use this type to emit them as a linear sequence. But a linear sequence is
|
|
|
|
/// what the parser expects, for the most part.
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-08-31 13:03:54 +00:00
|
|
|
struct TokenCursor {
|
2024-12-10 08:18:44 +00:00
|
|
|
// Cursor for the current (innermost) token stream. The index within the
|
|
|
|
// cursor can point to any token tree in the stream (or one past the end).
|
|
|
|
// The delimiters for this token stream are found in `self.stack.last()`;
|
|
|
|
// if that is `None` we are in the outermost token stream which never has
|
|
|
|
// delimiters.
|
|
|
|
curr: TokenTreeCursor,
|
|
|
|
|
|
|
|
// Token streams surrounding the current one. The index within each cursor
|
|
|
|
// always points to a `TokenTree::Delimited`.
|
|
|
|
stack: Vec<TokenTreeCursor>,
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenCursor {
|
2023-07-25 23:17:32 +00:00
|
|
|
fn next(&mut self) -> (Token, Spacing) {
|
|
|
|
self.inlined_next()
|
2022-03-07 04:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This always-inlined version should only be used on hot code paths.
|
|
|
|
#[inline(always)]
|
2023-07-25 23:17:32 +00:00
|
|
|
fn inlined_next(&mut self) -> (Token, Spacing) {
|
2022-04-19 07:01:26 +00:00
|
|
|
loop {
|
2023-07-31 01:10:25 +00:00
|
|
|
// FIXME: we currently don't return `Delimiter::Invisible` open/close delims. To fix
|
|
|
|
// #67062 we will need to, whereupon the `delim != Delimiter::Invisible` conditions
|
|
|
|
// below can be removed.
|
2024-12-10 08:18:44 +00:00
|
|
|
if let Some(tree) = self.curr.curr() {
|
2022-04-20 02:22:42 +00:00
|
|
|
match tree {
|
2023-07-25 23:17:32 +00:00
|
|
|
&TokenTree::Token(ref token, spacing) => {
|
2023-07-30 07:16:20 +00:00
|
|
|
debug_assert!(!matches!(
|
|
|
|
token.kind,
|
|
|
|
token::OpenDelim(_) | token::CloseDelim(_)
|
|
|
|
));
|
2024-12-10 08:18:44 +00:00
|
|
|
let res = (token.clone(), spacing);
|
|
|
|
self.curr.bump();
|
|
|
|
return res;
|
2023-07-25 23:17:32 +00:00
|
|
|
}
|
2023-10-12 04:36:14 +00:00
|
|
|
&TokenTree::Delimited(sp, spacing, delim, ref tts) => {
|
2024-12-10 08:18:44 +00:00
|
|
|
let trees = TokenTreeCursor::new(tts.clone());
|
|
|
|
self.stack.push(mem::replace(&mut self.curr, trees));
|
2024-04-16 23:59:27 +00:00
|
|
|
if !delim.skip() {
|
2023-10-12 04:36:14 +00:00
|
|
|
return (Token::new(token::OpenDelim(delim), sp.open), spacing.open);
|
2022-04-20 02:22:42 +00:00
|
|
|
}
|
2022-08-18 02:13:37 +00:00
|
|
|
// No open delimiter to return; continue on to the next iteration.
|
2022-04-19 03:41:02 +00:00
|
|
|
}
|
2022-04-19 07:01:26 +00:00
|
|
|
};
|
2024-12-10 08:18:44 +00:00
|
|
|
} else if let Some(parent) = self.stack.pop() {
|
2023-02-01 01:43:13 +00:00
|
|
|
// We have exhausted this token stream. Move back to its parent token stream.
|
2024-12-10 08:18:44 +00:00
|
|
|
let Some(&TokenTree::Delimited(span, spacing, delim, _)) = parent.curr() else {
|
|
|
|
panic!("parent should be Delimited")
|
|
|
|
};
|
|
|
|
self.curr = parent;
|
|
|
|
self.curr.bump(); // move past the `Delimited`
|
2024-04-16 23:59:27 +00:00
|
|
|
if !delim.skip() {
|
2023-10-12 04:36:14 +00:00
|
|
|
return (Token::new(token::CloseDelim(delim), span.close), spacing.close);
|
2022-04-21 02:26:58 +00:00
|
|
|
}
|
|
|
|
// No close delimiter to return; continue on to the next iteration.
|
2017-02-20 05:44:06 +00:00
|
|
|
} else {
|
2023-08-08 01:43:44 +00:00
|
|
|
// We have exhausted the outermost token stream. The use of
|
|
|
|
// `Spacing::Alone` is arbitrary and immaterial, because the
|
|
|
|
// `Eof` token's spacing is never used.
|
2022-04-19 03:41:02 +00:00
|
|
|
return (Token::new(token::Eof, DUMMY_SP), Spacing::Alone);
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
2022-04-19 07:01:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-20 05:44:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 10:59:59 +00:00
|
|
|
/// A sequence separator.
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Debug)]
|
2024-12-04 04:55:06 +00:00
|
|
|
struct SeqSep<'a> {
|
2019-10-08 10:59:59 +00:00
|
|
|
/// The separator token.
|
2024-12-04 04:55:06 +00:00
|
|
|
sep: Option<ExpTokenPair<'a>>,
|
2019-10-08 10:59:59 +00:00
|
|
|
/// `true` if a trailing separator is allowed.
|
|
|
|
trailing_sep_allowed: bool,
|
|
|
|
}
|
|
|
|
|
2024-12-04 04:55:06 +00:00
|
|
|
impl<'a> SeqSep<'a> {
|
|
|
|
fn trailing_allowed(sep: ExpTokenPair<'a>) -> SeqSep<'a> {
|
|
|
|
SeqSep { sep: Some(sep), trailing_sep_allowed: true }
|
2019-10-08 10:59:59 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 04:55:06 +00:00
|
|
|
fn none() -> SeqSep<'a> {
|
2019-10-08 10:59:59 +00:00
|
|
|
SeqSep { sep: None, trailing_sep_allowed: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Debug)]
|
2019-11-07 10:26:36 +00:00
|
|
|
pub enum FollowedByType {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:48:23 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2024-06-03 05:47:46 +00:00
|
|
|
enum Trailing {
|
2024-02-13 23:48:23 +00:00
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
}
|
|
|
|
|
2024-08-06 00:17:46 +00:00
|
|
|
impl From<bool> for Trailing {
|
|
|
|
fn from(b: bool) -> Trailing {
|
|
|
|
if b { Trailing::Yes } else { Trailing::No }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 19:44:40 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2024-06-03 05:47:46 +00:00
|
|
|
pub(super) enum TokenDescription {
|
2022-09-04 08:14:00 +00:00
|
|
|
ReservedIdentifier,
|
|
|
|
Keyword,
|
|
|
|
ReservedKeyword,
|
|
|
|
DocComment,
|
2024-04-17 22:58:06 +00:00
|
|
|
|
|
|
|
// Expanded metavariables are wrapped in invisible delimiters which aren't
|
|
|
|
// pretty-printed. In error messages we must handle these specially
|
|
|
|
// otherwise we get confusing things in messages like "expected `(`, found
|
|
|
|
// ``". It's better to say e.g. "expected `(`, found type metavariable".
|
|
|
|
MetaVar(MetaVarKind),
|
2022-09-04 08:14:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 16:39:17 +00:00
|
|
|
impl TokenDescription {
|
2024-06-03 05:47:46 +00:00
|
|
|
pub(super) fn from_token(token: &Token) -> Option<Self> {
|
2022-09-22 16:39:17 +00:00
|
|
|
match token.kind {
|
|
|
|
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
|
|
|
|
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
|
|
|
|
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
|
|
|
|
token::DocComment(..) => Some(TokenDescription::DocComment),
|
2024-04-17 22:58:06 +00:00
|
|
|
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => {
|
|
|
|
Some(TokenDescription::MetaVar(kind))
|
|
|
|
}
|
2022-09-22 16:39:17 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 07:08:58 +00:00
|
|
|
pub fn token_descr(token: &Token) -> String {
|
2024-04-17 22:58:06 +00:00
|
|
|
let s = pprust::token_to_string(token).to_string();
|
|
|
|
|
|
|
|
match (TokenDescription::from_token(token), &token.kind) {
|
|
|
|
(Some(TokenDescription::ReservedIdentifier), _) => format!("reserved identifier `{s}`"),
|
|
|
|
(Some(TokenDescription::Keyword), _) => format!("keyword `{s}`"),
|
|
|
|
(Some(TokenDescription::ReservedKeyword), _) => format!("reserved keyword `{s}`"),
|
|
|
|
(Some(TokenDescription::DocComment), _) => format!("doc comment `{s}`"),
|
|
|
|
// Deliberately doesn't print `s`, which is empty.
|
|
|
|
(Some(TokenDescription::MetaVar(kind)), _) => format!("`{kind}` metavariable"),
|
|
|
|
(None, TokenKind::NtIdent(..)) => format!("identifier `{s}`"),
|
|
|
|
(None, TokenKind::NtLifetime(..)) => format!("lifetime `{s}`"),
|
|
|
|
(None, TokenKind::Interpolated(node)) => format!("{} `{s}`", node.descr()),
|
|
|
|
(None, _) => format!("`{s}`"),
|
|
|
|
}
|
2019-12-07 02:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:54:34 +00:00
|
|
|
impl<'a> Parser<'a> {
|
2019-10-16 08:59:30 +00:00
|
|
|
pub fn new(
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &'a ParseSess,
|
2023-07-31 02:49:01 +00:00
|
|
|
stream: TokenStream,
|
2019-05-22 05:17:53 +00:00
|
|
|
subparser_name: Option<&'static str>,
|
2019-05-22 00:47:23 +00:00
|
|
|
) -> Self {
|
2016-11-03 07:43:29 +00:00
|
|
|
let mut parser = Parser {
|
2024-03-04 05:31:49 +00:00
|
|
|
psess,
|
2019-06-05 06:39:34 +00:00
|
|
|
token: Token::dummy(),
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
token_spacing: Spacing::Alone,
|
2020-02-09 14:54:38 +00:00
|
|
|
prev_token: Token::dummy(),
|
2020-11-28 23:33:17 +00:00
|
|
|
capture_cfg: false,
|
2015-04-29 21:58:43 +00:00
|
|
|
restrictions: Restrictions::empty(),
|
2024-12-04 04:55:06 +00:00
|
|
|
expected_token_types: TokenTypeSet::new(),
|
2024-12-10 08:18:44 +00:00
|
|
|
token_cursor: TokenCursor { curr: TokenTreeCursor::new(stream), stack: Vec::new() },
|
2023-07-31 06:15:54 +00:00
|
|
|
num_bump_calls: 0,
|
2024-09-19 09:32:17 +00:00
|
|
|
break_last_token: 0,
|
2019-01-23 01:35:13 +00:00
|
|
|
unmatched_angle_bracket_count: 0,
|
2023-10-24 22:22:52 +00:00
|
|
|
angle_bracket_nesting: 0,
|
2019-03-02 05:47:06 +00:00
|
|
|
last_unexpected_token_span: None,
|
2019-05-22 05:17:53 +00:00
|
|
|
subparser_name,
|
2020-11-28 23:33:17 +00:00
|
|
|
capture_state: CaptureState {
|
|
|
|
capturing: Capturing::No,
|
2024-07-31 20:44:39 +00:00
|
|
|
parser_replacements: Vec::new(),
|
|
|
|
inner_attr_parser_ranges: Default::default(),
|
2024-08-21 04:16:42 +00:00
|
|
|
seen_attrs: IntervalSet::new(u32::MAX as usize),
|
2020-11-28 23:33:17 +00:00
|
|
|
},
|
2021-08-16 13:22:36 +00:00
|
|
|
current_closure: None,
|
2022-10-25 19:24:01 +00:00
|
|
|
recovery: Recovery::Allowed,
|
2016-11-03 07:43:29 +00:00
|
|
|
};
|
|
|
|
|
2020-02-16 13:47:24 +00:00
|
|
|
// Make parser point to the first token.
|
|
|
|
parser.bump();
|
2017-05-17 22:37:24 +00:00
|
|
|
|
2024-07-17 07:59:05 +00:00
|
|
|
// Change this from 1 back to 0 after the bump. This eases debugging of
|
2024-08-06 07:16:40 +00:00
|
|
|
// `Parser::collect_tokens` because 0-indexed token positions are nicer
|
|
|
|
// than 1-indexed token positions.
|
2024-07-17 07:59:05 +00:00
|
|
|
parser.num_bump_calls = 0;
|
|
|
|
|
2016-11-03 07:43:29 +00:00
|
|
|
parser
|
|
|
|
}
|
|
|
|
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2022-11-12 21:12:33 +00:00
|
|
|
pub fn recovery(mut self, recovery: Recovery) -> Self {
|
|
|
|
self.recovery = recovery;
|
2022-10-25 19:24:01 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:09:28 +00:00
|
|
|
/// Whether the parser is allowed to recover from broken code.
|
|
|
|
///
|
|
|
|
/// If this returns false, recovering broken code into valid code (especially if this recovery does lookahead)
|
|
|
|
/// is not allowed. All recovery done by the parser must be gated behind this check.
|
|
|
|
///
|
2022-10-26 20:06:35 +00:00
|
|
|
/// Technically, this only needs to restrict eager recovery by doing lookahead at more tokens.
|
2022-10-26 19:09:28 +00:00
|
|
|
/// But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2022-10-25 19:24:01 +00:00
|
|
|
fn may_recover(&self) -> bool {
|
|
|
|
matches!(self.recovery, Recovery::Allowed)
|
|
|
|
}
|
|
|
|
|
2024-03-15 11:36:21 +00:00
|
|
|
/// Version of [`unexpected`](Parser::unexpected) that "returns" any type in the `Ok`
|
|
|
|
/// (both those functions never return "Ok", and so can lie like that in the type).
|
|
|
|
pub fn unexpected_any<T>(&mut self) -> PResult<'a, T> {
|
2015-03-28 21:58:51 +00:00
|
|
|
match self.expect_one_of(&[], &[]) {
|
2015-12-30 23:11:53 +00:00
|
|
|
Err(e) => Err(e),
|
2019-11-13 11:05:37 +00:00
|
|
|
// We can get `Ok(true)` from `recover_closing_delimiter`
|
|
|
|
// which is called in `expected_one_of_not_found`.
|
|
|
|
Ok(_) => FatalError.raise(),
|
2015-03-28 21:58:51 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-15 11:36:21 +00:00
|
|
|
pub fn unexpected(&mut self) -> PResult<'a, ()> {
|
|
|
|
self.unexpected_any()
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
|
2024-12-04 04:55:06 +00:00
|
|
|
pub fn expect(&mut self, exp: ExpTokenPair<'_>) -> PResult<'a, Recovered> {
|
2024-12-03 09:09:29 +00:00
|
|
|
if self.expected_token_types.is_empty() {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.token == *exp.tok {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2024-02-13 23:44:33 +00:00
|
|
|
Ok(Recovered::No)
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
} else {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.unexpected_try_recover(exp.tok)
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
} else {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expect_one_of(slice::from_ref(&exp), &[])
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Expect next token to be edible or inedible token. If edible,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// then consume it; if inedible, then return without consuming
|
2022-11-16 20:34:16 +00:00
|
|
|
/// anything. Signal a fatal error if next token is unexpected.
|
2024-06-03 05:47:46 +00:00
|
|
|
fn expect_one_of(
|
2019-01-28 05:04:50 +00:00
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
edible: &[ExpTokenPair<'_>],
|
|
|
|
inedible: &[ExpTokenPair<'_>],
|
2024-02-13 23:44:33 +00:00
|
|
|
) -> PResult<'a, Recovered> {
|
2024-12-04 04:55:06 +00:00
|
|
|
if edible.iter().any(|exp| exp.tok == &self.token.kind) {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2024-02-13 23:44:33 +00:00
|
|
|
Ok(Recovered::No)
|
2024-12-04 04:55:06 +00:00
|
|
|
} else if inedible.iter().any(|exp| exp.tok == &self.token.kind) {
|
2013-08-05 20:18:29 +00:00
|
|
|
// leave it in the input
|
2024-02-13 23:44:33 +00:00
|
|
|
Ok(Recovered::No)
|
2024-08-09 07:44:47 +00:00
|
|
|
} else if self.token != token::Eof
|
2023-05-04 08:30:02 +00:00
|
|
|
&& self.last_unexpected_token_span == Some(self.token.span)
|
|
|
|
{
|
2019-03-02 05:47:06 +00:00
|
|
|
FatalError.raise();
|
2013-08-05 20:18:29 +00:00
|
|
|
} else {
|
2019-05-23 20:10:24 +00:00
|
|
|
self.expected_one_of_not_found(edible, inedible)
|
2024-07-04 23:19:15 +00:00
|
|
|
.map(|error_guaranteed| Recovered::Yes(error_guaranteed))
|
2013-08-05 20:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 19:19:57 +00:00
|
|
|
// Public for rustfmt usage.
|
2020-04-19 11:00:18 +00:00
|
|
|
pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
|
2018-01-06 22:43:20 +00:00
|
|
|
self.parse_ident_common(true)
|
|
|
|
}
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
|
2023-03-17 08:35:43 +00:00
|
|
|
let (ident, is_raw) = self.ident_or_err(recover)?;
|
2023-03-17 09:27:17 +00:00
|
|
|
|
2024-02-13 23:28:27 +00:00
|
|
|
if matches!(is_raw, IdentIsRaw::No) && ident.is_reserved() {
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 01:17:35 +00:00
|
|
|
let err = self.expected_ident_found_err();
|
2020-09-16 21:10:05 +00:00
|
|
|
if recover {
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 21:10:05 +00:00
|
|
|
self.bump();
|
|
|
|
Ok(ident)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:28:27 +00:00
|
|
|
fn ident_or_err(&mut self, recover: bool) -> PResult<'a, (Ident, IdentIsRaw)> {
|
2023-12-27 20:16:29 +00:00
|
|
|
match self.token.ident() {
|
|
|
|
Some(ident) => Ok(ident),
|
|
|
|
None => self.expected_ident_found(recover),
|
|
|
|
}
|
2023-03-17 08:35:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks if the next token is `tok`, and returns `true` if so.
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
///
|
2024-12-03 09:09:29 +00:00
|
|
|
/// This method will automatically add `tok` to `expected_token_types` if `tok` is not
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
/// encountered.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-12-04 04:55:06 +00:00
|
|
|
fn check(&mut self, exp: ExpTokenPair<'_>) -> bool {
|
|
|
|
let is_present = self.token == *exp.tok;
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
if !is_present {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expected_token_types.insert(exp.token_type);
|
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code:
let x: [int ..4];
Currently spits out ‘expected `]`, found `..`’. However, a comma would also be
valid there, as would a number of other tokens. This change adjusts the parser
to produce more accurate errors, so that that example now produces ‘expected one
of `(`, `+`, `,`, `::`, or `]`, found `..`’.
2014-12-03 09:47:53 +00:00
|
|
|
}
|
|
|
|
is_present
|
|
|
|
}
|
|
|
|
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2022-05-01 17:05:35 +00:00
|
|
|
fn check_noexpect(&self, tok: &TokenKind) -> bool {
|
|
|
|
self.token == *tok
|
|
|
|
}
|
|
|
|
|
2024-04-17 03:17:44 +00:00
|
|
|
// Check the first token after the delimiter that closes the current
|
|
|
|
// delimited sequence. (Panics if used in the outermost token stream, which
|
|
|
|
// has no delimiters.) It uses a clone of the relevant tree cursor to skip
|
|
|
|
// past the entire `TokenTree::Delimited` in a single step, avoiding the
|
|
|
|
// need for unbounded token lookahead.
|
|
|
|
//
|
|
|
|
// Primarily used when `self.token` matches
|
|
|
|
// `OpenDelim(Delimiter::Invisible(_))`, to look ahead through the current
|
|
|
|
// metavar expansion.
|
|
|
|
fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
|
|
|
|
let mut tree_cursor = self.token_cursor.stack.last().unwrap().clone();
|
|
|
|
tree_cursor.bump();
|
|
|
|
matches!(
|
|
|
|
tree_cursor.curr(),
|
|
|
|
Some(TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:05:35 +00:00
|
|
|
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
|
|
|
|
///
|
|
|
|
/// the main purpose of this function is to reduce the cluttering of the suggestions list
|
|
|
|
/// which using the normal eat method could introduce in some cases.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-06-03 05:47:46 +00:00
|
|
|
fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
|
2022-05-01 17:05:35 +00:00
|
|
|
let is_present = self.check_noexpect(tok);
|
|
|
|
if is_present {
|
|
|
|
self.bump()
|
|
|
|
}
|
|
|
|
is_present
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-12-04 04:55:06 +00:00
|
|
|
pub fn eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
|
|
|
|
let is_present = self.check(exp);
|
2015-12-30 23:11:53 +00:00
|
|
|
if is_present {
|
|
|
|
self.bump()
|
|
|
|
}
|
|
|
|
is_present
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 03:13:42 +00:00
|
|
|
/// If the next token is the given keyword, returns `true` without eating it.
|
|
|
|
/// An expectation is also added for diagnostics purposes.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-12-04 04:55:06 +00:00
|
|
|
fn check_keyword(&mut self, exp: ExpKeywordPair) -> bool {
|
|
|
|
let is_keyword = self.token.is_keyword(exp.kw);
|
2024-12-04 04:36:49 +00:00
|
|
|
if !is_keyword {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expected_token_types.insert(exp.token_type);
|
2024-12-04 04:36:49 +00:00
|
|
|
}
|
|
|
|
is_keyword
|
2015-01-16 03:04:28 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-12-04 04:55:06 +00:00
|
|
|
fn check_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
|
|
|
|
if self.check_keyword(exp) {
|
2024-12-04 04:36:49 +00:00
|
|
|
true
|
|
|
|
} else if case == Case::Insensitive
|
2024-02-13 23:28:27 +00:00
|
|
|
&& let Some((ident, IdentIsRaw::No)) = self.token.ident()
|
2025-01-11 17:11:42 +00:00
|
|
|
// Do an ASCII case-insensitive match, because all keywords are ASCII.
|
2024-12-04 04:55:06 +00:00
|
|
|
&& ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
|
2022-09-13 18:48:29 +00:00
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 03:13:42 +00:00
|
|
|
/// If the next token is the given keyword, eats it and returns `true`.
|
|
|
|
/// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
|
2024-07-25 08:05:31 +00:00
|
|
|
// Public for rustc_builtin_macros and rustfmt usage.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-12-04 04:55:06 +00:00
|
|
|
pub fn eat_keyword(&mut self, exp: ExpKeywordPair) -> bool {
|
|
|
|
let is_keyword = self.check_keyword(exp);
|
2024-12-04 04:36:49 +00:00
|
|
|
if is_keyword {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2015-01-16 03:04:28 +00:00
|
|
|
}
|
2024-12-04 04:36:49 +00:00
|
|
|
is_keyword
|
2015-01-16 03:04:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 19:06:13 +00:00
|
|
|
/// Eats a keyword, optionally ignoring the case.
|
|
|
|
/// If the case differs (and is ignored) an error is issued.
|
|
|
|
/// This is useful for recovery.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-12-04 04:55:06 +00:00
|
|
|
fn eat_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
|
|
|
|
if self.eat_keyword(exp) {
|
2024-12-04 04:36:49 +00:00
|
|
|
true
|
|
|
|
} else if case == Case::Insensitive
|
2024-02-13 23:28:27 +00:00
|
|
|
&& let Some((ident, IdentIsRaw::No)) = self.token.ident()
|
2025-01-11 17:11:42 +00:00
|
|
|
// Do an ASCII case-insensitive match, because all keywords are ASCII.
|
|
|
|
&& ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
|
2022-07-29 19:06:13 +00:00
|
|
|
{
|
2024-12-04 04:55:06 +00:00
|
|
|
self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw: exp.kw.as_str() });
|
2022-07-29 19:06:13 +00:00
|
|
|
self.bump();
|
2024-12-04 04:36:49 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2022-07-29 19:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 08:05:31 +00:00
|
|
|
/// If the next token is the given keyword, eats it and returns `true`.
|
|
|
|
/// Otherwise, returns `false`. No expectation is added.
|
|
|
|
// Public for rustc_builtin_macros usage.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-07-30 01:21:15 +00:00
|
|
|
#[must_use]
|
2024-07-25 08:05:31 +00:00
|
|
|
pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
|
2024-12-04 04:36:49 +00:00
|
|
|
let is_keyword = self.token.is_keyword(kw);
|
|
|
|
if is_keyword {
|
2015-12-30 23:11:53 +00:00
|
|
|
self.bump();
|
2014-08-28 04:34:03 +00:00
|
|
|
}
|
2024-12-04 04:36:49 +00:00
|
|
|
is_keyword
|
2013-06-15 01:21:47 +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.
|
2024-12-04 04:55:06 +00:00
|
|
|
pub fn expect_keyword(&mut self, exp: ExpKeywordPair) -> PResult<'a, ()> {
|
|
|
|
if !self.eat_keyword(exp) { self.unexpected() } else { Ok(()) }
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 02:17:09 +00:00
|
|
|
/// Consume a sequence produced by a metavar expansion, if present.
|
|
|
|
fn eat_metavar_seq<T>(
|
|
|
|
&mut self,
|
|
|
|
mv_kind: MetaVarKind,
|
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> Option<T> {
|
|
|
|
self.eat_metavar_seq_with_matcher(|mvk| mvk == mv_kind, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A slightly more general form of `eat_metavar_seq`, for use with the
|
|
|
|
/// `MetaVarKind` variants that have parameters, where an exact match isn't
|
|
|
|
/// desired.
|
|
|
|
fn eat_metavar_seq_with_matcher<T>(
|
|
|
|
&mut self,
|
|
|
|
match_mv_kind: impl Fn(MetaVarKind) -> bool,
|
|
|
|
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> Option<T> {
|
|
|
|
if let token::OpenDelim(delim) = self.token.kind
|
|
|
|
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)) = delim
|
|
|
|
&& match_mv_kind(mv_kind)
|
|
|
|
{
|
|
|
|
self.bump();
|
|
|
|
let res = f(self).expect("failed to reparse {mv_kind:?}");
|
|
|
|
if let token::CloseDelim(delim) = self.token.kind
|
|
|
|
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)) = delim
|
|
|
|
&& match_mv_kind(mv_kind)
|
|
|
|
{
|
|
|
|
self.bump();
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
panic!("no close delim when reparsing {mv_kind:?}");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 05:18:10 +00:00
|
|
|
/// Is the given keyword `kw` followed by a non-reserved identifier?
|
|
|
|
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
|
|
|
|
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
|
|
|
|
}
|
|
|
|
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2024-12-04 04:55:06 +00:00
|
|
|
fn check_or_expected(&mut self, ok: bool, token_type: TokenType) -> bool {
|
2024-12-04 04:36:49 +00:00
|
|
|
if !ok {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expected_token_types.insert(token_type);
|
2017-01-18 16:01:04 +00:00
|
|
|
}
|
2024-12-04 04:36:49 +00:00
|
|
|
ok
|
2017-01-18 16:01:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 07:35:34 +00:00
|
|
|
fn check_ident(&mut self) -> bool {
|
2019-10-01 03:55:28 +00:00
|
|
|
self.check_or_expected(self.token.is_ident(), TokenType::Ident)
|
2019-09-30 04:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:01:04 +00:00
|
|
|
fn check_path(&mut self) -> bool {
|
2019-10-01 03:55:28 +00:00
|
|
|
self.check_or_expected(self.token.is_path_start(), TokenType::Path)
|
2017-01-18 16:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_type(&mut self) -> bool {
|
2019-10-01 03:55:28 +00:00
|
|
|
self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
|
2017-01-18 16:01:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:49:38 +00:00
|
|
|
fn check_const_arg(&mut self) -> bool {
|
2019-10-01 03:55:28 +00:00
|
|
|
self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
|
2019-09-30 04:21:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:15:55 +00:00
|
|
|
fn check_const_closure(&self) -> bool {
|
|
|
|
self.is_keyword_ahead(0, &[kw::Const])
|
|
|
|
&& self.look_ahead(1, |t| match &t.kind {
|
2023-02-01 05:55:48 +00:00
|
|
|
// async closures do not work with const closures, so we do not parse that here.
|
2025-02-16 15:20:53 +00:00
|
|
|
token::Ident(kw::Move | kw::Static, IdentIsRaw::No)
|
|
|
|
| token::OrOr
|
|
|
|
| token::BinOp(token::Or) => true,
|
2022-12-20 16:15:55 +00:00
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:44:37 +00:00
|
|
|
fn check_inline_const(&self, dist: usize) -> bool {
|
|
|
|
self.is_keyword_ahead(dist, &[kw::Const])
|
2022-11-22 09:42:01 +00:00
|
|
|
&& self.look_ahead(dist + 1, |t| match &t.kind {
|
2024-04-22 06:29:27 +00:00
|
|
|
token::Interpolated(nt) => matches!(&**nt, token::NtBlock(..)),
|
2022-04-26 12:40:14 +00:00
|
|
|
token::OpenDelim(Delimiter::Brace) => true,
|
2020-10-19 19:57:04 +00:00
|
|
|
_ => false,
|
|
|
|
})
|
2020-09-21 20:55:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 04:21:30 +00:00
|
|
|
/// Checks to see if the next token is either `+` or `+=`.
|
|
|
|
/// Otherwise returns `false`.
|
2024-03-19 05:33:53 +00:00
|
|
|
#[inline]
|
2019-09-30 04:21:30 +00:00
|
|
|
fn check_plus(&mut self) -> bool {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.check_or_expected(self.token.is_like_plus(), TokenType::Plus)
|
2019-02-05 15:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats the expected token if it's present possibly breaking
|
|
|
|
/// compound tokens like multi-character operators in process.
|
|
|
|
/// Returns `true` if the token was eaten.
|
2024-12-04 04:55:06 +00:00
|
|
|
fn break_and_eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
|
|
|
|
if self.token == *exp.tok {
|
2020-02-22 13:22:38 +00:00
|
|
|
self.bump();
|
|
|
|
return true;
|
|
|
|
}
|
2024-09-19 09:32:17 +00:00
|
|
|
match self.token.kind.break_two_token_op(1) {
|
2024-12-04 04:55:06 +00:00
|
|
|
Some((first, second)) if first == *exp.tok => {
|
2024-03-04 05:31:49 +00:00
|
|
|
let first_span = self.psess.source_map().start_point(self.token.span);
|
2020-02-22 13:22:38 +00:00
|
|
|
let second_span = self.token.span.with_lo(first_span.hi());
|
2020-03-07 13:34:29 +00:00
|
|
|
self.token = Token::new(first, first_span);
|
2020-12-12 20:20:22 +00:00
|
|
|
// Keep track of this token - if we end token capturing now,
|
|
|
|
// we'll want to append this token to the captured stream.
|
|
|
|
//
|
|
|
|
// If we consume any additional tokens, then this token
|
|
|
|
// is not needed (we'll capture the entire 'glued' token),
|
2024-09-19 09:32:17 +00:00
|
|
|
// and `bump` will set this field to 0.
|
|
|
|
self.break_last_token += 1;
|
2023-08-08 01:43:44 +00:00
|
|
|
// Use the spacing of the glued token as the spacing of the
|
|
|
|
// unglued second token.
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
self.bump_with((Token::new(second, second_span), self.token_spacing));
|
2018-05-25 20:40:16 +00:00
|
|
|
true
|
|
|
|
}
|
2020-02-22 13:22:38 +00:00
|
|
|
_ => {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expected_token_types.insert(exp.token_type);
|
2020-02-22 13:22:38 +00:00
|
|
|
false
|
2018-05-25 20:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 21:36:23 +00:00
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats `+` possibly breaking tokens like `+=` in process.
|
|
|
|
fn eat_plus(&mut self) -> bool {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.break_and_eat(exp!(Plus))
|
2020-02-22 13:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Eats `&` possibly breaking tokens like `&&` in process.
|
|
|
|
/// Signals an error if `&` is not eaten.
|
2015-12-20 21:00:43 +00:00
|
|
|
fn expect_and(&mut self) -> PResult<'a, ()> {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.break_and_eat(exp!(And)) { Ok(()) } else { self.unexpected() }
|
2014-04-17 08:35:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats `|` possibly breaking tokens like `||` in process.
|
|
|
|
/// Signals an error if `|` was not eaten.
|
2017-09-07 06:07:49 +00:00
|
|
|
fn expect_or(&mut self) -> PResult<'a, ()> {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.break_and_eat(exp!(Or)) { Ok(()) } else { self.unexpected() }
|
2017-09-07 06:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats `<` possibly breaking tokens like `<<` in process.
|
2015-12-30 23:11:53 +00:00
|
|
|
fn eat_lt(&mut self) -> bool {
|
2024-12-04 04:55:06 +00:00
|
|
|
let ate = self.break_and_eat(exp!(Lt));
|
2019-01-23 01:35:13 +00:00
|
|
|
if ate {
|
|
|
|
// See doc comment for `unmatched_angle_bracket_count`.
|
|
|
|
self.unmatched_angle_bracket_count += 1;
|
|
|
|
debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
2019-01-23 01:35:13 +00:00
|
|
|
ate
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats `<` possibly breaking tokens like `<<` in process.
|
|
|
|
/// Signals an error if `<` was not eaten.
|
2015-12-20 21:00:43 +00:00
|
|
|
fn expect_lt(&mut self) -> PResult<'a, ()> {
|
2020-02-22 13:22:38 +00:00
|
|
|
if self.eat_lt() { Ok(()) } else { self.unexpected() }
|
2014-05-11 04:27:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Eats `>` possibly breaking tokens like `>>` in process.
|
|
|
|
/// Signals an error if `>` was not eaten.
|
2018-05-31 22:53:30 +00:00
|
|
|
fn expect_gt(&mut self) -> PResult<'a, ()> {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.break_and_eat(exp!(Gt)) {
|
2020-02-22 13:22:38 +00:00
|
|
|
// 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);
|
2019-01-23 01:35:13 +00:00
|
|
|
}
|
2020-02-22 13:22:38 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
self.unexpected()
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 04:50:46 +00:00
|
|
|
/// Checks if the next token is contained within `closes`, and returns `true` if so.
|
2024-06-05 21:02:18 +00:00
|
|
|
fn expect_any_with_type(
|
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
closes_expected: &[ExpTokenPair<'_>],
|
2024-12-04 04:50:46 +00:00
|
|
|
closes_not_expected: &[&TokenKind],
|
2024-06-05 21:02:18 +00:00
|
|
|
) -> bool {
|
2024-12-04 04:55:06 +00:00
|
|
|
closes_expected.iter().any(|&close| self.check(close))
|
2024-12-04 04:50:46 +00:00
|
|
|
|| closes_not_expected.iter().any(|k| self.check_noexpect(k))
|
2019-07-09 08:27:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a sequence until the specified delimiters. The function
|
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
|
|
|
/// closing bracket.
|
2019-10-08 07:35:34 +00:00
|
|
|
fn parse_seq_to_before_tokens<T>(
|
2018-08-20 23:16:17 +00:00
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
closes_expected: &[ExpTokenPair<'_>],
|
2024-12-04 04:50:46 +00:00
|
|
|
closes_not_expected: &[&TokenKind],
|
2024-12-04 04:55:06 +00:00
|
|
|
sep: SeqSep<'_>,
|
2019-07-09 08:27:07 +00:00
|
|
|
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
|
2019-01-28 05:04:50 +00:00
|
|
|
let mut first = true;
|
2024-02-13 23:44:33 +00:00
|
|
|
let mut recovered = Recovered::No;
|
2024-02-13 23:48:23 +00:00
|
|
|
let mut trailing = Trailing::No;
|
2022-11-23 00:55:16 +00:00
|
|
|
let mut v = ThinVec::new();
|
2021-04-09 02:12:00 +00:00
|
|
|
|
2024-12-04 04:50:46 +00:00
|
|
|
while !self.expect_any_with_type(closes_expected, closes_not_expected) {
|
2019-07-09 08:27:07 +00:00
|
|
|
if let token::CloseDelim(..) | token::Eof = self.token.kind {
|
|
|
|
break;
|
|
|
|
}
|
2024-12-04 04:55:06 +00:00
|
|
|
if let Some(exp) = sep.sep {
|
2017-05-12 18:05:39 +00:00
|
|
|
if first {
|
2023-12-28 13:06:51 +00:00
|
|
|
// no separator for the first element
|
2017-05-12 18:05:39 +00:00
|
|
|
first = false;
|
|
|
|
} else {
|
2023-12-28 13:06:51 +00:00
|
|
|
// check for separator
|
2024-12-04 04:55:06 +00:00
|
|
|
match self.expect(exp) {
|
2024-02-13 23:44:33 +00:00
|
|
|
Ok(Recovered::No) => {
|
2021-08-16 13:22:36 +00:00
|
|
|
self.current_closure.take();
|
|
|
|
}
|
2024-05-09 08:44:40 +00:00
|
|
|
Ok(Recovered::Yes(guar)) => {
|
2021-08-16 13:22:36 +00:00
|
|
|
self.current_closure.take();
|
2024-05-09 08:44:40 +00:00
|
|
|
recovered = Recovered::Yes(guar);
|
2019-01-28 05:04:50 +00:00
|
|
|
break;
|
2017-10-24 13:04:01 +00:00
|
|
|
}
|
2019-11-24 21:33:00 +00:00
|
|
|
Err(mut expect_err) => {
|
2020-02-29 11:56:15 +00:00
|
|
|
let sp = self.prev_token.span.shrink_to_hi();
|
2024-12-04 04:55:06 +00:00
|
|
|
let token_str = pprust::token_kind_to_string(exp.tok);
|
2019-11-24 21:33:00 +00:00
|
|
|
|
2021-08-16 13:22:36 +00:00
|
|
|
match self.current_closure.take() {
|
2024-08-09 07:44:47 +00:00
|
|
|
Some(closure_spans) if self.token == TokenKind::Semi => {
|
2021-08-16 13:22:36 +00:00
|
|
|
// Finding a semicolon instead of a comma
|
|
|
|
// after a closure body indicates that the
|
|
|
|
// closure body may be a block but the user
|
|
|
|
// forgot to put braces around its
|
|
|
|
// statements.
|
|
|
|
|
|
|
|
self.recover_missing_braces_around_closure_body(
|
|
|
|
closure_spans,
|
|
|
|
expect_err,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
// Attempt to keep parsing if it was a similar separator.
|
2025-01-22 15:01:10 +00:00
|
|
|
if exp.tok.similar_tokens().contains(&self.token.kind) {
|
|
|
|
self.bump();
|
2021-08-16 13:22:36 +00:00
|
|
|
}
|
2019-01-28 05:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 21:33:00 +00:00
|
|
|
|
2020-05-27 18:09:54 +00:00
|
|
|
// If this was a missing `@` in a binding pattern
|
|
|
|
// bail with a suggestion
|
|
|
|
// https://github.com/rust-lang/rust/issues/72373
|
2024-08-09 07:44:47 +00:00
|
|
|
if self.prev_token.is_ident() && self.token == token::DotDot {
|
2020-05-27 18:09:54 +00:00
|
|
|
let msg = format!(
|
2023-11-06 19:56:45 +00:00
|
|
|
"if you meant to bind the contents of the rest of the array \
|
|
|
|
pattern into `{}`, use `@`",
|
2020-05-27 18:09:54 +00:00
|
|
|
pprust::token_to_string(&self.prev_token)
|
|
|
|
);
|
|
|
|
expect_err
|
2024-01-08 22:08:49 +00:00
|
|
|
.with_span_suggestion_verbose(
|
2020-05-27 18:09:54 +00:00
|
|
|
self.prev_token.span.shrink_to_hi().until(self.token.span),
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
msg,
|
2022-04-26 05:17:33 +00:00
|
|
|
" @ ",
|
2020-05-27 18:09:54 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-06 02:56:45 +00:00
|
|
|
// Attempt to keep parsing if it was an omitted separator.
|
2024-04-19 17:12:12 +00:00
|
|
|
self.last_unexpected_token_span = None;
|
2019-01-28 05:04:50 +00:00
|
|
|
match f(self) {
|
|
|
|
Ok(t) => {
|
2019-11-24 21:33:00 +00:00
|
|
|
// Parsed successfully, therefore most probably the code only
|
|
|
|
// misses a separator.
|
|
|
|
expect_err
|
2024-01-08 22:08:49 +00:00
|
|
|
.with_span_suggestion_short(
|
2020-12-19 10:29:56 +00:00
|
|
|
sp,
|
2023-07-25 20:00:13 +00:00
|
|
|
format!("missing `{token_str}`"),
|
2022-04-26 05:17:33 +00:00
|
|
|
token_str,
|
2019-11-24 21:33:00 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
|
2019-01-28 05:04:50 +00:00
|
|
|
v.push(t);
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-26 03:39:14 +00:00
|
|
|
Err(e) => {
|
2019-11-24 21:33:00 +00:00
|
|
|
// Parsing failed, therefore it must be something more serious
|
|
|
|
// than just a missing separator.
|
2022-11-30 20:01:00 +00:00
|
|
|
for xx in &e.children {
|
2024-12-04 04:55:06 +00:00
|
|
|
// Propagate the help message from sub error `e` to main
|
|
|
|
// error `expect_err`.
|
2022-11-30 20:01:00 +00:00
|
|
|
expect_err.children.push(xx.clone());
|
|
|
|
}
|
2019-01-28 05:04:50 +00:00
|
|
|
e.cancel();
|
2022-11-16 20:46:06 +00:00
|
|
|
if self.token == token::Colon {
|
2024-12-04 04:55:06 +00:00
|
|
|
// We will try to recover in
|
|
|
|
// `maybe_recover_struct_lit_bad_delims`.
|
2022-11-16 20:46:06 +00:00
|
|
|
return Err(expect_err);
|
2024-12-04 04:55:06 +00:00
|
|
|
} else if let [exp] = closes_expected
|
|
|
|
&& exp.token_type == TokenType::CloseParen
|
2023-11-06 23:19:14 +00:00
|
|
|
{
|
|
|
|
return Err(expect_err);
|
2022-11-16 20:46:06 +00:00
|
|
|
} else {
|
|
|
|
expect_err.emit();
|
|
|
|
break;
|
|
|
|
}
|
2019-01-28 05:04:50 +00:00
|
|
|
}
|
2017-10-24 13:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2024-06-05 21:02:18 +00:00
|
|
|
if sep.trailing_sep_allowed
|
2024-12-04 04:50:46 +00:00
|
|
|
&& self.expect_any_with_type(closes_expected, closes_not_expected)
|
2024-06-05 21:02:18 +00:00
|
|
|
{
|
2024-02-13 23:48:23 +00:00
|
|
|
trailing = Trailing::Yes;
|
2016-01-31 19:39:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-29 04:49:59 +00:00
|
|
|
|
2017-10-22 16:19:30 +00:00
|
|
|
let t = f(self)?;
|
|
|
|
v.push(t);
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2023-03-14 23:10:59 +00:00
|
|
|
|
2019-07-09 08:27:07 +00:00
|
|
|
Ok((v, trailing, recovered))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:22:36 +00:00
|
|
|
fn recover_missing_braces_around_closure_body(
|
|
|
|
&mut self,
|
|
|
|
closure_spans: ClosureSpans,
|
2024-02-22 23:20:45 +00:00
|
|
|
mut expect_err: Diag<'_>,
|
2021-08-16 13:22:36 +00:00
|
|
|
) -> PResult<'a, ()> {
|
|
|
|
let initial_semicolon = self.token.span;
|
|
|
|
|
2024-12-04 04:55:06 +00:00
|
|
|
while self.eat(exp!(Semi)) {
|
2024-07-16 16:21:39 +00:00
|
|
|
let _ = self.parse_stmt_without_recovery(false, ForceCollect::No).unwrap_or_else(|e| {
|
|
|
|
e.cancel();
|
|
|
|
None
|
|
|
|
});
|
2021-08-16 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-23 22:08:41 +00:00
|
|
|
expect_err
|
|
|
|
.primary_message("closure bodies that contain statements must be surrounded by braces");
|
2021-08-16 13:22:36 +00:00
|
|
|
|
|
|
|
let preceding_pipe_span = closure_spans.closing_pipe;
|
|
|
|
let following_token_span = self.token.span;
|
|
|
|
|
|
|
|
let mut first_note = MultiSpan::from(vec![initial_semicolon]);
|
|
|
|
first_note.push_span_label(
|
|
|
|
initial_semicolon,
|
2022-06-29 12:16:43 +00:00
|
|
|
"this `;` turns the preceding closure into a statement",
|
2021-08-16 13:22:36 +00:00
|
|
|
);
|
|
|
|
first_note.push_span_label(
|
|
|
|
closure_spans.body,
|
2022-06-29 12:16:43 +00:00
|
|
|
"this expression is a statement because of the trailing semicolon",
|
2021-08-16 13:22:36 +00:00
|
|
|
);
|
|
|
|
expect_err.span_note(first_note, "statement found outside of a block");
|
|
|
|
|
|
|
|
let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
|
2022-06-29 12:16:43 +00:00
|
|
|
second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
|
2021-08-16 13:22:36 +00:00
|
|
|
second_note.push_span_label(
|
|
|
|
following_token_span,
|
2022-06-29 12:16:43 +00:00
|
|
|
"...but likely you meant the closure to end here",
|
2021-08-16 13:22:36 +00:00
|
|
|
);
|
|
|
|
expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
|
|
|
|
|
2023-12-23 22:08:41 +00:00
|
|
|
expect_err.span(vec![preceding_pipe_span, following_token_span]);
|
2021-08-16 13:22:36 +00:00
|
|
|
|
|
|
|
let opening_suggestion_str = " {".to_string();
|
|
|
|
let closing_suggestion_str = "}".to_string();
|
|
|
|
|
|
|
|
expect_err.multipart_suggestion(
|
|
|
|
"try adding braces",
|
|
|
|
vec![
|
|
|
|
(preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
|
|
|
|
(following_token_span.shrink_to_lo(), closing_suggestion_str),
|
|
|
|
],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect_err.emit();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a sequence, not including the delimiters. The function
|
2019-12-04 09:13:29 +00:00
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
|
|
|
/// closing bracket.
|
|
|
|
fn parse_seq_to_before_end<T>(
|
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
close: ExpTokenPair<'_>,
|
|
|
|
sep: SeqSep<'_>,
|
2019-12-04 09:13:29 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
|
2024-12-04 04:50:46 +00:00
|
|
|
self.parse_seq_to_before_tokens(&[close], &[], sep, f)
|
2019-12-04 09:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a sequence, including only the closing delimiter. The function
|
2019-12-04 09:13:29 +00:00
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
|
|
|
/// closing bracket.
|
|
|
|
fn parse_seq_to_end<T>(
|
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
close: ExpTokenPair<'_>,
|
|
|
|
sep: SeqSep<'_>,
|
2019-12-04 09:13:29 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing)> {
|
2024-12-04 04:50:46 +00:00
|
|
|
let (val, trailing, recovered) = self.parse_seq_to_before_end(close, sep, f)?;
|
|
|
|
if matches!(recovered, Recovered::No) && !self.eat(close) {
|
2024-07-30 01:21:15 +00:00
|
|
|
self.dcx().span_delayed_bug(
|
|
|
|
self.token.span,
|
2024-12-04 04:50:46 +00:00
|
|
|
"recovered but `parse_seq_to_before_end` did not give us the close token",
|
2024-07-30 01:21:15 +00:00
|
|
|
);
|
2019-12-04 09:13:29 +00:00
|
|
|
}
|
|
|
|
Ok((val, trailing))
|
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a sequence, including both delimiters. The function
|
2019-02-08 13:53:55 +00:00
|
|
|
/// `f` must consume tokens until reaching the next separator or
|
2014-06-09 20:12:30 +00:00
|
|
|
/// closing bracket.
|
2019-07-09 08:27:07 +00:00
|
|
|
fn parse_unspanned_seq<T>(
|
2019-01-28 05:04:50 +00:00
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
open: ExpTokenPair<'_>,
|
|
|
|
close: ExpTokenPair<'_>,
|
|
|
|
sep: SeqSep<'_>,
|
2019-07-09 08:27:07 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing)> {
|
2024-12-04 04:50:46 +00:00
|
|
|
self.expect(open)?;
|
|
|
|
self.parse_seq_to_end(close, sep, f)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a comma-separated sequence, including both delimiters.
|
|
|
|
/// The function `f` must consume tokens until reaching the next separator or
|
|
|
|
/// closing bracket.
|
2019-07-09 08:31:24 +00:00
|
|
|
fn parse_delim_comma_seq<T>(
|
|
|
|
&mut self,
|
2024-12-04 04:55:06 +00:00
|
|
|
open: ExpTokenPair<'_>,
|
|
|
|
close: ExpTokenPair<'_>,
|
2019-07-09 08:31:24 +00:00
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing)> {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.parse_unspanned_seq(open, close, SeqSep::trailing_allowed(exp!(Comma)), f)
|
2019-07-09 08:31:24 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 13:06:51 +00:00
|
|
|
/// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
|
|
|
|
/// The function `f` must consume tokens until reaching the next separator or
|
|
|
|
/// closing bracket.
|
2019-07-09 08:31:24 +00:00
|
|
|
fn parse_paren_comma_seq<T>(
|
|
|
|
&mut self,
|
|
|
|
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
2024-02-13 23:48:23 +00:00
|
|
|
) -> PResult<'a, (ThinVec<T>, Trailing)> {
|
2024-12-04 04:55:06 +00:00
|
|
|
self.parse_delim_comma_seq(exp!(OpenParen), exp!(CloseParen), f)
|
2019-07-09 08:31:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Advance the parser by one token using provided token as the next one.
|
2022-03-07 04:17:38 +00:00
|
|
|
fn bump_with(&mut self, next: (Token, Spacing)) {
|
|
|
|
self.inlined_bump_with(next)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This always-inlined version should only be used on hot code paths.
|
|
|
|
#[inline(always)]
|
|
|
|
fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
|
2020-02-09 14:54:38 +00:00
|
|
|
// Update the current and previous tokens.
|
2020-03-07 13:34:29 +00:00
|
|
|
self.prev_token = mem::replace(&mut self.token, next_token);
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
self.token_spacing = next_spacing;
|
2016-09-16 05:46:40 +00:00
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
// Diagnostics.
|
2024-12-03 09:09:29 +00:00
|
|
|
self.expected_token_types.clear();
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2013-07-02 19:47:32 +00:00
|
|
|
|
2020-02-22 13:22:38 +00:00
|
|
|
/// Advance the parser by one token.
|
|
|
|
pub fn bump(&mut self) {
|
2022-04-20 06:34:33 +00:00
|
|
|
// Note: destructuring here would give nicer code, but it was found in #96210 to be slower
|
|
|
|
// than `.0`/`.1` access.
|
2023-07-25 23:17:32 +00:00
|
|
|
let mut next = self.token_cursor.inlined_next();
|
2023-07-31 06:15:54 +00:00
|
|
|
self.num_bump_calls += 1;
|
2024-09-19 09:32:17 +00:00
|
|
|
// We got a token from the underlying cursor and no longer need to
|
|
|
|
// worry about an unglued token. See `break_and_eat` for more details.
|
|
|
|
self.break_last_token = 0;
|
2022-04-20 06:34:33 +00:00
|
|
|
if next.0.span.is_dummy() {
|
2022-04-20 02:22:42 +00:00
|
|
|
// Tweak the location for better diagnostics, but keep syntactic context intact.
|
2022-04-20 04:04:22 +00:00
|
|
|
let fallback_span = self.token.span;
|
2022-04-20 06:34:33 +00:00
|
|
|
next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
|
2022-04-14 01:13:20 +00:00
|
|
|
}
|
2022-04-20 02:22:42 +00:00
|
|
|
debug_assert!(!matches!(
|
2022-04-20 06:34:33 +00:00
|
|
|
next.0.kind,
|
2024-04-16 23:59:27 +00:00
|
|
|
token::OpenDelim(delim) | token::CloseDelim(delim) if delim.skip()
|
2022-04-20 02:22:42 +00:00
|
|
|
));
|
2022-04-20 06:34:33 +00:00
|
|
|
self.inlined_bump_with(next)
|
2012-01-13 08:56:53 +00:00
|
|
|
}
|
2016-02-06 17:42:17 +00:00
|
|
|
|
2019-10-01 03:13:42 +00:00
|
|
|
/// Look-ahead `dist` tokens of `self.token` and get access to that token there.
|
2023-08-09 05:02:30 +00:00
|
|
|
/// When `dist == 0` then the current token is looked at. `Eof` will be
|
|
|
|
/// returned if the look-ahead is any distance past the end of the tokens.
|
2019-09-30 04:21:30 +00:00
|
|
|
pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
|
2019-05-23 20:10:24 +00:00
|
|
|
if dist == 0 {
|
2019-09-30 04:21:30 +00:00
|
|
|
return looker(&self.token);
|
2019-05-23 20:10:24 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 03:20:24 +00:00
|
|
|
// Typically around 98% of the `dist > 0` cases have `dist == 1`, so we
|
|
|
|
// have a fast special case for that.
|
|
|
|
if dist == 1 {
|
|
|
|
// The index is zero because the tree cursor's index always points
|
|
|
|
// to the next token to be gotten.
|
2024-12-10 08:18:44 +00:00
|
|
|
match self.token_cursor.curr.curr() {
|
2024-07-12 03:20:24 +00:00
|
|
|
Some(tree) => {
|
|
|
|
// Indexing stayed within the current token tree.
|
2024-08-12 00:27:19 +00:00
|
|
|
match tree {
|
|
|
|
TokenTree::Token(token, _) => return looker(token),
|
|
|
|
&TokenTree::Delimited(dspan, _, delim, _) => {
|
2024-04-16 23:59:27 +00:00
|
|
|
if !delim.skip() {
|
2024-08-12 00:27:19 +00:00
|
|
|
return looker(&Token::new(token::OpenDelim(delim), dspan.open));
|
|
|
|
}
|
2024-07-12 03:20:24 +00:00
|
|
|
}
|
2024-12-10 08:18:44 +00:00
|
|
|
}
|
2024-07-12 03:20:24 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// The tree cursor lookahead went (one) past the end of the
|
|
|
|
// current token tree. Try to return a close delimiter.
|
2024-12-10 08:18:44 +00:00
|
|
|
if let Some(last) = self.token_cursor.stack.last()
|
|
|
|
&& let Some(&TokenTree::Delimited(span, _, delim, _)) = last.curr()
|
2024-04-16 23:59:27 +00:00
|
|
|
&& !delim.skip()
|
2024-07-12 03:20:24 +00:00
|
|
|
{
|
|
|
|
// We are not in the outermost token stream, so we have
|
|
|
|
// delimiters. Also, those delimiters are not skipped.
|
|
|
|
return looker(&Token::new(token::CloseDelim(delim), span.close));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 02:56:58 +00:00
|
|
|
// Just clone the token cursor and use `next`, skipping delimiters as
|
|
|
|
// necessary. Slow but simple.
|
2021-04-12 15:15:38 +00:00
|
|
|
let mut cursor = self.token_cursor.clone();
|
|
|
|
let mut i = 0;
|
|
|
|
let mut token = Token::dummy();
|
|
|
|
while i < dist {
|
2023-07-25 23:17:32 +00:00
|
|
|
token = cursor.next().0;
|
2021-04-12 15:15:38 +00:00
|
|
|
if matches!(
|
|
|
|
token.kind,
|
2024-04-16 23:59:27 +00:00
|
|
|
token::OpenDelim(delim) | token::CloseDelim(delim) if delim.skip()
|
2021-04-12 15:15:38 +00:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i += 1;
|
2020-09-14 05:45:10 +00:00
|
|
|
}
|
2023-07-31 01:07:50 +00:00
|
|
|
looker(&token)
|
2019-05-23 20:10:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:58:44 +00:00
|
|
|
/// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
|
2023-11-12 22:46:01 +00:00
|
|
|
pub(crate) fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
|
2019-05-29 15:58:44 +00:00
|
|
|
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.
|
2023-12-05 21:45:01 +00:00
|
|
|
fn parse_coroutine_kind(&mut self, case: Case) -> Option<CoroutineKind> {
|
|
|
|
let span = self.token.uninterpolated_span();
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword_case(exp!(Async), case) {
|
2023-12-05 21:45:01 +00:00
|
|
|
// FIXME(gen_blocks): Do we want to unconditionally parse `gen` and then
|
|
|
|
// error if edition <= 2024, like we do with async and edition <= 2018?
|
|
|
|
if self.token.uninterpolated_span().at_least_rust_2024()
|
2024-12-04 04:55:06 +00:00
|
|
|
&& self.eat_keyword_case(exp!(Gen), case)
|
2023-12-05 21:45:01 +00:00
|
|
|
{
|
|
|
|
let gen_span = self.prev_token.uninterpolated_span();
|
|
|
|
Some(CoroutineKind::AsyncGen {
|
|
|
|
span: span.to(gen_span),
|
|
|
|
closure_id: DUMMY_NODE_ID,
|
|
|
|
return_impl_trait_id: DUMMY_NODE_ID,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Some(CoroutineKind::Async {
|
|
|
|
span,
|
|
|
|
closure_id: DUMMY_NODE_ID,
|
|
|
|
return_impl_trait_id: DUMMY_NODE_ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else if self.token.uninterpolated_span().at_least_rust_2024()
|
2024-12-04 04:55:06 +00:00
|
|
|
&& self.eat_keyword_case(exp!(Gen), case)
|
2023-12-05 21:45:01 +00:00
|
|
|
{
|
2023-12-01 00:39:56 +00:00
|
|
|
Some(CoroutineKind::Gen {
|
2023-11-30 22:54:39 +00:00
|
|
|
span,
|
|
|
|
closure_id: DUMMY_NODE_ID,
|
|
|
|
return_impl_trait_id: DUMMY_NODE_ID,
|
2023-12-01 00:39:56 +00:00
|
|
|
})
|
2023-10-05 11:30:55 +00:00
|
|
|
} else {
|
2023-12-01 00:39:56 +00:00
|
|
|
None
|
2023-10-05 11:30:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:17:48 +00:00
|
|
|
/// Parses fn unsafety: `unsafe`, `safe` or nothing.
|
|
|
|
fn parse_safety(&mut self, case: Case) -> Safety {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword_case(exp!(Unsafe), case) {
|
2024-05-17 17:17:48 +00:00
|
|
|
Safety::Unsafe(self.prev_token.uninterpolated_span())
|
2024-12-04 04:55:06 +00:00
|
|
|
} else if self.eat_keyword_case(exp!(Safe), case) {
|
2024-05-23 13:01:05 +00:00
|
|
|
Safety::Safe(self.prev_token.uninterpolated_span())
|
2020-02-29 11:59:37 +00:00
|
|
|
} else {
|
2024-05-17 17:17:48 +00:00
|
|
|
Safety::Default
|
2020-02-29 11:59:37 +00:00
|
|
|
}
|
2020-01-30 01:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses constness: `const` or nothing.
|
2022-09-15 16:27:23 +00:00
|
|
|
fn parse_constness(&mut self, case: Case) -> Const {
|
2023-02-01 05:55:48 +00:00
|
|
|
self.parse_constness_(case, false)
|
|
|
|
}
|
|
|
|
|
2023-03-11 21:29:15 +00:00
|
|
|
/// Parses constness for closures (case sensitive, feature-gated)
|
|
|
|
fn parse_closure_constness(&mut self) -> Const {
|
|
|
|
let constness = self.parse_constness_(Case::Sensitive, true);
|
|
|
|
if let Const::Yes(span) = constness {
|
2024-03-04 05:31:49 +00:00
|
|
|
self.psess.gated_spans.gate(sym::const_closures, span);
|
2023-03-11 21:29:15 +00:00
|
|
|
}
|
|
|
|
constness
|
2023-02-01 05:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
|
|
|
|
// Avoid const blocks and const closures to be parsed as const items
|
|
|
|
if (self.check_const_closure() == is_closure)
|
2023-07-17 19:02:06 +00:00
|
|
|
&& !self
|
|
|
|
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
|
2024-12-04 04:55:06 +00:00
|
|
|
&& self.eat_keyword_case(exp!(Const), case)
|
2020-09-21 20:55:58 +00:00
|
|
|
{
|
2020-03-04 21:34:57 +00:00
|
|
|
Const::Yes(self.prev_token.uninterpolated_span())
|
2020-02-29 11:59:37 +00:00
|
|
|
} else {
|
|
|
|
Const::No
|
|
|
|
}
|
2013-02-01 01:12:29 +00:00
|
|
|
}
|
2012-11-05 04:41:00 +00:00
|
|
|
|
2020-09-21 20:55:58 +00:00
|
|
|
/// Parses inline const expressions.
|
2021-11-22 16:25:28 +00:00
|
|
|
fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
|
|
|
|
if pat {
|
2024-03-04 05:31:49 +00:00
|
|
|
self.psess.gated_spans.gate(sym::inline_const_pat, span);
|
2021-11-22 16:25:28 +00:00
|
|
|
}
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expect_keyword(exp!(Const))?;
|
2022-03-16 00:20:21 +00:00
|
|
|
let (attrs, blk) = self.parse_inner_attrs_and_block()?;
|
2024-06-03 09:11:58 +00:00
|
|
|
let anon_const = AnonConst {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
|
|
|
|
};
|
|
|
|
let blk_span = anon_const.value.span;
|
|
|
|
Ok(self.mk_expr_with_attrs(span.to(blk_span), ExprKind::ConstBlock(anon_const), attrs))
|
2020-09-21 20:55:58 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword(exp!(Mut)) { Mutability::Mut } else { Mutability::Not }
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2012-04-24 22:52:52 +00:00
|
|
|
|
2024-03-24 01:04:45 +00:00
|
|
|
/// Parses reference binding mode (`ref`, `ref mut`, or nothing).
|
|
|
|
fn parse_byref(&mut self) -> ByRef {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword(exp!(Ref)) { ByRef::Yes(self.parse_mutability()) } else { ByRef::No }
|
2024-03-24 01:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 00:36:08 +00:00
|
|
|
/// Possibly parses mutability (`const` or `mut`).
|
|
|
|
fn parse_const_or_mut(&mut self) -> Option<Mutability> {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword(exp!(Mut)) {
|
2019-12-16 16:28:40 +00:00
|
|
|
Some(Mutability::Mut)
|
2024-12-04 04:55:06 +00:00
|
|
|
} else if self.eat_keyword(exp!(Const)) {
|
2019-12-16 16:28:40 +00:00
|
|
|
Some(Mutability::Not)
|
2019-09-30 00:36:08 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 22:53:30 +00:00
|
|
|
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
|
|
|
|
{
|
2022-09-15 08:12:09 +00:00
|
|
|
if let Some(suffix) = suffix {
|
|
|
|
self.expect_no_tuple_index_suffix(self.token.span, suffix);
|
|
|
|
}
|
2016-07-29 20:47:55 +00:00
|
|
|
self.bump();
|
2020-02-29 11:56:15 +00:00
|
|
|
Ok(Ident::new(symbol, self.prev_token.span))
|
2016-07-29 20:47:55 +00:00
|
|
|
} else {
|
2021-01-21 02:49:11 +00:00
|
|
|
self.parse_ident_common(true)
|
2016-07-29 20:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:24:21 +00:00
|
|
|
fn parse_delim_args(&mut self) -> PResult<'a, P<DelimArgs>> {
|
2024-03-15 11:36:21 +00:00
|
|
|
if let Some(args) = self.parse_delim_args_inner() {
|
|
|
|
Ok(P(args))
|
|
|
|
} else {
|
|
|
|
self.unexpected_any()
|
|
|
|
}
|
2019-11-30 23:25:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 00:24:21 +00:00
|
|
|
fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
|
|
|
|
Ok(if let Some(args) = self.parse_delim_args_inner() {
|
|
|
|
AttrArgs::Delimited(args)
|
2024-12-04 04:55:06 +00:00
|
|
|
} else if self.eat(exp!(Eq)) {
|
2024-09-11 21:23:56 +00:00
|
|
|
let eq_span = self.prev_token.span;
|
2024-10-16 23:14:01 +00:00
|
|
|
AttrArgs::Eq { eq_span, expr: self.parse_expr_force_collect()? }
|
2022-11-18 00:24:21 +00:00
|
|
|
} else {
|
2024-09-11 21:23:56 +00:00
|
|
|
AttrArgs::Empty
|
2022-11-18 00:24:21 +00:00
|
|
|
})
|
2019-11-30 23:25:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 00:24:21 +00:00
|
|
|
fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
|
2024-12-04 04:55:06 +00:00
|
|
|
let delimited = self.check(exp!(OpenParen))
|
|
|
|
|| self.check(exp!(OpenBracket))
|
|
|
|
|| self.check(exp!(OpenBrace));
|
2023-02-15 11:43:41 +00:00
|
|
|
|
|
|
|
delimited.then(|| {
|
2023-10-12 04:36:14 +00:00
|
|
|
let TokenTree::Delimited(dspan, _, delim, tokens) = self.parse_token_tree() else {
|
2023-02-15 11:43:41 +00:00
|
|
|
unreachable!()
|
|
|
|
};
|
2023-08-01 23:56:26 +00:00
|
|
|
DelimArgs { dspan, delim, tokens }
|
2023-02-15 11:43:41 +00:00
|
|
|
})
|
2014-10-29 14:47:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
/// Parses a single token tree from the input.
|
2023-07-31 05:51:08 +00:00
|
|
|
pub fn parse_token_tree(&mut self) -> TokenTree {
|
2019-08-11 11:14:30 +00:00
|
|
|
match self.token.kind {
|
|
|
|
token::OpenDelim(..) => {
|
2024-12-10 08:18:44 +00:00
|
|
|
// Clone the `TokenTree::Delimited` that we are currently
|
|
|
|
// within. That's what we are going to return.
|
|
|
|
let tree = self.token_cursor.stack.last().unwrap().curr().unwrap().clone();
|
|
|
|
debug_assert_matches!(tree, TokenTree::Delimited(..));
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
|
2022-04-21 02:26:58 +00:00
|
|
|
// Advance the token cursor through the entire delimited
|
|
|
|
// sequence. After getting the `OpenDelim` we are *within* the
|
|
|
|
// delimited sequence, i.e. at depth `d`. After getting the
|
|
|
|
// matching `CloseDelim` we are *after* the delimited sequence,
|
|
|
|
// i.e. at depth `d - 1`.
|
|
|
|
let target_depth = self.token_cursor.stack.len() - 1;
|
|
|
|
loop {
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
// Advance one token at a time, so `TokenCursor::next()`
|
|
|
|
// can capture these tokens if necessary.
|
|
|
|
self.bump();
|
2022-04-21 02:26:58 +00:00
|
|
|
if self.token_cursor.stack.len() == target_depth {
|
2024-08-11 16:10:36 +00:00
|
|
|
debug_assert_matches!(self.token.kind, token::CloseDelim(_));
|
2022-04-21 02:26:58 +00:00
|
|
|
break;
|
|
|
|
}
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
}
|
2022-04-21 02:26:58 +00:00
|
|
|
|
Rewrite `collect_tokens` implementations to use a flattened buffer
Instead of trying to collect tokens at each depth, we 'flatten' the
stream as we go allong, pushing open/close delimiters to our buffer
just like regular tokens. One capturing is complete, we reconstruct a
nested `TokenTree::Delimited` structure, producing a normal
`TokenStream`.
The reconstructed `TokenStream` is not created immediately - instead, it is
produced on-demand by a closure (wrapped in a new `LazyTokenStream` type). This
closure stores a clone of the original `TokenCursor`, plus a record of the
number of calls to `next()/next_desugared()`. This is sufficient to reconstruct
the tokenstream seen by the callback without storing any additional state. If
the tokenstream is never used (e.g. when a captured `macro_rules!` argument is
never passed to a proc macro), we never actually create a `TokenStream`.
This implementation has a number of advantages over the previous one:
* It is significantly simpler, with no edge cases around capturing the
start/end of a delimited group.
* It can be easily extended to allow replacing tokens an an arbitrary
'depth' by just using `Vec::splice` at the proper position. This is
important for PR #76130, which requires us to track information about
attributes along with tokens.
* The lazy approach to `TokenStream` construction allows us to easily
parse an AST struct, and then decide after the fact whether we need a
`TokenStream`. This will be useful when we start collecting tokens for
`Attribute` - we can discard the `LazyTokenStream` if the parsed
attribute doesn't need tokens (e.g. is a builtin attribute).
The performance impact seems to be neglibile (see
https://github.com/rust-lang/rust/pull/77250#issuecomment-703960604). There is a
small slowdown on a few benchmarks, but it only rises above 1% for incremental
builds, where it represents a larger fraction of the much smaller instruction
count. There a ~1% speedup on a few other incremental benchmarks - my guess is
that the speedups and slowdowns will usually cancel out in practice.
2020-09-27 01:56:29 +00:00
|
|
|
// Consume close delimiter
|
2019-08-11 11:14:30 +00:00
|
|
|
self.bump();
|
2024-12-10 08:18:44 +00:00
|
|
|
tree
|
2019-08-11 11:14:30 +00:00
|
|
|
}
|
|
|
|
token::CloseDelim(_) | token::Eof => unreachable!(),
|
|
|
|
_ => {
|
2023-08-08 01:43:44 +00:00
|
|
|
let prev_spacing = self.token_spacing;
|
2019-08-11 11:14:30 +00:00
|
|
|
self.bump();
|
2023-08-08 01:43:44 +00:00
|
|
|
TokenTree::Token(self.prev_token.clone(), prev_spacing)
|
2014-07-06 21:29:29 +00:00
|
|
|
}
|
2012-05-23 22:06:11 +00:00
|
|
|
}
|
2015-11-03 16:39:51 +00:00
|
|
|
}
|
|
|
|
|
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,
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 00:31:04 +00:00
|
|
|
_ => result.push(self.parse_token_tree()),
|
2019-04-15 00:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-11 11:14:30 +00:00
|
|
|
TokenStream::new(result)
|
2019-04-15 00:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 11:14:30 +00:00
|
|
|
/// Evaluates the closure with restrictions in place.
|
|
|
|
///
|
|
|
|
/// Afters the closure is evaluated, restrictions are reset.
|
2019-09-30 01:29:41 +00:00
|
|
|
fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
|
2019-08-11 11:14:30 +00:00
|
|
|
let old = self.restrictions;
|
2019-09-30 01:29:41 +00:00
|
|
|
self.restrictions = res;
|
|
|
|
let res = f(self);
|
2019-08-11 11:14:30 +00:00
|
|
|
self.restrictions = old;
|
2019-09-30 01:29:41 +00:00
|
|
|
res
|
2019-08-11 11:14:30 +00:00
|
|
|
}
|
2011-01-01 01:28:43 +00:00
|
|
|
|
2022-05-21 18:45:14 +00:00
|
|
|
/// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
|
|
|
|
/// for `pub(in self)` and `pub(super)` for `pub(in super)`.
|
2019-08-11 16:34:42 +00:00
|
|
|
/// If the following element can't be a tuple (i.e., it's a function definition), then
|
2021-10-17 10:04:01 +00:00
|
|
|
/// it's not a tuple struct field), and the contents within the parentheses aren't valid,
|
2019-08-11 16:34:42 +00:00
|
|
|
/// so emit a proper diagnostic.
|
2020-08-30 18:04:36 +00:00
|
|
|
// Public for rustfmt usage.
|
|
|
|
pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
|
2024-04-17 02:17:09 +00:00
|
|
|
if let Some(vis) = self
|
|
|
|
.eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
|
|
|
|
{
|
|
|
|
return Ok(vis);
|
|
|
|
}
|
2012-08-16 00:10:23 +00:00
|
|
|
|
2024-12-04 04:55:06 +00:00
|
|
|
if !self.eat_keyword(exp!(Pub)) {
|
2019-08-11 16:34:42 +00:00
|
|
|
// 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".
|
2020-08-21 23:11:00 +00:00
|
|
|
return Ok(Visibility {
|
|
|
|
span: self.token.span.shrink_to_lo(),
|
|
|
|
kind: VisibilityKind::Inherited,
|
|
|
|
tokens: None,
|
|
|
|
});
|
2019-08-11 16:34:42 +00:00
|
|
|
}
|
2020-02-29 11:56:15 +00:00
|
|
|
let lo = self.prev_token.span;
|
2017-03-07 23:50:13 +00:00
|
|
|
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.check(exp!(OpenParen)) {
|
2017-03-18 04:13:00 +00:00
|
|
|
// 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.
|
2022-05-21 18:45:14 +00:00
|
|
|
if self.is_keyword_ahead(1, &[kw::In]) {
|
2019-10-01 03:53:23 +00:00
|
|
|
// Parse `pub(in path)`.
|
|
|
|
self.bump(); // `(`
|
|
|
|
self.bump(); // `in`
|
|
|
|
let path = self.parse_path(PathStyle::Mod)?; // `path`
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expect(exp!(CloseParen))?; // `)`
|
2022-08-10 03:31:45 +00:00
|
|
|
let vis = VisibilityKind::Restricted {
|
|
|
|
path: P(path),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
shorthand: false,
|
|
|
|
};
|
2020-08-21 23:11:00 +00:00
|
|
|
return Ok(Visibility {
|
|
|
|
span: lo.to(self.prev_token.span),
|
|
|
|
kind: vis,
|
|
|
|
tokens: None,
|
|
|
|
});
|
2022-04-26 12:40:14 +00:00
|
|
|
} else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis))
|
2022-05-21 18:45:14 +00:00
|
|
|
&& self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
|
2018-01-27 07:13:50 +00:00
|
|
|
{
|
2022-05-21 18:45:14 +00:00
|
|
|
// Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
|
2019-10-01 03:53:23 +00:00
|
|
|
self.bump(); // `(`
|
2022-05-21 18:45:14 +00:00
|
|
|
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expect(exp!(CloseParen))?; // `)`
|
2022-08-10 03:31:45 +00:00
|
|
|
let vis = VisibilityKind::Restricted {
|
|
|
|
path: P(path),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
shorthand: true,
|
|
|
|
};
|
2020-08-21 23:11:00 +00:00
|
|
|
return Ok(Visibility {
|
|
|
|
span: lo.to(self.prev_token.span),
|
|
|
|
kind: vis,
|
|
|
|
tokens: None,
|
|
|
|
});
|
2019-11-07 10:26:36 +00:00
|
|
|
} else if let FollowedByType::No = fbt {
|
|
|
|
// Provide this diagnostic if a type cannot follow;
|
|
|
|
// in particular, if this is not a tuple struct.
|
2019-09-30 04:42:56 +00:00
|
|
|
self.recover_incorrect_vis_restriction()?;
|
|
|
|
// Emit diagnostic, but continue with public visibility.
|
2016-04-23 05:40:55 +00:00
|
|
|
}
|
2016-04-11 00:39:35 +00:00
|
|
|
}
|
2017-03-07 23:50:13 +00:00
|
|
|
|
2020-08-21 23:11:00 +00:00
|
|
|
Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
|
2012-02-23 05:47:23 +00:00
|
|
|
}
|
2013-03-22 19:56:10 +00:00
|
|
|
|
2019-09-30 04:42:56 +00:00
|
|
|
/// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
|
|
|
|
fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
|
|
|
|
self.bump(); // `(`
|
|
|
|
let path = self.parse_path(PathStyle::Mod)?;
|
2024-12-04 04:55:06 +00:00
|
|
|
self.expect(exp!(CloseParen))?; // `)`
|
2019-09-30 04:42:56 +00:00
|
|
|
|
2019-10-08 20:17:46 +00:00
|
|
|
let path_str = pprust::path_to_string(&path);
|
2023-12-18 10:14:02 +00:00
|
|
|
self.dcx()
|
|
|
|
.emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
|
2019-09-30 04:42:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-27 23:29:23 +00:00
|
|
|
/// Parses `extern string_literal?`.
|
2022-09-15 16:27:23 +00:00
|
|
|
fn parse_extern(&mut self, case: Case) -> Extern {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.eat_keyword_case(exp!(Extern), case) {
|
2022-07-02 17:25:55 +00:00
|
|
|
let mut extern_span = self.prev_token.span;
|
|
|
|
let abi = self.parse_abi();
|
|
|
|
if let Some(abi) = abi {
|
|
|
|
extern_span = extern_span.to(abi.span);
|
|
|
|
}
|
|
|
|
Extern::from_abi(abi, extern_span)
|
|
|
|
} else {
|
|
|
|
Extern::None
|
|
|
|
}
|
2019-09-29 23:22:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:29:23 +00:00
|
|
|
/// Parses a string literal as an ABI spec.
|
2019-11-10 14:04:12 +00:00
|
|
|
fn parse_abi(&mut self) -> Option<StrLit> {
|
|
|
|
match self.parse_str_lit() {
|
|
|
|
Ok(str_lit) => Some(str_lit),
|
|
|
|
Err(Some(lit)) => match lit.kind {
|
2024-02-14 09:12:05 +00:00
|
|
|
ast::LitKind::Err(_) => None,
|
2019-10-27 23:29:23 +00:00
|
|
|
_ => {
|
2023-12-18 10:14:02 +00:00
|
|
|
self.dcx().emit_err(NonStringAbiLiteral { span: lit.span });
|
2019-11-10 14:04:12 +00:00
|
|
|
None
|
2019-10-27 23:29:23 +00:00
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
},
|
2019-11-10 14:04:12 +00:00
|
|
|
Err(None) => None,
|
2019-11-09 19:05:20 +00:00
|
|
|
}
|
2013-03-14 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 05:47:46 +00:00
|
|
|
fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
|
2021-01-14 15:42:01 +00:00
|
|
|
&mut self,
|
|
|
|
f: impl FnOnce(&mut Self) -> PResult<'a, R>,
|
|
|
|
) -> PResult<'a, R> {
|
2021-01-22 18:28:08 +00:00
|
|
|
// The only reason to call `collect_tokens_no_attrs` is if you want tokens, so use
|
|
|
|
// `ForceCollect::Yes`
|
2024-08-06 07:16:40 +00:00
|
|
|
self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |this, _attrs| {
|
|
|
|
Ok((f(this)?, Trailing::No, UsePreAttrPos::No))
|
|
|
|
})
|
2021-01-14 15:42:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-21 17:07:52 +00:00
|
|
|
/// Checks for `::` or, potentially, `:::` and then look ahead after it.
|
|
|
|
fn check_path_sep_and_look_ahead(&mut self, looker: impl Fn(&Token) -> bool) -> bool {
|
2024-12-04 04:55:06 +00:00
|
|
|
if self.check(exp!(PathSep)) {
|
2024-09-21 17:07:52 +00:00
|
|
|
if self.may_recover() && self.look_ahead(1, |t| t.kind == token::Colon) {
|
|
|
|
debug_assert!(!self.look_ahead(1, &looker), "Looker must not match on colon");
|
|
|
|
self.look_ahead(2, looker)
|
|
|
|
} else {
|
|
|
|
self.look_ahead(1, looker)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:44:44 +00:00
|
|
|
/// `::{` or `::*`
|
|
|
|
fn is_import_coupler(&mut self) -> bool {
|
2024-09-21 17:07:52 +00:00
|
|
|
self.check_path_sep_and_look_ahead(|t| {
|
|
|
|
matches!(t.kind, token::OpenDelim(Delimiter::Brace) | token::BinOp(token::Star))
|
|
|
|
})
|
2016-04-17 00:48:40 +00:00
|
|
|
}
|
2020-08-31 09:45:50 +00:00
|
|
|
|
2024-06-03 05:47:46 +00:00
|
|
|
// Debug view of the parser's token stream, up to `{lookahead}` tokens.
|
|
|
|
// Only used when debugging.
|
|
|
|
#[allow(unused)]
|
2025-02-20 18:58:46 +00:00
|
|
|
pub(crate) fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug {
|
2025-01-23 19:48:54 +00:00
|
|
|
fmt::from_fn(move |f| {
|
|
|
|
let mut dbg_fmt = f.debug_struct("Parser"); // or at least, one view of
|
|
|
|
|
|
|
|
// we don't need N spans, but we want at least one, so print all of prev_token
|
|
|
|
dbg_fmt.field("prev_token", &self.prev_token);
|
|
|
|
let mut tokens = vec![];
|
|
|
|
for i in 0..lookahead {
|
|
|
|
let tok = self.look_ahead(i, |tok| tok.kind.clone());
|
|
|
|
let is_eof = tok == TokenKind::Eof;
|
|
|
|
tokens.push(tok);
|
|
|
|
if is_eof {
|
|
|
|
// Don't look ahead past EOF.
|
|
|
|
break;
|
2024-05-05 22:41:00 +00:00
|
|
|
}
|
2025-01-23 19:48:54 +00:00
|
|
|
}
|
|
|
|
dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
|
|
|
|
dbg_fmt.field("approx_token_stream_pos", &self.num_bump_calls);
|
2024-05-05 22:41:00 +00:00
|
|
|
|
2025-01-23 19:48:54 +00:00
|
|
|
// some fields are interesting for certain values, as they relate to macro parsing
|
|
|
|
if let Some(subparser) = self.subparser_name {
|
|
|
|
dbg_fmt.field("subparser_name", &subparser);
|
|
|
|
}
|
|
|
|
if let Recovery::Forbidden = self.recovery {
|
|
|
|
dbg_fmt.field("recovery", &self.recovery);
|
2024-05-05 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2025-01-23 19:48:54 +00:00
|
|
|
// imply there's "more to know" than this view
|
|
|
|
dbg_fmt.finish_non_exhaustive()
|
|
|
|
})
|
2024-05-05 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-03 09:09:29 +00:00
|
|
|
pub fn clear_expected_token_types(&mut self) {
|
|
|
|
self.expected_token_types.clear();
|
2020-08-31 09:45:50 +00:00
|
|
|
}
|
2022-12-11 20:46:30 +00:00
|
|
|
|
2024-07-02 06:31:24 +00:00
|
|
|
pub fn approx_token_stream_pos(&self) -> u32 {
|
2023-07-31 06:15:54 +00:00
|
|
|
self.num_bump_calls
|
2022-12-11 20:46:30 +00:00
|
|
|
}
|
2011-01-11 02:18:16 +00:00
|
|
|
}
|
2019-02-05 09:35:25 +00:00
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn make_unclosed_delims_error(
|
2023-02-21 14:51:19 +00:00
|
|
|
unmatched: UnmatchedDelim,
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &ParseSess,
|
2024-02-22 23:20:45 +00:00
|
|
|
) -> Option<Diag<'_>> {
|
2019-10-26 01:30:02 +00:00
|
|
|
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
|
2023-02-21 14:51:19 +00:00
|
|
|
// `unmatched_delims` only for error recovery in the `Parser`.
|
2019-10-26 01:30:02 +00:00
|
|
|
let found_delim = unmatched.found_delim?;
|
2022-08-24 20:41:51 +00:00
|
|
|
let mut spans = vec![unmatched.found_span];
|
2019-10-26 01:30:02 +00:00
|
|
|
if let Some(sp) = unmatched.unclosed_span {
|
2022-08-24 20:41:51 +00:00
|
|
|
spans.push(sp);
|
|
|
|
};
|
2024-06-18 09:43:28 +00:00
|
|
|
let err = psess.dcx().create_err(MismatchedClosingDelimiter {
|
2022-08-24 20:41:51 +00:00
|
|
|
spans,
|
|
|
|
delimiter: pprust::token_kind_to_string(&token::CloseDelim(found_delim)).to_string(),
|
|
|
|
unmatched: unmatched.found_span,
|
|
|
|
opening_candidate: unmatched.candidate_span,
|
|
|
|
unclosed: unmatched.unclosed_span,
|
2023-12-18 03:00:17 +00:00
|
|
|
});
|
2019-10-26 01:30:02 +00:00
|
|
|
Some(err)
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:05 +00:00
|
|
|
/// A helper struct used when building an `AttrTokenStream` from
|
2022-09-09 07:15:53 +00:00
|
|
|
/// a `LazyAttrTokenStream`. Both delimiter and non-delimited tokens
|
2020-11-28 23:33:17 +00:00
|
|
|
/// are stored as `FlatToken::Token`. A vector of `FlatToken`s
|
2022-09-09 02:44:05 +00:00
|
|
|
/// is then 'parsed' to build up an `AttrTokenStream` with nested
|
|
|
|
/// `AttrTokenTree::Delimited` tokens.
|
2020-11-28 23:33:17 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2024-06-03 05:47:46 +00:00
|
|
|
enum FlatToken {
|
2020-11-28 23:33:17 +00:00
|
|
|
/// A token - this holds both delimiter (e.g. '{' and '}')
|
|
|
|
/// and non-delimiter tokens
|
2024-07-08 09:32:21 +00:00
|
|
|
Token((Token, Spacing)),
|
2024-07-07 06:14:30 +00:00
|
|
|
/// Holds the `AttrsTarget` for an AST node. The `AttrsTarget` is inserted
|
|
|
|
/// directly into the constructed `AttrTokenStream` as an
|
|
|
|
/// `AttrTokenTree::AttrsTarget`.
|
|
|
|
AttrsTarget(AttrsTarget),
|
2020-11-28 23:33:17 +00:00
|
|
|
/// A special 'empty' token that is ignored during the conversion
|
2022-09-09 02:44:05 +00:00
|
|
|
/// to an `AttrTokenStream`. This is used to simplify the
|
2020-11-28 23:33:17 +00:00
|
|
|
/// handling of replace ranges.
|
|
|
|
Empty,
|
|
|
|
}
|
2022-03-25 01:39:12 +00:00
|
|
|
|
2024-03-19 02:33:33 +00:00
|
|
|
// Metavar captures of various kinds.
|
|
|
|
#[derive(Clone, Debug)]
|
2024-04-22 06:29:27 +00:00
|
|
|
pub enum ParseNtResult {
|
2022-03-25 01:39:12 +00:00
|
|
|
Tt(TokenTree),
|
2024-04-22 09:46:51 +00:00
|
|
|
Ident(Ident, IdentIsRaw),
|
2024-09-05 09:43:55 +00:00
|
|
|
Lifetime(Ident, IdentIsRaw),
|
2024-04-17 03:17:44 +00:00
|
|
|
Ty(P<ast::Ty>),
|
2024-04-17 02:17:09 +00:00
|
|
|
Vis(P<ast::Visibility>),
|
2024-04-22 09:46:51 +00:00
|
|
|
|
2024-04-17 02:17:09 +00:00
|
|
|
/// This variant will eventually be removed, along with `Token::Interpolate`.
|
2025-02-03 03:44:41 +00:00
|
|
|
Nt(Arc<Nonterminal>),
|
2022-03-25 01:39:12 +00:00
|
|
|
}
|