Skip to content

Commit 09d0cd3

Browse files
committed
Remove NtPath.
1 parent 92d4c9f commit 09d0cd3

File tree

16 files changed

+59
-57
lines changed

16 files changed

+59
-57
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,14 @@ impl MetaItem {
326326
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
327327
Path { span, segments, tokens: None }
328328
}
329-
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
330-
token::Nonterminal::NtPath(path) => (**path).clone(),
331-
_ => return None,
332-
},
333329
Some(TokenTree::Delimited(
334330
_span,
335-
Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Meta)),
331+
Delimiter::Invisible(InvisibleSource::MetaVar(
332+
NonterminalKind::Meta | NonterminalKind::Path,
333+
)),
336334
_stream,
337335
)) => {
338-
// njn: this pre-existing (equivalent) path is unreachable in the test suite
336+
// njn: these pre-existing (equivalent) paths are unreachable in the test suite
339337
unreachable!()
340338
}
341339
_ => return None,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
794794
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
795795
token::NtLifetime(ident) => vis.visit_ident(ident),
796796
token::NtLiteral(expr) => vis.visit_expr(expr),
797-
token::NtPath(path) => vis.visit_path(path),
798797
}
799798
}
800799

compiler/rustc_ast/src/token.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ impl Token {
471471
Pound => true, // expression attributes
472472
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
473473
NtExpr(..) |
474-
NtBlock(..) |
475-
NtPath(..)),
474+
NtBlock(..)),
475+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Path)))
476+
=> true,
476477
_ => false,
477478
}
478479
}
@@ -493,9 +494,11 @@ impl Token {
493494
| DotDot | DotDotDot | DotDotEq // ranges
494495
| Lt | BinOp(Shl) // associated path
495496
| ModSep => true, // global path
496-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtBlock(..) | NtPath(..)),
497+
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtBlock(..)),
497498
| OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
498-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr
499+
NonterminalKind::PatParam { .. } |
500+
NonterminalKind::PatWithOr |
501+
NonterminalKind::Path
499502
))) => true,
500503
_ => false,
501504
}
@@ -516,8 +519,10 @@ impl Token {
516519
Lifetime(..) | // lifetime bound in trait object
517520
Lt | BinOp(Shl) | // associated path
518521
ModSep => true, // global path
519-
Interpolated(ref nt) => matches!(**nt, NtPath(..)),
520-
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Ty))) => true,
522+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
523+
NonterminalKind::Ty |
524+
NonterminalKind::Path
525+
))) => true,
521526
_ => false,
522527
}
523528
}
@@ -656,26 +661,19 @@ impl Token {
656661
self.ident().is_some_and(|(ident, _)| ident.name == name)
657662
}
658663

659-
/// Returns `true` if the token is an interpolated path.
660-
fn is_path(&self) -> bool {
661-
if let Interpolated(nt) = &self.kind && let NtPath(..) = **nt {
662-
return true;
663-
}
664-
665-
false
666-
}
667-
668664
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
669665
/// That is, is this a pre-parsed expression dropped into the token stream
670666
/// (which happens while parsing the result of macro expansion)?
671667
pub fn is_whole_expr(&self) -> bool {
672668
if let Interpolated(nt) = &self.kind
673-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt
669+
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = **nt
674670
{
675-
return true;
671+
true
672+
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
673+
true
674+
} else {
675+
false
676676
}
677-
678-
false
679677
}
680678

681679
/// Is the token an interpolated block (`$b:block`)?
@@ -699,7 +697,7 @@ impl Token {
699697
pub fn is_path_start(&self) -> bool {
700698
self == &ModSep
701699
|| self.is_qpath_start()
702-
|| self.is_path()
700+
|| matches!(self.is_metavar_seq(), Some(NonterminalKind::Path))
703701
|| self.is_path_segment_keyword()
704702
|| self.is_ident() && !self.is_reserved_ident()
705703
}
@@ -847,7 +845,6 @@ pub enum Nonterminal {
847845
NtIdent(Ident, /* is_raw */ bool),
848846
NtLifetime(Ident),
849847
NtLiteral(P<ast::Expr>),
850-
NtPath(P<ast::Path>),
851848
}
852849

853850
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -934,7 +931,6 @@ impl Nonterminal {
934931
NtBlock(block) => block.span,
935932
NtExpr(expr) | NtLiteral(expr) => expr.span,
936933
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
937-
NtPath(path) => path.span,
938934
}
939935
}
940936
}
@@ -962,7 +958,6 @@ impl fmt::Debug for Nonterminal {
962958
NtExpr(..) => f.pad("NtExpr(..)"),
963959
NtIdent(..) => f.pad("NtIdent(..)"),
964960
NtLiteral(..) => f.pad("NtLiteral(..)"),
965-
NtPath(..) => f.pad("NtPath(..)"),
966961
NtLifetime(..) => f.pad("NtLifetime(..)"),
967962
}
968963
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ impl TokenStream {
464464
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
465465
}
466466
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
467-
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
468467
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
469468
}
470469
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
727727
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
728728
match nt {
729729
token::NtExpr(e) => self.expr_to_string(e),
730-
token::NtPath(e) => self.path_to_string(e),
731730
token::NtBlock(e) => self.block_to_string(e),
732731
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
733732
token::NtLifetime(e) => e.to_string(),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ pub(super) fn transcribe<'a>(
258258
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
259259
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
260260
}
261+
MatchedSingle(ParseNtResult::Path(ref path)) => {
262+
mk_delimited(NonterminalKind::Path, TokenStream::from_ast(path))
263+
}
261264
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
262265
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
263266
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,29 @@ use thin_vec::{thin_vec, ThinVec};
4242
/// `token::Interpolated` tokens.
4343
macro_rules! maybe_whole_expr {
4444
($p:expr) => {
45+
let span = $p.token.span;
4546
if let token::Interpolated(nt) = &$p.token.kind {
4647
match &**nt {
4748
token::NtExpr(e) | token::NtLiteral(e) => {
4849
let e = e.clone();
4950
$p.bump();
5051
return Ok(e);
5152
}
52-
token::NtPath(path) => {
53-
let path = (**path).clone();
54-
$p.bump();
55-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Path(None, path)));
56-
}
5753
token::NtBlock(block) => {
5854
let block = block.clone();
5955
$p.bump();
6056
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6157
}
6258
_ => {}
6359
};
60+
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
61+
$p,
62+
token::NonterminalKind::Path,
63+
token::NonterminalKind::Path,
64+
super::ParseNtResult::Path(path),
65+
path
66+
) {
67+
return Ok($p.mk_expr(span, ExprKind::Path(None, path.into_inner())));
6468
}
6569
};
6670
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ pub enum ParseNtResult {
15881588
PatWithOr(P<ast::Pat>),
15891589
Ty(P<ast::Ty>),
15901590
Meta(P<ast::AttrItem>),
1591+
Path(P<ast::Path>),
15911592
Vis(P<ast::Visibility>),
15921593

15931594
/// This case will eventually be removed, along with `Token::Interpolate`.

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 4 additions & 4 deletions
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 {
8080
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
81-
NtIdent(..) | NtPath(_) => false,
81+
NtIdent(..) => false,
8282
},
8383
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(k))) => match k {
8484
NonterminalKind::Block
@@ -213,9 +213,9 @@ impl<'a> Parser<'a> {
213213
token: self.token.clone(),
214214
}.into_diagnostic(&self.sess.span_diagnostic));
215215
}
216-
NonterminalKind::Path => NtPath(
216+
NonterminalKind::Path => return Ok(ParseNtResult::Path(
217217
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
218-
),
218+
)),
219219
NonterminalKind::Meta => return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?))),
220220
NonterminalKind::Vis => return Ok(ParseNtResult::Vis(
221221
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),

compiler/rustc_parse/src/parser/path.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
22
use super::{ParseNtResult, Parser, Restrictions, TokenType};
33
use crate::errors::PathSingleColon;
4-
use crate::{errors, maybe_whole};
4+
use crate::{errors, maybe_reparse_metavar_seq};
55
use rustc_ast::ptr::P;
66
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
77
use rustc_ast::{
@@ -179,10 +179,16 @@ impl<'a> Parser<'a> {
179179
}
180180
};
181181

182-
maybe_whole!(self, NtPath, |path| {
182+
if let Some(path) = maybe_reparse_metavar_seq!(
183+
self,
184+
NonterminalKind::Path,
185+
NonterminalKind::Path,
186+
ParseNtResult::Path(path),
187+
path
188+
) {
183189
reject_generics_if_mod_style(self, &path);
184-
path.into_inner()
185-
});
190+
return Ok(path.into_inner());
191+
}
186192

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

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

Lines changed: 1 addition & 1 deletion
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 invisible open delimiter
1212
}
1313

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected identifier, found `a::b::c`
1+
error: expected identifier, found invisible open delimiter
22
--> $DIR/import-prefix-macro-2.rs:11:26
33
|
44
LL | ($p: path) => (use ::$p {S, Z});

tests/ui/macros/issue-8709.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ macro_rules! spath {
1010

1111
fn main() {
1212
assert_eq!(sty!(isize), "isize");
13-
assert_eq!(spath!(std::option), "std::option");
13+
assert_eq!(spath!(std::option), "std :: option");
1414
}

tests/ui/macros/stringify.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,12 @@ fn test_pat() {
734734
#[test]
735735
fn test_path() {
736736
assert_eq!(stringify_path!(thing), "thing");
737-
assert_eq!(stringify_path!(m::thing), "m::thing");
738-
assert_eq!(stringify_path!(self::thing), "self::thing");
739-
assert_eq!(stringify_path!(crate::thing), "crate::thing");
740-
assert_eq!(stringify_path!(Self::thing), "Self::thing");
741-
assert_eq!(stringify_path!(Self<'static>), "Self<'static>");
742-
assert_eq!(stringify_path!(Self::<'static>), "Self<'static>");
737+
assert_eq!(stringify_path!(m::thing), "m :: thing");
738+
assert_eq!(stringify_path!(self::thing), "self :: thing");
739+
assert_eq!(stringify_path!(crate::thing), "crate :: thing");
740+
assert_eq!(stringify_path!(Self::thing), "Self :: thing");
741+
assert_eq!(stringify_path!(Self<'static>), "Self < 'static >");
742+
assert_eq!(stringify_path!(Self::<'static>), "Self :: < 'static >"); // njn: added `::`
743743
assert_eq!(stringify_path!(Self()), "Self()");
744744
assert_eq!(stringify_path!(Self() -> ()), "Self() -> ()");
745745
}

tests/ui/proc-macro/capture-macro-rules-invoke.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
1212
},
1313
]
1414
PRINT-BANG INPUT (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
15-
std::option::Option, pub(in some :: path), [a b c], -30
15+
std :: option :: Option, pub(in some :: path), [a b c], -30
1616
PRINT-BANG RE-COLLECTED (DISPLAY): 1 + 1, { "a" }, let a = 1, String, my_name, 'a, my_val = 30,
1717
std :: option :: Option, pub(in some :: path), [a b c], - 30
1818
PRINT-BANG INPUT (DEBUG): TokenStream [

0 commit comments

Comments
 (0)