Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit de4bd9f

Browse files
committed
Attach TokenStream to ast::Block
A `Block` does not have outer attributes, so we only capture tokens when parsing a `macro_rules!` matcher
1 parent ad3a6f7 commit de4bd9f

File tree

8 files changed

+21
-4
lines changed

8 files changed

+21
-4
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ pub struct Block {
540540
/// Distinguishes between `unsafe { ... }` and `{ ... }`.
541541
pub rules: BlockCheckMode,
542542
pub span: Span,
543+
pub tokens: Option<TokenStream>,
543544
}
544545

545546
/// A match pattern.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ pub fn noop_visit_mt<T: MutVisitor>(MutTy { ty, mutbl: _ }: &mut MutTy, vis: &mu
871871
}
872872

873873
pub fn noop_visit_block<T: MutVisitor>(block: &mut P<Block>, vis: &mut T) {
874-
let Block { id, stmts, rules: _, span } = block.deref_mut();
874+
let Block { id, stmts, rules: _, span, tokens: _ } = block.deref_mut();
875875
vis.visit_id(id);
876876
stmts.flat_map_in_place(|stmt| vis.flat_map_stmt(stmt));
877877
vis.visit_span(span);

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fn call_intrinsic(
7575
id: ast::DUMMY_NODE_ID,
7676
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
7777
span,
78+
tokens: None,
7879
}))
7980
}
8081

compiler/rustc_expand/src/build.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ impl<'a> ExtCtxt<'a> {
207207
)
208208
}
209209
pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
210-
P(ast::Block { stmts, id: ast::DUMMY_NODE_ID, rules: BlockCheckMode::Default, span })
210+
P(ast::Block {
211+
stmts,
212+
id: ast::DUMMY_NODE_ID,
213+
rules: BlockCheckMode::Default,
214+
span,
215+
tokens: None,
216+
})
211217
}
212218

213219
pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {

compiler/rustc_interface/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
693693
rules,
694694
id: resolver.next_node_id(),
695695
span: rustc_span::DUMMY_SP,
696+
tokens: None,
696697
}
697698
}
698699

compiler/rustc_parse/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
268268
Nonterminal::NtItem(ref item) => {
269269
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
270270
}
271+
Nonterminal::NtBlock(ref block) => block.tokens.clone(),
271272
Nonterminal::NtPat(ref pat) => pat.tokens.clone(),
272273
Nonterminal::NtIdent(ident, is_raw) => {
273274
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ impl<'a> Parser<'a> {
111111
return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
112112
}
113113
},
114-
NonterminalKind::Block => token::NtBlock(self.parse_block()?),
114+
NonterminalKind::Block => {
115+
let (mut block, tokens) = self.collect_tokens(|this| this.parse_block())?;
116+
// We have have eaten an NtBlock, which could already have tokens
117+
if block.tokens.is_none() {
118+
block.tokens = Some(tokens);
119+
}
120+
token::NtBlock(block)
121+
}
115122
NonterminalKind::Stmt => match self.parse_stmt()? {
116123
Some(s) => token::NtStmt(s),
117124
None => return Err(self.struct_span_err(self.token.span, "expected a statement")),

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl<'a> Parser<'a> {
411411
}
412412

413413
pub(super) fn mk_block(&self, stmts: Vec<Stmt>, rules: BlockCheckMode, span: Span) -> P<Block> {
414-
P(Block { stmts, id: DUMMY_NODE_ID, rules, span })
414+
P(Block { stmts, id: DUMMY_NODE_ID, rules, span, tokens: None })
415415
}
416416

417417
pub(super) fn mk_stmt(&self, span: Span, kind: StmtKind) -> Stmt {

0 commit comments

Comments
 (0)