Skip to content

Commit 156ef2b

Browse files
committed
Attach tokens to ast::Stmt
We currently only attach tokens when parsing a `:stmt` matcher for a `macro_rules!` macro. Proc-macro attributes on statements are still unstable, and need additional work.
1 parent c101116 commit 156ef2b

File tree

11 files changed

+56
-15
lines changed

11 files changed

+56
-15
lines changed

compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ pub struct Stmt {
918918
pub id: NodeId,
919919
pub kind: StmtKind,
920920
pub span: Span,
921+
pub tokens: Option<TokenStream>,
921922
}
922923

923924
impl Stmt {

compiler/rustc_ast/src/mut_visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1286,12 +1286,15 @@ pub fn noop_filter_map_expr<T: MutVisitor>(mut e: P<Expr>, vis: &mut T) -> Optio
12861286
}
12871287

12881288
pub fn noop_flat_map_stmt<T: MutVisitor>(
1289-
Stmt { kind, mut span, mut id }: Stmt,
1289+
Stmt { kind, mut span, mut id, tokens }: Stmt,
12901290
vis: &mut T,
12911291
) -> SmallVec<[Stmt; 1]> {
12921292
vis.visit_id(&mut id);
12931293
vis.visit_span(&mut span);
1294-
noop_flat_map_stmt_kind(kind, vis).into_iter().map(|kind| Stmt { id, kind, span }).collect()
1294+
noop_flat_map_stmt_kind(kind, vis)
1295+
.into_iter()
1296+
.map(|kind| Stmt { id, kind, span, tokens: tokens.clone() })
1297+
.collect()
12951298
}
12961299

12971300
pub fn noop_flat_map_stmt_kind<T: MutVisitor>(

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ fn stmt_let_underscore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> as
133133
span: sp,
134134
attrs: ast::AttrVec::new(),
135135
});
136-
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
136+
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp, tokens: None }
137137
}

compiler/rustc_expand/src/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ macro_rules! make_stmts_default {
400400
id: ast::DUMMY_NODE_ID,
401401
span: e.span,
402402
kind: ast::StmtKind::Expr(e),
403+
tokens: None
403404
}]
404405
})
405406
};
@@ -642,6 +643,7 @@ impl MacResult for DummyResult {
642643
id: ast::DUMMY_NODE_ID,
643644
kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.is_error)),
644645
span: self.span,
646+
tokens: None
645647
}])
646648
}
647649

compiler/rustc_expand/src/build.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ impl<'a> ExtCtxt<'a> {
158158
}
159159

160160
pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
161-
ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Expr(expr) }
161+
ast::Stmt {
162+
id: ast::DUMMY_NODE_ID,
163+
span: expr.span,
164+
kind: ast::StmtKind::Expr(expr),
165+
tokens: None,
166+
}
162167
}
163168

164169
pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: Ident, ex: P<ast::Expr>) -> ast::Stmt {
@@ -176,7 +181,12 @@ impl<'a> ExtCtxt<'a> {
176181
span: sp,
177182
attrs: AttrVec::new(),
178183
});
179-
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
184+
ast::Stmt {
185+
id: ast::DUMMY_NODE_ID,
186+
kind: ast::StmtKind::Local(local),
187+
span: sp,
188+
tokens: None,
189+
}
180190
}
181191

182192
// Generates `let _: Type;`, which is usually used for type assertions.
@@ -189,11 +199,16 @@ impl<'a> ExtCtxt<'a> {
189199
span,
190200
attrs: AttrVec::new(),
191201
});
192-
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span }
202+
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span, tokens: None }
193203
}
194204

195205
pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
196-
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Item(item), span: sp }
206+
ast::Stmt {
207+
id: ast::DUMMY_NODE_ID,
208+
kind: ast::StmtKind::Item(item),
209+
span: sp,
210+
tokens: None,
211+
}
197212
}
198213

199214
pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
@@ -203,6 +218,7 @@ impl<'a> ExtCtxt<'a> {
203218
id: ast::DUMMY_NODE_ID,
204219
span: expr.span,
205220
kind: ast::StmtKind::Expr(expr),
221+
tokens: None,
206222
}],
207223
)
208224
}

compiler/rustc_expand/src/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1396,10 +1396,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13961396
}
13971397

13981398
// The placeholder expander gives ids to statements, so we avoid folding the id here.
1399-
let ast::Stmt { id, kind, span } = stmt;
1399+
let ast::Stmt { id, kind, span, tokens } = stmt;
14001400
noop_flat_map_stmt_kind(kind, self)
14011401
.into_iter()
1402-
.map(|kind| ast::Stmt { id, kind, span })
1402+
.map(|kind| ast::Stmt { id, kind, span, tokens: tokens.clone() })
14031403
.collect()
14041404
}
14051405

compiler/rustc_expand/src/placeholders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn placeholder(
105105
style: ast::MacStmtStyle::Braces,
106106
attrs: ast::AttrVec::new(),
107107
});
108-
ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac) }
108+
ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac), tokens: None }
109109
}]),
110110
AstFragmentKind::Arms => AstFragment::Arms(smallvec![ast::Arm {
111111
attrs: Default::default(),

compiler/rustc_interface/src/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
710710
id: resolver.next_node_id(),
711711
kind: ast::StmtKind::Expr(expr),
712712
span: rustc_span::DUMMY_SP,
713+
tokens: None,
713714
}
714715
}
715716

@@ -726,6 +727,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
726727
id: self.resolver.next_node_id(),
727728
span: rustc_span::DUMMY_SP,
728729
kind: ast::StmtKind::Expr(loop_expr),
730+
tokens: None,
729731
};
730732

731733
if self.within_static_or_const {

compiler/rustc_parse/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
269269
prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span)
270270
}
271271
Nonterminal::NtBlock(ref block) => block.tokens.clone(),
272+
Nonterminal::NtStmt(ref stmt) => {
273+
// FIXME: We currently only collect tokens for `:stmt`
274+
// matchers in `macro_rules!` macros. When we start collecting
275+
// tokens for attributes on statements, we will need to prepend
276+
// attributes here
277+
stmt.tokens.clone()
278+
}
272279
Nonterminal::NtPat(ref pat) => pat.tokens.clone(),
273280
Nonterminal::NtTy(ref ty) => ty.tokens.clone(),
274281
Nonterminal::NtIdent(ident, is_raw) => {

compiler/rustc_parse/src/parser/nonterminal.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,20 @@ impl<'a> Parser<'a> {
119119
}
120120
token::NtBlock(block)
121121
}
122-
NonterminalKind::Stmt => match self.parse_stmt()? {
123-
Some(s) => token::NtStmt(s),
124-
None => return Err(self.struct_span_err(self.token.span, "expected a statement")),
125-
},
122+
NonterminalKind::Stmt => {
123+
let (stmt, tokens) = self.collect_tokens(|this| this.parse_stmt())?;
124+
match stmt {
125+
Some(mut s) => {
126+
if s.tokens.is_none() {
127+
s.tokens = Some(tokens);
128+
}
129+
token::NtStmt(s)
130+
}
131+
None => {
132+
return Err(self.struct_span_err(self.token.span, "expected a statement"));
133+
}
134+
}
135+
}
126136
NonterminalKind::Pat => {
127137
let (mut pat, tokens) = self.collect_tokens(|this| this.parse_pat(None))?;
128138
// We have have eaten an NtPat, which could already have tokens

compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'a> Parser<'a> {
415415
}
416416

417417
pub(super) fn mk_stmt(&self, span: Span, kind: StmtKind) -> Stmt {
418-
Stmt { id: DUMMY_NODE_ID, kind, span }
418+
Stmt { id: DUMMY_NODE_ID, kind, span, tokens: None }
419419
}
420420

421421
fn mk_stmt_err(&self, span: Span) -> Stmt {

0 commit comments

Comments
 (0)