Skip to content

Commit

Permalink
Attach TokenStream to ast::Block
Browse files Browse the repository at this point in the history
A `Block` does not have outer attributes, so we only capture tokens when
parsing a `macro_rules!` matcher
  • Loading branch information
Aaron1011 committed Sep 10, 2020
1 parent ad3a6f7 commit de4bd9f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/ast.rs
Expand Up @@ -540,6 +540,7 @@ pub struct Block {
/// Distinguishes between `unsafe { ... }` and `{ ... }`.
pub rules: BlockCheckMode,
pub span: Span,
pub tokens: Option<TokenStream>,
}

/// A match pattern.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Expand Up @@ -871,7 +871,7 @@ pub fn noop_visit_mt<T: MutVisitor>(MutTy { ty, mutbl: _ }: &mut MutTy, vis: &mu
}

pub fn noop_visit_block<T: MutVisitor>(block: &mut P<Block>, vis: &mut T) {
let Block { id, stmts, rules: _, span } = block.deref_mut();
let Block { id, stmts, rules: _, span, tokens: _ } = block.deref_mut();
vis.visit_id(id);
stmts.flat_map_in_place(|stmt| vis.flat_map_stmt(stmt));
vis.visit_span(span);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/deriving/mod.rs
Expand Up @@ -75,6 +75,7 @@ fn call_intrinsic(
id: ast::DUMMY_NODE_ID,
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
span,
tokens: None,
}))
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_expand/src/build.rs
Expand Up @@ -207,7 +207,13 @@ impl<'a> ExtCtxt<'a> {
)
}
pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
P(ast::Block { stmts, id: ast::DUMMY_NODE_ID, rules: BlockCheckMode::Default, span })
P(ast::Block {
stmts,
id: ast::DUMMY_NODE_ID,
rules: BlockCheckMode::Default,
span,
tokens: None,
})
}

pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/util.rs
Expand Up @@ -693,6 +693,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
rules,
id: resolver.next_node_id(),
span: rustc_span::DUMMY_SP,
tokens: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/lib.rs
Expand Up @@ -268,6 +268,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
Nonterminal::NtItem(ref item) => {
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
}
Nonterminal::NtBlock(ref block) => block.tokens.clone(),
Nonterminal::NtPat(ref pat) => pat.tokens.clone(),
Nonterminal::NtIdent(ident, is_raw) => {
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_parse/src/parser/nonterminal.rs
Expand Up @@ -111,7 +111,14 @@ impl<'a> Parser<'a> {
return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
}
},
NonterminalKind::Block => token::NtBlock(self.parse_block()?),
NonterminalKind::Block => {
let (mut block, tokens) = self.collect_tokens(|this| this.parse_block())?;
// We have have eaten an NtBlock, which could already have tokens
if block.tokens.is_none() {
block.tokens = Some(tokens);
}
token::NtBlock(block)
}
NonterminalKind::Stmt => match self.parse_stmt()? {
Some(s) => token::NtStmt(s),
None => return Err(self.struct_span_err(self.token.span, "expected a statement")),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Expand Up @@ -411,7 +411,7 @@ impl<'a> Parser<'a> {
}

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

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

0 comments on commit de4bd9f

Please sign in to comment.