Skip to content

Commit 0f490b0

Browse files
committed
Avoid snapshotting the parser in parse_path_inner.
1 parent 76b0443 commit 0f490b0

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

Diff for: compiler/rustc_ast/src/token.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ pub enum MetaVarKind {
8484
// This field is needed for `Token::can_begin_string_literal`.
8585
can_begin_string_literal: bool,
8686
},
87-
Ty,
87+
Ty {
88+
is_path: bool,
89+
},
8890
Ident,
8991
Lifetime,
9092
Literal,
@@ -104,7 +106,7 @@ impl fmt::Display for MetaVarKind {
104106
MetaVarKind::Pat(PatParam { inferred: false }) => sym::pat_param,
105107
MetaVarKind::Expr { kind: Expr2021 { inferred: true } | Expr, .. } => sym::expr,
106108
MetaVarKind::Expr { kind: Expr2021 { inferred: false }, .. } => sym::expr_2021,
107-
MetaVarKind::Ty => sym::ty,
109+
MetaVarKind::Ty { .. } => sym::ty,
108110
MetaVarKind::Ident => sym::ident,
109111
MetaVarKind::Lifetime => sym::lifetime,
110112
MetaVarKind::Literal => sym::literal,
@@ -666,7 +668,7 @@ impl Token {
666668
MetaVarKind::Meta |
667669
MetaVarKind::Pat(_) |
668670
MetaVarKind::Path |
669-
MetaVarKind::Ty
671+
MetaVarKind::Ty { .. }
670672
))) => true,
671673
_ => false,
672674
}
@@ -689,7 +691,7 @@ impl Token {
689691
PathSep => true, // global path
690692
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
691693
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
692-
MetaVarKind::Ty |
694+
MetaVarKind::Ty { .. } |
693695
MetaVarKind::Path
694696
))) => true,
695697
// For anonymous structs or unions, which only appear in specific positions

Diff for: compiler/rustc_expand/src/mbe/transcribe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::mem;
22
use std::sync::Arc;
33

4-
use rustc_ast::ExprKind;
54
use rustc_ast::mut_visit::{self, MutVisitor};
65
use rustc_ast::token::{
76
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
87
TokenKind,
98
};
109
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
10+
use rustc_ast::{ExprKind, TyKind};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
1313
use rustc_parse::lexer::nfc_normalize;
@@ -323,7 +323,8 @@ pub(super) fn transcribe<'a>(
323323
TokenTree::token_alone(kind, sp)
324324
}
325325
MatchedSingle(ParseNtResult::Ty(ty)) => {
326-
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
326+
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
327+
mk_delimited(MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
327328
}
328329
MatchedSingle(ParseNtResult::Vis(vis)) => {
329330
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))

Diff for: compiler/rustc_parse/src/parser/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,13 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
117117
($self: expr, $allow_qpath_recovery: expr) => {
118118
if $allow_qpath_recovery
119119
&& $self.may_recover()
120-
&& let Some(token::MetaVarKind::Ty) = $self.token.is_metavar_seq()
120+
&& let Some(mv_kind) = $self.token.is_metavar_seq()
121+
&& let token::MetaVarKind::Ty { .. } = mv_kind
121122
&& $self.check_noexpect_past_close_delim(&token::PathSep)
122123
{
123124
// Reparse the type, then move to recovery.
124125
let ty = $self
125-
.eat_metavar_seq(token::MetaVarKind::Ty, |this| {
126-
this.parse_ty_no_question_mark_recover()
127-
})
126+
.eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
128127
.expect("metavar seq ty");
129128

130129
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);

Diff for: compiler/rustc_parse/src/parser/nonterminal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a> Parser<'a> {
3030
MetaVarKind::Stmt
3131
| MetaVarKind::Pat(_)
3232
| MetaVarKind::Expr { .. }
33-
| MetaVarKind::Ty
33+
| MetaVarKind::Ty { .. }
3434
| MetaVarKind::Literal // `true`, `false`
3535
| MetaVarKind::Meta
3636
| MetaVarKind::Path => true,
@@ -108,7 +108,7 @@ impl<'a> Parser<'a> {
108108
| MetaVarKind::Literal => true,
109109
MetaVarKind::Item
110110
| MetaVarKind::Pat(_)
111-
| MetaVarKind::Ty
111+
| MetaVarKind::Ty { .. }
112112
| MetaVarKind::Meta
113113
| MetaVarKind::Path
114114
| MetaVarKind::Vis => false,

Diff for: compiler/rustc_parse/src/parser/path.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,12 @@ impl<'a> Parser<'a> {
196196

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

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

210207
let lo = self.token.span;

Diff for: compiler/rustc_parse/src/parser/ty.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@ impl<'a> Parser<'a> {
249249
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
250250
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
251251

252-
if let Some(ty) =
253-
self.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
254-
{
252+
if let Some(ty) = self.eat_metavar_seq_with_matcher(
253+
|mv_kind| matches!(mv_kind, MetaVarKind::Ty { .. }),
254+
|this| this.parse_ty_no_question_mark_recover(),
255+
) {
255256
return Ok(ty);
256257
}
257258

0 commit comments

Comments
 (0)