Skip to content

Commit fc289d3

Browse files
committed
Auto merge of rust-lang#137517 - nnethercote:rm-NtPat-NtItem-NtStmt, r=<try>
Remove `NtPat`, `NtMeta`, and `NtPath` Another part of rust-lang#124141. r? `@petrochenkov`
2 parents f5729cf + 5caaeba commit fc289d3

35 files changed

+191
-237
lines changed

compiler/rustc_ast/src/ast_traits.rs

-6
Original file line numberDiff line numberDiff line change
@@ -202,9 +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::NtPat(pat) => pat.tokens(),
206-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
207-
Nonterminal::NtPath(path) => path.tokens(),
208205
Nonterminal::NtBlock(block) => block.tokens(),
209206
}
210207
}
@@ -213,9 +210,6 @@ impl HasTokens for Nonterminal {
213210
Nonterminal::NtItem(item) => item.tokens_mut(),
214211
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
215212
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
216-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
217-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
218-
Nonterminal::NtPath(path) => path.tokens_mut(),
219213
Nonterminal::NtBlock(block) => block.tokens_mut(),
220214
}
221215
}

compiler/rustc_ast/src/attr/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::ast::{
1414
PathSegment, Safety,
1515
};
1616
use crate::ptr::P;
17-
use crate::token::{self, CommentKind, Delimiter, Token};
17+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};
1818
use crate::tokenstream::{
1919
DelimSpan, LazyAttrTokenStream, Spacing, TokenStream, TokenStreamIter, TokenTree,
2020
};
@@ -405,11 +405,17 @@ 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::NtMeta(item) => return item.meta(item.path.span),
410-
token::Nonterminal::NtPath(path) => (**path).clone(),
411-
_ => return None,
412-
},
408+
Some(TokenTree::Delimited(
409+
_span,
410+
_spacing,
411+
Delimiter::Invisible(InvisibleOrigin::MetaVar(
412+
MetaVarKind::Meta { .. } | MetaVarKind::Path,
413+
)),
414+
_stream,
415+
)) => {
416+
// This path is currently unreachable in the test suite.
417+
unreachable!()
418+
}
413419
Some(TokenTree::Token(
414420
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
415421
_,

compiler/rustc_ast/src/mut_visit.rs

-8
Original file line numberDiff line numberDiff line change
@@ -905,16 +905,8 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
905905
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
906906
})
907907
}),
908-
token::NtPat(pat) => vis.visit_pat(pat),
909908
token::NtExpr(expr) => vis.visit_expr(expr),
910909
token::NtLiteral(expr) => vis.visit_expr(expr),
911-
token::NtMeta(item) => {
912-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
913-
vis.visit_path(path);
914-
visit_attr_args(vis, args);
915-
visit_lazy_tts(vis, tokens);
916-
}
917-
token::NtPath(path) => vis.visit_path(path),
918910
}
919911
}
920912

compiler/rustc_ast/src/token.rs

+13-38
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ pub enum MetaVarKind {
9090
Ident,
9191
Lifetime,
9292
Literal,
93-
Meta,
93+
Meta {
94+
/// Will `AttrItem::meta` succeed on this, if reparsed?
95+
has_meta_form: bool,
96+
},
9497
Path,
9598
Vis,
9699
TT,
@@ -110,7 +113,7 @@ impl fmt::Display for MetaVarKind {
110113
MetaVarKind::Ident => sym::ident,
111114
MetaVarKind::Lifetime => sym::lifetime,
112115
MetaVarKind::Literal => sym::literal,
113-
MetaVarKind::Meta => sym::meta,
116+
MetaVarKind::Meta { .. } => sym::meta,
114117
MetaVarKind::Path => sym::path,
115118
MetaVarKind::Vis => sym::vis,
116119
MetaVarKind::TT => sym::tt,
@@ -621,8 +624,7 @@ impl Token {
621624
matches!(&**nt,
622625
NtBlock(..) |
623626
NtExpr(..) |
624-
NtLiteral(..) |
625-
NtPath(..)
627+
NtLiteral(..)
626628
),
627629
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
628630
MetaVarKind::Block |
@@ -658,14 +660,11 @@ impl Token {
658660
matches!(&**nt,
659661
| NtExpr(..)
660662
| NtLiteral(..)
661-
| NtMeta(..)
662-
| NtPat(..)
663-
| NtPath(..)
664663
),
665664
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
666665
MetaVarKind::Expr { .. } |
667666
MetaVarKind::Literal |
668-
MetaVarKind::Meta |
667+
MetaVarKind::Meta { .. } |
669668
MetaVarKind::Pat(_) |
670669
MetaVarKind::Path |
671670
MetaVarKind::Ty { .. }
@@ -689,7 +688,6 @@ impl Token {
689688
Lifetime(..) | // lifetime bound in trait object
690689
Lt | BinOp(Shl) | // associated path
691690
PathSep => true, // global path
692-
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
693691
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
694692
MetaVarKind::Ty { .. } |
695693
MetaVarKind::Path
@@ -848,27 +846,17 @@ impl Token {
848846
self.ident().is_some_and(|(ident, _)| ident.name == name)
849847
}
850848

851-
/// Returns `true` if the token is an interpolated path.
852-
fn is_whole_path(&self) -> bool {
853-
if let Interpolated(nt) = &self.kind
854-
&& let NtPath(..) = &**nt
855-
{
856-
return true;
857-
}
858-
859-
false
860-
}
861-
862849
/// Is this a pre-parsed expression dropped into the token stream
863850
/// (which happens while parsing the result of macro expansion)?
864851
pub fn is_whole_expr(&self) -> bool {
852+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
865853
if let Interpolated(nt) = &self.kind
866-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
854+
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
867855
{
868-
return true;
856+
true
857+
} else {
858+
matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
869859
}
870-
871-
false
872860
}
873861

874862
/// Is the token an interpolated block (`$b:block`)?
@@ -894,7 +882,7 @@ impl Token {
894882
pub fn is_path_start(&self) -> bool {
895883
self == &PathSep
896884
|| self.is_qpath_start()
897-
|| self.is_whole_path()
885+
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
898886
|| self.is_path_segment_keyword()
899887
|| self.is_ident() && !self.is_reserved_ident()
900888
}
@@ -1075,12 +1063,8 @@ pub enum Nonterminal {
10751063
NtItem(P<ast::Item>),
10761064
NtBlock(P<ast::Block>),
10771065
NtStmt(P<ast::Stmt>),
1078-
NtPat(P<ast::Pat>),
10791066
NtExpr(P<ast::Expr>),
10801067
NtLiteral(P<ast::Expr>),
1081-
/// Stuff inside brackets for attributes
1082-
NtMeta(P<ast::AttrItem>),
1083-
NtPath(P<ast::Path>),
10841068
}
10851069

10861070
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1172,10 +1156,7 @@ impl Nonterminal {
11721156
NtItem(item) => item.span,
11731157
NtBlock(block) => block.span,
11741158
NtStmt(stmt) => stmt.span,
1175-
NtPat(pat) => pat.span,
11761159
NtExpr(expr) | NtLiteral(expr) => expr.span,
1177-
NtMeta(attr_item) => attr_item.span(),
1178-
NtPath(path) => path.span,
11791160
}
11801161
}
11811162

@@ -1184,11 +1165,8 @@ impl Nonterminal {
11841165
NtItem(..) => "item",
11851166
NtBlock(..) => "block",
11861167
NtStmt(..) => "statement",
1187-
NtPat(..) => "pattern",
11881168
NtExpr(..) => "expression",
11891169
NtLiteral(..) => "literal",
1190-
NtMeta(..) => "attribute",
1191-
NtPath(..) => "path",
11921170
}
11931171
}
11941172
}
@@ -1209,11 +1187,8 @@ impl fmt::Debug for Nonterminal {
12091187
NtItem(..) => f.pad("NtItem(..)"),
12101188
NtBlock(..) => f.pad("NtBlock(..)"),
12111189
NtStmt(..) => f.pad("NtStmt(..)"),
1212-
NtPat(..) => f.pad("NtPat(..)"),
12131190
NtExpr(..) => f.pad("NtExpr(..)"),
12141191
NtLiteral(..) => f.pad("NtLiteral(..)"),
1215-
NtMeta(..) => f.pad("NtMeta(..)"),
1216-
NtPath(..) => f.pad("NtPath(..)"),
12171192
}
12181193
}
12191194
}

compiler/rustc_ast/src/tokenstream.rs

-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,6 @@ impl TokenStream {
468468
TokenStream::token_alone(token::Semi, stmt.span)
469469
}
470470
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
471-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
472-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
473-
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
474471
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
475472
}
476473
}

compiler/rustc_attr_parsing/src/parser.rs

+13-33
Original file line numberDiff line numberDiff line change
@@ -474,41 +474,21 @@ impl<'a> MetaItemListParserContext<'a> {
474474

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

compiler/rustc_expand/src/mbe/transcribe.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ pub(super) fn transcribe<'a>(
279279
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
280280
// We wrap the tokens in invisible delimiters, unless they are already wrapped
281281
// in invisible delimiters with the same `MetaVarKind`. Because some proc
282-
// macros can't multiple layers of invisible delimiters of the same
282+
// macros can't handle multiple layers of invisible delimiters of the same
283283
// `MetaVarKind`. This loses some span info, though it hopefully won't matter.
284-
let mut mk_delimited = |mv_kind, mut stream: TokenStream| {
284+
let mut mk_delimited = |mk_span, mv_kind, mut stream: TokenStream| {
285285
if stream.len() == 1 {
286286
let tree = stream.iter().next().unwrap();
287287
if let TokenTree::Delimited(_, _, delim, inner) = tree
@@ -295,6 +295,7 @@ pub(super) fn transcribe<'a>(
295295
// Emit as a token stream within `Delimiter::Invisible` to maintain
296296
// parsing priorities.
297297
marker.visit_span(&mut sp);
298+
with_metavar_spans(|mspans| mspans.insert(mk_span, sp));
298299
// Both the open delim and close delim get the same span, which covers the
299300
// `$foo` in the decl macro RHS.
300301
TokenTree::Delimited(
@@ -322,12 +323,32 @@ pub(super) fn transcribe<'a>(
322323
let kind = token::NtLifetime(*ident, *is_raw);
323324
TokenTree::token_alone(kind, sp)
324325
}
326+
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => mk_delimited(
327+
pat.span,
328+
MetaVarKind::Pat(*pat_kind),
329+
TokenStream::from_ast(pat),
330+
),
325331
MatchedSingle(ParseNtResult::Ty(ty)) => {
326332
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
327-
mk_delimited(MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
333+
mk_delimited(
334+
ty.span,
335+
MetaVarKind::Ty { is_path },
336+
TokenStream::from_ast(ty),
337+
)
338+
}
339+
MatchedSingle(ParseNtResult::Meta(attr_item)) => {
340+
let has_meta_form = attr_item.meta_kind().is_some();
341+
mk_delimited(
342+
attr_item.span(),
343+
MetaVarKind::Meta { has_meta_form },
344+
TokenStream::from_ast(attr_item),
345+
)
346+
}
347+
MatchedSingle(ParseNtResult::Path(path)) => {
348+
mk_delimited(path.span, MetaVarKind::Path, TokenStream::from_ast(path))
328349
}
329350
MatchedSingle(ParseNtResult::Vis(vis)) => {
330-
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
351+
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
331352
}
332353
MatchedSingle(ParseNtResult::Nt(nt)) => {
333354
// Other variables are emitted into the output stream as groups with

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
424424
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
425425
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
426426
427-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
427+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
428428
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
429429
430430
parse_invalid_offset_of = offset_of expects dot-separated field and variant names

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
10251025
pub(crate) struct InvalidMetaItem {
10261026
#[primary_span]
10271027
pub span: Span,
1028-
pub token: Token,
1028+
pub descr: String,
10291029
#[subdiagnostic]
10301030
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
10311031
}

0 commit comments

Comments
 (0)