Skip to content

Commit

Permalink
Avoid snapshotting the parser in parse_path_inner.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Feb 21, 2025
1 parent 76b0443 commit 0f490b0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ pub enum MetaVarKind {
// This field is needed for `Token::can_begin_string_literal`.
can_begin_string_literal: bool,
},
Ty,
Ty {
is_path: bool,
},
Ident,
Lifetime,
Literal,
Expand All @@ -104,7 +106,7 @@ impl fmt::Display for MetaVarKind {
MetaVarKind::Pat(PatParam { inferred: false }) => sym::pat_param,
MetaVarKind::Expr { kind: Expr2021 { inferred: true } | Expr, .. } => sym::expr,
MetaVarKind::Expr { kind: Expr2021 { inferred: false }, .. } => sym::expr_2021,
MetaVarKind::Ty => sym::ty,
MetaVarKind::Ty { .. } => sym::ty,
MetaVarKind::Ident => sym::ident,
MetaVarKind::Lifetime => sym::lifetime,
MetaVarKind::Literal => sym::literal,
Expand Down Expand Up @@ -666,7 +668,7 @@ impl Token {
MetaVarKind::Meta |
MetaVarKind::Pat(_) |
MetaVarKind::Path |
MetaVarKind::Ty
MetaVarKind::Ty { .. }
))) => true,
_ => false,
}
Expand All @@ -689,7 +691,7 @@ impl Token {
PathSep => true, // global path
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
MetaVarKind::Ty |
MetaVarKind::Ty { .. } |
MetaVarKind::Path
))) => true,
// For anonymous structs or unions, which only appear in specific positions
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::mem;
use std::sync::Arc;

use rustc_ast::ExprKind;
use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::token::{
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
TokenKind,
};
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::{ExprKind, TyKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
use rustc_parse::lexer::nfc_normalize;
Expand Down Expand Up @@ -323,7 +323,8 @@ pub(super) fn transcribe<'a>(
TokenTree::token_alone(kind, sp)
}
MatchedSingle(ParseNtResult::Ty(ty)) => {
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
mk_delimited(MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
}
MatchedSingle(ParseNtResult::Vis(vis)) => {
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,13 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
($self: expr, $allow_qpath_recovery: expr) => {
if $allow_qpath_recovery
&& $self.may_recover()
&& let Some(token::MetaVarKind::Ty) = $self.token.is_metavar_seq()
&& let Some(mv_kind) = $self.token.is_metavar_seq()
&& let token::MetaVarKind::Ty { .. } = mv_kind
&& $self.check_noexpect_past_close_delim(&token::PathSep)
{
// Reparse the type, then move to recovery.
let ty = $self
.eat_metavar_seq(token::MetaVarKind::Ty, |this| {
this.parse_ty_no_question_mark_recover()
})
.eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
.expect("metavar seq ty");

return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> Parser<'a> {
MetaVarKind::Stmt
| MetaVarKind::Pat(_)
| MetaVarKind::Expr { .. }
| MetaVarKind::Ty
| MetaVarKind::Ty { .. }
| MetaVarKind::Literal // `true`, `false`
| MetaVarKind::Meta
| MetaVarKind::Path => true,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'a> Parser<'a> {
| MetaVarKind::Literal => true,
MetaVarKind::Item
| MetaVarKind::Pat(_)
| MetaVarKind::Ty
| MetaVarKind::Ty { .. }
| MetaVarKind::Meta
| MetaVarKind::Path
| MetaVarKind::Vis => false,
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,12 @@ impl<'a> Parser<'a> {

maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));

if let Some(MetaVarKind::Ty) = self.token.is_metavar_seq() {
let mut snapshot = self.create_snapshot_for_diagnostic();
let ty = snapshot
.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
.expect("metavar seq ty");
if let ast::TyKind::Path(None, path) = ty.into_inner().kind {
self.restore_snapshot(snapshot);
return Ok(reject_generics_if_mod_style(self, path));
}
// If we have a `ty` metavar in the form of a path, reparse it directly as a path, instead
// of reparsing it as a `ty` and then extracting the path.
if let Some(path) = self.eat_metavar_seq(MetaVarKind::Ty { is_path: true }, |this| {
this.parse_path(PathStyle::Type)
}) {
return Ok(reject_generics_if_mod_style(self, path));
}

let lo = self.token.span;
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ impl<'a> Parser<'a> {
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);

if let Some(ty) =
self.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
{
if let Some(ty) = self.eat_metavar_seq_with_matcher(
|mv_kind| matches!(mv_kind, MetaVarKind::Ty { .. }),
|this| this.parse_ty_no_question_mark_recover(),
) {
return Ok(ty);
}

Expand Down

0 comments on commit 0f490b0

Please sign in to comment.