Skip to content

Commit 50076cd

Browse files
committed
Remove NtPath.
1 parent 7ea59e0 commit 50076cd

File tree

15 files changed

+49
-74
lines changed

15 files changed

+49
-74
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ impl HasTokens for Nonterminal {
202202
Nonterminal::NtItem(item) => item.tokens(),
203203
Nonterminal::NtStmt(stmt) => stmt.tokens(),
204204
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
205-
Nonterminal::NtPath(path) => path.tokens(),
206205
Nonterminal::NtBlock(block) => block.tokens(),
207206
}
208207
}
@@ -211,7 +210,6 @@ impl HasTokens for Nonterminal {
211210
Nonterminal::NtItem(item) => item.tokens_mut(),
212211
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
213212
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
214-
Nonterminal::NtPath(path) => path.tokens_mut(),
215213
Nonterminal::NtBlock(block) => block.tokens_mut(),
216214
}
217215
}

compiler/rustc_ast/src/attr/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,12 @@ impl MetaItem {
405405
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
406406
Path { span, segments, tokens: None }
407407
}
408-
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
409-
token::Nonterminal::NtPath(path) => (**path).clone(),
410-
_ => return None,
411-
},
412408
Some(TokenTree::Delimited(
413409
_span,
414410
_spacing,
415-
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta { .. })),
411+
Delimiter::Invisible(InvisibleOrigin::MetaVar(
412+
MetaVarKind::Meta { .. } | MetaVarKind::Path,
413+
)),
416414
_stream,
417415
)) => {
418416
// This path is currently unreachable in the test suite.

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
907907
}),
908908
token::NtExpr(expr) => vis.visit_expr(expr),
909909
token::NtLiteral(expr) => vis.visit_expr(expr),
910-
token::NtPath(path) => vis.visit_path(path),
911910
}
912911
}
913912

compiler/rustc_ast/src/token.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,7 @@ impl Token {
624624
matches!(&**nt,
625625
NtBlock(..) |
626626
NtExpr(..) |
627-
NtLiteral(..) |
628-
NtPath(..)
627+
NtLiteral(..)
629628
),
630629
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
631630
MetaVarKind::Block |
@@ -661,7 +660,6 @@ impl Token {
661660
matches!(&**nt,
662661
| NtExpr(..)
663662
| NtLiteral(..)
664-
| NtPath(..)
665663
),
666664
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
667665
MetaVarKind::Expr { .. } |
@@ -690,7 +688,6 @@ impl Token {
690688
Lifetime(..) | // lifetime bound in trait object
691689
Lt | BinOp(Shl) | // associated path
692690
PathSep => true, // global path
693-
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
694691
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
695692
MetaVarKind::Ty { .. } |
696693
MetaVarKind::Path
@@ -849,27 +846,16 @@ impl Token {
849846
self.ident().is_some_and(|(ident, _)| ident.name == name)
850847
}
851848

852-
/// Returns `true` if the token is an interpolated path.
853-
fn is_whole_path(&self) -> bool {
854-
if let Interpolated(nt) = &self.kind
855-
&& let NtPath(..) = &**nt
856-
{
857-
return true;
858-
}
859-
860-
false
861-
}
862-
863849
/// Is this a pre-parsed expression dropped into the token stream
864850
/// (which happens while parsing the result of macro expansion)?
865851
pub fn is_whole_expr(&self) -> bool {
866852
if let Interpolated(nt) = &self.kind
867-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
853+
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
868854
{
869-
return true;
855+
true
856+
} else {
857+
matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
870858
}
871-
872-
false
873859
}
874860

875861
/// Is the token an interpolated block (`$b:block`)?
@@ -895,7 +881,7 @@ impl Token {
895881
pub fn is_path_start(&self) -> bool {
896882
self == &PathSep
897883
|| self.is_qpath_start()
898-
|| self.is_whole_path()
884+
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
899885
|| self.is_path_segment_keyword()
900886
|| self.is_ident() && !self.is_reserved_ident()
901887
}
@@ -1078,7 +1064,6 @@ pub enum Nonterminal {
10781064
NtStmt(P<ast::Stmt>),
10791065
NtExpr(P<ast::Expr>),
10801066
NtLiteral(P<ast::Expr>),
1081-
NtPath(P<ast::Path>),
10821067
}
10831068

10841069
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1171,7 +1156,6 @@ impl Nonterminal {
11711156
NtBlock(block) => block.span,
11721157
NtStmt(stmt) => stmt.span,
11731158
NtExpr(expr) | NtLiteral(expr) => expr.span,
1174-
NtPath(path) => path.span,
11751159
}
11761160
}
11771161

@@ -1182,7 +1166,6 @@ impl Nonterminal {
11821166
NtStmt(..) => "statement",
11831167
NtExpr(..) => "expression",
11841168
NtLiteral(..) => "literal",
1185-
NtPath(..) => "path",
11861169
}
11871170
}
11881171
}
@@ -1205,7 +1188,6 @@ impl fmt::Debug for Nonterminal {
12051188
NtStmt(..) => f.pad("NtStmt(..)"),
12061189
NtExpr(..) => f.pad("NtExpr(..)"),
12071190
NtLiteral(..) => f.pad("NtLiteral(..)"),
1208-
NtPath(..) => f.pad("NtPath(..)"),
12091191
}
12101192
}
12111193
}

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ impl TokenStream {
468468
TokenStream::token_alone(token::Semi, stmt.span)
469469
}
470470
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
471-
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
472471
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
473472
}
474473
}

compiler/rustc_attr_parsing/src/parser.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -477,32 +477,21 @@ impl<'a> MetaItemListParserContext<'a> {
477477

478478
// or a path.
479479
let path =
480-
if let Some(TokenTree::Token(Token { kind: token::Interpolated(nt), span, .. }, _)) =
480+
if let Some(TokenTree::Token(Token { kind: token::Interpolated(_), span, .. }, _)) =
481481
self.inside_delimiters.peek()
482482
{
483-
match &**nt {
484-
// an already interpolated path from a macro expansion is a path, no need to parse
485-
// one from tokens
486-
token::Nonterminal::NtPath(path) => {
487-
self.inside_delimiters.next();
488-
489-
AttrPath::from_ast(path)
490-
}
491-
_ => {
492-
self.inside_delimiters.next();
493-
// we go into this path if an expr ended up in an attribute that
494-
// expansion did not turn into a literal. Say, `#[repr(align(macro!()))]`
495-
// where the macro didn't expand to a literal. An error is already given
496-
// for this at this point, and then we do continue. This makes this path
497-
// reachable...
498-
let e = self.dcx.span_delayed_bug(
499-
*span,
500-
"expr in place where literal is expected (builtin attr parsing)",
501-
);
502-
503-
return Some(MetaItemOrLitParser::Err(*span, e));
504-
}
505-
}
483+
self.inside_delimiters.next();
484+
// We go into this path if an expr ended up in an attribute that
485+
// expansion did not turn into a literal. Say, `#[repr(align(macro!()))]`
486+
// where the macro didn't expand to a literal. An error is already given
487+
// for this at this point, and then we do continue. This makes this path
488+
// reachable...
489+
let e = self.dcx.span_delayed_bug(
490+
*span,
491+
"expr in place where literal is expected (builtin attr parsing)",
492+
);
493+
494+
return Some(MetaItemOrLitParser::Err(*span, e));
506495
} else {
507496
self.next_path()?
508497
};

compiler/rustc_expand/src/mbe/transcribe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ pub(super) fn transcribe<'a>(
344344
TokenStream::from_ast(attr_item),
345345
)
346346
}
347+
MatchedSingle(ParseNtResult::Path(path)) => {
348+
mk_delimited(path.span, MetaVarKind::Path, TokenStream::from_ast(path))
349+
}
347350
MatchedSingle(ParseNtResult::Vis(vis)) => {
348351
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
349352
}

compiler/rustc_parse/src/parser/expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::mem;
44
use core::ops::{Bound, ControlFlow};
55

66
use ast::mut_visit::{self, MutVisitor};
7-
use ast::token::IdentIsRaw;
7+
use ast::token::{IdentIsRaw, MetaVarKind};
88
use ast::{CoroutineKind, ForLoopKind, GenBlockKind, MatchKind, Pat, Path, PathSegment, Recovered};
99
use rustc_ast::ptr::P;
1010
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
@@ -1382,25 +1382,25 @@ impl<'a> Parser<'a> {
13821382
fn parse_expr_bottom(&mut self) -> PResult<'a, P<Expr>> {
13831383
maybe_recover_from_interpolated_ty_qpath!(self, true);
13841384

1385+
let span = self.token.span;
13851386
if let token::Interpolated(nt) = &self.token.kind {
13861387
match &**nt {
13871388
token::NtExpr(e) | token::NtLiteral(e) => {
13881389
let e = e.clone();
13891390
self.bump();
13901391
return Ok(e);
13911392
}
1392-
token::NtPath(path) => {
1393-
let path = (**path).clone();
1394-
self.bump();
1395-
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Path(None, path)));
1396-
}
13971393
token::NtBlock(block) => {
13981394
let block = block.clone();
13991395
self.bump();
14001396
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
14011397
}
14021398
_ => {}
14031399
};
1400+
} else if let Some(path) = self.eat_metavar_seq(MetaVarKind::Path, |this| {
1401+
this.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))
1402+
}) {
1403+
return Ok(self.mk_expr(span, ExprKind::Path(None, path)));
14041404
}
14051405

14061406
// Outer attributes are already parsed and will be

compiler/rustc_parse/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,7 @@ pub enum ParseNtResult {
17491749
Pat(P<ast::Pat>, NtPatKind),
17501750
Ty(P<ast::Ty>),
17511751
Meta(P<ast::AttrItem>),
1752+
Path(P<ast::Path>),
17521753
Vis(P<ast::Visibility>),
17531754

17541755
/// This variant will eventually be removed, along with `Token::Interpolate`.

compiler/rustc_parse/src/parser/nonterminal.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a> Parser<'a> {
5151
NtStmt(_)
5252
| NtExpr(_)
5353
| NtLiteral(_) // `true`, `false`
54-
| NtPath(_) => true,
54+
=> true,
5555

5656
NtItem(_) | NtBlock(_) => false,
5757
}
@@ -97,7 +97,7 @@ impl<'a> Parser<'a> {
9797
token::NtLifetime(..) => true,
9898
token::Interpolated(nt) => match &**nt {
9999
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
100-
NtItem(_) | NtPath(_) => false,
100+
NtItem(_) => false,
101101
},
102102
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
103103
MetaVarKind::Block
@@ -204,7 +204,9 @@ impl<'a> Parser<'a> {
204204
};
205205
}
206206
NonterminalKind::Path => {
207-
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
207+
return Ok(ParseNtResult::Path(P(
208+
self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?
209+
)));
208210
}
209211
NonterminalKind::Meta => {
210212
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(ForceCollect::Yes)?)));

compiler/rustc_parse/src/parser/path.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use tracing::debug;
1515

1616
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
1717
use super::{Parser, Restrictions, TokenType};
18-
use crate::errors::{PathSingleColon, PathTripleColon};
18+
use crate::errors::{self, PathSingleColon, PathTripleColon};
19+
use crate::exp;
1920
use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
20-
use crate::{errors, exp, maybe_whole};
2121

2222
/// Specifies how to parse a path.
2323
#[derive(Copy, Clone, PartialEq)]
@@ -194,7 +194,11 @@ impl<'a> Parser<'a> {
194194
}
195195
};
196196

197-
maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));
197+
if let Some(path) =
198+
self.eat_metavar_seq(MetaVarKind::Path, |this| this.parse_path(PathStyle::Type))
199+
{
200+
return Ok(reject_generics_if_mod_style(self, path));
201+
}
198202

199203
// If we have a `ty` metavar in the form of a path, reparse it directly as a path, instead
200204
// of reparsing it as a `ty` and then extracting the path.

tests/ui/imports/import-prefix-macro-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod a {
88
}
99

1010
macro_rules! import {
11-
($p: path) => (use ::$p {S, Z}); //~ERROR expected identifier, found `a::b::c`
11+
($p: path) => (use ::$p {S, Z}); //~ERROR expected identifier, found metavariable
1212
}
1313

1414
import! { a::b::c }

tests/ui/imports/import-prefix-macro-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected identifier, found `a::b::c`
1+
error: expected identifier, found metavariable
22
--> $DIR/import-prefix-macro-2.rs:11:26
33
|
44
LL | ($p: path) => (use ::$p {S, Z});
5-
| ^^ expected identifier
5+
| ^^ expected identifier, found metavariable
66
...
77
LL | import! { a::b::c }
88
| ------------------- in this macro invocation

tests/ui/macros/nonterminal-matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! foo {
3131
(tt $x:tt) => { bar!(tt $x); };
3232
(expr $x:expr) => { bar!(expr $x); }; //~ ERROR: no rules expected expression `3`
3333
(literal $x:literal) => { bar!(literal $x); }; //~ ERROR: no rules expected literal `4`
34-
(path $x:path) => { bar!(path $x); }; //~ ERROR: no rules expected path `a::b::c`
34+
(path $x:path) => { bar!(path $x); }; //~ ERROR: no rules expected `path` metavariable
3535
(stmt $x:stmt) => { bar!(stmt $x); }; //~ ERROR: no rules expected statement `let abc = 0`
3636
}
3737

tests/ui/macros/nonterminal-matching.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ LL | (literal 4) => {};
6767
= help: try using `:tt` instead in the macro definition
6868
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
6969

70-
error: no rules expected path `a::b::c`
70+
error: no rules expected `path` metavariable
7171
--> $DIR/nonterminal-matching.rs:34:35
7272
|
7373
LL | (path $x:path) => { bar!(path $x); };

0 commit comments

Comments
 (0)