Skip to content

Commit e783775

Browse files
committed
Remove NtPath.
1 parent 65b8dfd commit e783775

File tree

14 files changed

+45
-52
lines changed

14 files changed

+45
-52
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,13 @@ impl HasTokens for Nonterminal {
232232
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233233
match self {
234234
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
235-
Nonterminal::NtPath(path) => path.tokens(),
236235
Nonterminal::NtBlock(block) => block.tokens(),
237236
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
238237
}
239238
}
240239
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
241240
match self {
242241
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
243-
Nonterminal::NtPath(path) => path.tokens_mut(),
244242
Nonterminal::NtBlock(block) => block.tokens_mut(),
245243
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
246244
}

compiler/rustc_ast/src/attr/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,12 @@ impl MetaItem {
342342
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
343343
Path { span, segments, tokens: None }
344344
}
345-
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &nt.0 {
346-
token::Nonterminal::NtPath(path) => (**path).clone(),
347-
_ => return None,
348-
},
349345
Some(TokenTree::Delimited(
350346
_span,
351347
_spacing,
352-
Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Meta)),
348+
Delimiter::Invisible(InvisibleOrigin::MetaVar(
349+
NonterminalKind::Meta | NonterminalKind::Path,
350+
)),
353351
_stream,
354352
)) => {
355353
// 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
@@ -828,7 +828,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
828828
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
829829
token::NtLifetime(ident) => vis.visit_ident(ident),
830830
token::NtLiteral(expr) => vis.visit_expr(expr),
831-
token::NtPath(path) => vis.visit_path(path),
832831
}
833832
}
834833

compiler/rustc_ast/src/token.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,7 @@ impl Token {
518518
Pound => true, // expression attributes
519519
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
520520
NtExpr(..) |
521-
NtBlock(..) |
522-
NtPath(..)),
521+
NtBlock(..)),
523522
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
524523
NonterminalKind::Block |
525524
NonterminalKind::Expr |
@@ -546,9 +545,7 @@ impl Token {
546545
| DotDot | DotDotDot | DotDotEq // ranges
547546
| Lt | BinOp(Shl) // associated path
548547
| PathSep => true, // global path
549-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
550-
NtBlock(..) |
551-
NtPath(..)),
548+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtBlock(..)),
552549
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
553550
NonterminalKind::Block |
554551
NonterminalKind::PatParam { .. } |
@@ -575,7 +572,6 @@ impl Token {
575572
Lifetime(..) | // lifetime bound in trait object
576573
Lt | BinOp(Shl) | // associated path
577574
PathSep => true, // global path
578-
Interpolated(ref nt) => matches!(&nt.0, NtPath(..)),
579575
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
580576
NonterminalKind::Ty |
581577
NonterminalKind::Path
@@ -718,28 +714,19 @@ impl Token {
718714
self.ident().is_some_and(|(ident, _)| ident.name == name)
719715
}
720716

721-
/// Returns `true` if the token is an interpolated path.
722-
fn is_whole_path(&self) -> bool {
723-
if let Interpolated(nt) = &self.kind
724-
&& let NtPath(..) = &nt.0
725-
{
726-
return true;
727-
}
728-
729-
false
730-
}
731-
732717
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
733718
/// That is, is this a pre-parsed expression dropped into the token stream
734719
/// (which happens while parsing the result of macro expansion)?
735720
pub fn is_whole_expr(&self) -> bool {
736721
if let Interpolated(nt) = &self.kind
737-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &nt.0
722+
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &nt.0
738723
{
739-
return true;
724+
true
725+
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
726+
true
727+
} else {
728+
false
740729
}
741-
742-
false
743730
}
744731

745732
/// Is the token an interpolated block (`$b:block`)?
@@ -765,7 +752,7 @@ impl Token {
765752
pub fn is_path_start(&self) -> bool {
766753
self == &PathSep
767754
|| self.is_qpath_start()
768-
|| self.is_whole_path()
755+
|| matches!(self.is_metavar_seq(), Some(NonterminalKind::Path))
769756
|| self.is_path_segment_keyword()
770757
|| self.is_ident() && !self.is_reserved_ident()
771758
}
@@ -918,7 +905,6 @@ pub enum Nonterminal {
918905
NtIdent(Ident, IdentIsRaw),
919906
NtLifetime(Ident),
920907
NtLiteral(P<ast::Expr>),
921-
NtPath(P<ast::Path>),
922908
}
923909

924910
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1005,7 +991,6 @@ impl Nonterminal {
1005991
NtBlock(block) => block.span,
1006992
NtExpr(expr) | NtLiteral(expr) => expr.span,
1007993
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
1008-
NtPath(path) => path.span,
1009994
}
1010995
}
1011996

@@ -1016,7 +1001,6 @@ impl Nonterminal {
10161001
NtLiteral(..) => "literal",
10171002
NtIdent(..) => "identifier",
10181003
NtLifetime(..) => "lifetime",
1019-
NtPath(..) => "path",
10201004
}
10211005
}
10221006
}
@@ -1044,7 +1028,6 @@ impl fmt::Debug for Nonterminal {
10441028
NtExpr(..) => f.pad("NtExpr(..)"),
10451029
NtIdent(..) => f.pad("NtIdent(..)"),
10461030
NtLiteral(..) => f.pad("NtLiteral(..)"),
1047-
NtPath(..) => f.pad("NtPath(..)"),
10481031
NtLifetime(..) => f.pad("NtLifetime(..)"),
10491032
}
10501033
}

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ impl TokenStream {
484484
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
485485
}
486486
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
487-
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
488487
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
489488
}
490489
}

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845845
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
846846
match nt {
847847
token::NtExpr(e) => self.expr_to_string(e),
848-
token::NtPath(e) => self.path_to_string(e),
849848
token::NtBlock(e) => self.block_to_string(e),
850849
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),
851850
token::NtLifetime(e) => e.to_string(),

compiler/rustc_expand/src/mbe/transcribe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ pub(super) fn transcribe<'a>(
292292
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
293293
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
294294
}
295+
MatchedSingle(ParseNtResult::Path(ref path)) => {
296+
mk_delimited(NonterminalKind::Path, TokenStream::from_ast(path))
297+
}
295298
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
296299
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
297300
}

compiler/rustc_parse/src/parser/expr.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,29 @@ use thin_vec::{thin_vec, ThinVec};
4444
/// `token::Interpolated` tokens.
4545
macro_rules! maybe_whole_expr {
4646
($p:expr) => {
47+
let span = $p.token.span;
4748
if let token::Interpolated(nt) = &$p.token.kind {
4849
match &nt.0 {
4950
token::NtExpr(e) | token::NtLiteral(e) => {
5051
let e = e.clone();
5152
$p.bump();
5253
return Ok(e);
5354
}
54-
token::NtPath(path) => {
55-
let path = (**path).clone();
56-
$p.bump();
57-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Path(None, path)));
58-
}
5955
token::NtBlock(block) => {
6056
let block = block.clone();
6157
$p.bump();
6258
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6359
}
6460
_ => {}
6561
};
62+
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
63+
$p,
64+
token::NonterminalKind::Path,
65+
token::NonterminalKind::Path,
66+
super::ParseNtResult::Path(path),
67+
path
68+
) {
69+
return Ok($p.mk_expr(span, ExprKind::Path(None, path.into_inner())));
6670
}
6771
};
6872
}

compiler/rustc_parse/src/parser/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,7 @@ pub enum ParseNtResult<NtType> {
16761676
PatWithOr(P<ast::Pat>),
16771677
Ty(P<ast::Ty>),
16781678
Meta(P<ast::AttrItem>),
1679+
Path(P<ast::Path>),
16791680
Vis(P<ast::Visibility>),
16801681

16811682
/// This variant will eventually be removed, along with `Token::Interpolate`.
@@ -1695,6 +1696,7 @@ impl<T> ParseNtResult<T> {
16951696
ParseNtResult::PatWithOr(x) => ParseNtResult::PatWithOr(x),
16961697
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
16971698
ParseNtResult::Meta(x) => ParseNtResult::Meta(x),
1699+
ParseNtResult::Path(x) => ParseNtResult::Path(x),
16981700
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16991701
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
17001702
}

compiler/rustc_parse/src/parser/nonterminal.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a> Parser<'a> {
4848
NtExpr(_)
4949
| NtIdent(..)
5050
| NtLiteral(_) // `true`, `false`
51-
| NtPath(_) => true,
51+
=> true,
5252

5353
NtBlock(_)
5454
| NtLifetime(_) => false,
@@ -78,7 +78,7 @@ impl<'a> Parser<'a> {
7878
token::OpenDelim(Delimiter::Brace) => true,
7979
token::Interpolated(nt) => match &nt.0 {
8080
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
81-
NtIdent(..) | NtPath(_) => false,
81+
NtIdent(..) => false,
8282
},
8383
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
8484
NonterminalKind::Block
@@ -218,7 +218,9 @@ impl<'a> Parser<'a> {
218218
}));
219219
}
220220
NonterminalKind::Path => {
221-
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
221+
return Ok(ParseNtResult::Path(P(
222+
self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?
223+
)));
222224
}
223225
NonterminalKind::Meta => {
224226
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?)));

compiler/rustc_parse/src/parser/path.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
22
use super::{ParseNtResult, Parser, Restrictions, TokenType};
33
use crate::errors::PathSingleColon;
44
use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
5-
use crate::{errors, maybe_whole};
5+
use crate::{errors, maybe_reparse_metavar_seq};
66
use ast::token::IdentIsRaw;
77
use rustc_ast::ptr::P;
88
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
@@ -181,10 +181,16 @@ impl<'a> Parser<'a> {
181181
}
182182
};
183183

184-
maybe_whole!(self, NtPath, |path| {
184+
if let Some(path) = maybe_reparse_metavar_seq!(
185+
self,
186+
NonterminalKind::Path,
187+
NonterminalKind::Path,
188+
ParseNtResult::Path(path),
189+
path
190+
) {
185191
reject_generics_if_mod_style(self, &path);
186-
path.into_inner()
187-
});
192+
return Ok(path.into_inner());
193+
}
188194

189195
if let Some(NonterminalKind::Ty) = self.token.is_metavar_seq() {
190196
let mut self2 = self.clone();

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/stringify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn test_path() {
616616
c1!(path, [ crate::thing ], "crate::thing");
617617
c1!(path, [ Self::thing ], "Self::thing");
618618
c1!(path, [ Self<'static> ], "Self<'static>");
619-
c2!(path, [ Self::<'static> ], "Self<'static>", "Self::<'static>");
619+
c1!(path, [ Self::<'static> ], "Self::<'static>");
620620
c1!(path, [ Self() ], "Self()");
621621
c1!(path, [ Self() -> () ], "Self() -> ()");
622622
}

0 commit comments

Comments
 (0)