Skip to content

Commit 705e4fd

Browse files
committed
Remove NtPath.
1 parent 973cedb commit 705e4fd

File tree

14 files changed

+45
-53
lines changed

14 files changed

+45
-53
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-27
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,7 @@ impl Token {
522522
Pound => true, // expression attributes
523523
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
524524
NtExpr(..) |
525-
NtBlock(..) |
526-
NtPath(..)),
525+
NtBlock(..)),
527526
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
528527
NonterminalKind::Block |
529528
NonterminalKind::Expr |
@@ -550,9 +549,7 @@ impl Token {
550549
| DotDot | DotDotDot | DotDotEq // ranges
551550
| Lt | BinOp(Shl) // associated path
552551
| PathSep => true, // global path
553-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
554-
NtBlock(..) |
555-
NtPath(..)),
552+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtBlock(..)),
556553
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
557554
NonterminalKind::Block |
558555
NonterminalKind::PatParam { .. } |
@@ -579,7 +576,6 @@ impl Token {
579576
Lifetime(..) | // lifetime bound in trait object
580577
Lt | BinOp(Shl) | // associated path
581578
PathSep => true, // global path
582-
Interpolated(ref nt) => matches!(&nt.0, NtPath(..)),
583579
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
584580
NonterminalKind::Ty |
585581
NonterminalKind::Path
@@ -725,30 +721,20 @@ impl Token {
725721
self.ident().is_some_and(|(ident, _)| ident.name == name)
726722
}
727723

728-
/// Returns `true` if the token is an interpolated path.
729-
fn is_whole_path(&self) -> bool {
730-
// njn: nothing needed, just deal with NtPath later
731-
if let Interpolated(nt) = &self.kind
732-
&& let NtPath(..) = &nt.0
733-
{
734-
return true;
735-
}
736-
737-
false
738-
}
739-
740724
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
741725
/// That is, is this a pre-parsed expression dropped into the token stream
742726
/// (which happens while parsing the result of macro expansion)?
743727
pub fn is_whole_expr(&self) -> bool {
744728
// njn: nothing needed, just deal with NtExpr/NtLiteral/NtPath/NtBlock later
745729
if let Interpolated(nt) = &self.kind
746-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &nt.0
730+
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &nt.0
747731
{
748-
return true;
732+
true
733+
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
734+
true
735+
} else {
736+
false
749737
}
750-
751-
false
752738
}
753739

754740
/// Is the token an interpolated block (`$b:block`)?
@@ -775,7 +761,7 @@ impl Token {
775761
pub fn is_path_start(&self) -> bool {
776762
self == &PathSep
777763
|| self.is_qpath_start()
778-
|| self.is_whole_path()
764+
|| matches!(self.is_metavar_seq(), Some(NonterminalKind::Path))
779765
|| self.is_path_segment_keyword()
780766
|| self.is_ident() && !self.is_reserved_ident()
781767
}
@@ -929,7 +915,6 @@ pub enum Nonterminal {
929915
NtIdent(Ident, IdentIsRaw),
930916
NtLifetime(Ident),
931917
NtLiteral(P<ast::Expr>),
932-
NtPath(P<ast::Path>),
933918
}
934919

935920
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1016,7 +1001,6 @@ impl Nonterminal {
10161001
NtBlock(block) => block.span,
10171002
NtExpr(expr) | NtLiteral(expr) => expr.span,
10181003
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
1019-
NtPath(path) => path.span,
10201004
}
10211005
}
10221006

@@ -1027,7 +1011,6 @@ impl Nonterminal {
10271011
NtLiteral(..) => "literal",
10281012
NtIdent(..) => "identifier",
10291013
NtLifetime(..) => "lifetime",
1030-
NtPath(..) => "path",
10311014
}
10321015
}
10331016
}
@@ -1055,7 +1038,6 @@ impl fmt::Debug for Nonterminal {
10551038
NtExpr(..) => f.pad("NtExpr(..)"),
10561039
NtIdent(..) => f.pad("NtIdent(..)"),
10571040
NtLiteral(..) => f.pad("NtLiteral(..)"),
1058-
NtPath(..) => f.pad("NtPath(..)"),
10591041
NtLifetime(..) => f.pad("NtLifetime(..)"),
10601042
}
10611043
}

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
@@ -45,25 +45,29 @@ use thin_vec::{thin_vec, ThinVec};
4545
macro_rules! maybe_whole_expr {
4646
($p:expr) => {
4747
// njn: nothing needed here due to Nt* matches
48+
let span = $p.token.span;
4849
if let token::Interpolated(nt) = &$p.token.kind {
4950
match &nt.0 {
5051
token::NtExpr(e) | token::NtLiteral(e) => {
5152
let e = e.clone();
5253
$p.bump();
5354
return Ok(e);
5455
}
55-
token::NtPath(path) => {
56-
let path = (**path).clone();
57-
$p.bump();
58-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Path(None, path)));
59-
}
6056
token::NtBlock(block) => {
6157
let block = block.clone();
6258
$p.bump();
6359
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6460
}
6561
_ => {}
6662
};
63+
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
64+
$p,
65+
token::NonterminalKind::Path,
66+
token::NonterminalKind::Path,
67+
super::ParseNtResult::Path(path),
68+
path
69+
) {
70+
return Ok($p.mk_expr(span, ExprKind::Path(None, path.into_inner())));
6771
}
6872
};
6973
}

compiler/rustc_parse/src/parser/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,7 @@ pub enum ParseNtResult<NtType> {
16861686
PatWithOr(P<ast::Pat>),
16871687
Ty(P<ast::Ty>),
16881688
Meta(P<ast::AttrItem>),
1689+
Path(P<ast::Path>),
16891690
Vis(P<ast::Visibility>),
16901691

16911692
/// This variant will eventually be removed, along with `Token::Interpolate`.
@@ -1705,6 +1706,7 @@ impl<T> ParseNtResult<T> {
17051706
ParseNtResult::PatWithOr(x) => ParseNtResult::PatWithOr(x),
17061707
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
17071708
ParseNtResult::Meta(x) => ParseNtResult::Meta(x),
1709+
ParseNtResult::Path(x) => ParseNtResult::Path(x),
17081710
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
17091711
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
17101712
}

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)