Skip to content

Commit 1202034

Browse files
Mac calls include their semicolons
1 parent e5e79f8 commit 1202034

File tree

40 files changed

+134
-173
lines changed

40 files changed

+134
-173
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ impl Stmt {
11851185
pub fn has_trailing_semicolon(&self) -> bool {
11861186
match &self.kind {
11871187
StmtKind::Semi(_) => true,
1188-
StmtKind::MacCall(mac) => matches!(mac.style, MacStmtStyle::Semicolon),
1188+
StmtKind::MacCall(mac) => matches!(mac.style, MacStmtStyle::Semicolon(_)),
11891189
_ => false,
11901190
}
11911191
}
@@ -1197,11 +1197,14 @@ impl Stmt {
11971197
/// `LazyAttrTokenStream`. The parser is responsible for calling
11981198
/// `ToAttrTokenStream::add_trailing_semi` when there is actually
11991199
/// a semicolon in the tokenstream.
1200-
pub fn add_trailing_semicolon(mut self) -> Self {
1200+
pub fn add_trailing_semicolon(mut self, semi_span: Span) -> Self {
1201+
if let Some(mac_span) = self.span.find_ancestor_in_same_ctxt(semi_span) {
1202+
self.span = mac_span.to(semi_span);
1203+
}
12011204
self.kind = match self.kind {
12021205
StmtKind::Expr(expr) => StmtKind::Semi(expr),
12031206
StmtKind::MacCall(mut mac) => {
1204-
mac.style = MacStmtStyle::Semicolon;
1207+
mac.style = MacStmtStyle::Semicolon(semi_span);
12051208
StmtKind::MacCall(mac)
12061209
}
12071210
kind => kind,
@@ -1231,11 +1234,17 @@ pub enum StmtKind {
12311234
/// Expr with a trailing semi-colon.
12321235
Semi(P<Expr>),
12331236
/// Just a trailing semi-colon.
1234-
Empty,
1237+
Empty(EmptyFromMacro),
12351238
/// Macro.
12361239
MacCall(P<MacCallStmt>),
12371240
}
12381241

1242+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
1243+
pub enum EmptyFromMacro {
1244+
Yes,
1245+
No,
1246+
}
1247+
12391248
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
12401249
pub struct MacCallStmt {
12411250
pub mac: P<MacCall>,
@@ -1248,7 +1257,7 @@ pub struct MacCallStmt {
12481257
pub enum MacStmtStyle {
12491258
/// The macro statement had a trailing semicolon (e.g., `foo! { ... };`
12501259
/// `foo!(...);`, `foo![...];`).
1251-
Semicolon,
1260+
Semicolon(Span),
12521261
/// The macro statement had braces (e.g., `foo! { ... }`).
12531262
Braces,
12541263
/// The macro statement had parentheses or brackets and no semicolon (e.g.,

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl HasTokens for StmtKind {
134134
StmtKind::Let(local) => local.tokens.as_ref(),
135135
StmtKind::Item(item) => item.tokens(),
136136
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.tokens(),
137-
StmtKind::Empty => None,
137+
StmtKind::Empty(_) => None,
138138
StmtKind::MacCall(mac) => mac.tokens.as_ref(),
139139
}
140140
}
@@ -143,7 +143,7 @@ impl HasTokens for StmtKind {
143143
StmtKind::Let(local) => Some(&mut local.tokens),
144144
StmtKind::Item(item) => item.tokens_mut(),
145145
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.tokens_mut(),
146-
StmtKind::Empty => None,
146+
StmtKind::Empty(_) => None,
147147
StmtKind::MacCall(mac) => Some(&mut mac.tokens),
148148
}
149149
}
@@ -277,7 +277,7 @@ impl HasAttrs for StmtKind {
277277
StmtKind::Let(local) => &local.attrs,
278278
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.attrs(),
279279
StmtKind::Item(item) => item.attrs(),
280-
StmtKind::Empty => &[],
280+
StmtKind::Empty(_) => &[],
281281
StmtKind::MacCall(mac) => &mac.attrs,
282282
}
283283
}
@@ -287,7 +287,7 @@ impl HasAttrs for StmtKind {
287287
StmtKind::Let(local) => f(&mut local.attrs),
288288
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.visit_attrs(f),
289289
StmtKind::Item(item) => item.visit_attrs(f),
290-
StmtKind::Empty => {}
290+
StmtKind::Empty(_) => {}
291291
StmtKind::MacCall(mac) => f(&mut mac.attrs),
292292
}
293293
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallV
372372
StmtKind::Item(item) => vis.flat_map_item(item).into_iter().map(StmtKind::Item).collect(),
373373
StmtKind::Expr(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Expr).collect(),
374374
StmtKind::Semi(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Semi).collect(),
375-
StmtKind::Empty => smallvec![StmtKind::Empty],
375+
StmtKind::Empty(from_macro) => smallvec![StmtKind::Empty(from_macro)],
376376
StmtKind::MacCall(mut mac) => {
377377
let MacCallStmt { mac: mac_, style: _, attrs, tokens: _ } = mac.deref_mut();
378378
for attr in attrs {

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ macro_rules! common_visitor_and_walkers {
428428
DelegationMac,
429429
DelimArgs,
430430
DelimSpan,
431+
EmptyFromMacro,
431432
EnumDef,
432433
Extern,
433434
ForLoopKind,
@@ -1166,7 +1167,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V:
11661167
StmtKind::Let(local) => try_visit!(visitor.visit_local(local)),
11671168
StmtKind::Item(item) => try_visit!(visitor.visit_item(item)),
11681169
StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(visitor.visit_expr(expr)),
1169-
StmtKind::Empty => {}
1170+
StmtKind::Empty(_) => {}
11701171
StmtKind::MacCall(mac) => {
11711172
let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
11721173
walk_list!(visitor, visit_attribute, attrs);

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7575
let span = self.lower_span(s.span);
7676
stmts.push(hir::Stmt { hir_id, kind, span });
7777
}
78-
StmtKind::Empty => {}
78+
StmtKind::Empty(_) => {}
7979
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
8080
}
8181
ast_stmts = tail;

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,15 +1446,15 @@ impl<'a> State<'a> {
14461446
self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
14471447
self.word(";");
14481448
}
1449-
ast::StmtKind::Empty => {
1449+
ast::StmtKind::Empty(_) => {
14501450
self.space_if_not_bol();
14511451
self.word(";");
14521452
}
14531453
ast::StmtKind::MacCall(mac) => {
14541454
self.space_if_not_bol();
14551455
self.print_outer_attributes(&mac.attrs);
14561456
self.print_mac(&mac.mac);
1457-
if mac.style == ast::MacStmtStyle::Semicolon {
1457+
if matches!(mac.style, ast::MacStmtStyle::Semicolon(_)) {
14581458
self.word(";");
14591459
}
14601460
}

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
393393
)],
394394
self.span,
395395
))
396-
.add_trailing_semicolon();
396+
.add_trailing_semicolon(self.span.shrink_to_hi());
397397
let local_bind_path = self.cx.expr_path(Path::from_ident(local_bind));
398398
let rslt = if self.is_consumed {
399399
let ret = self.cx.stmt_expr(local_bind_path);

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Annotatable {
124124
Annotatable::AssocItem(node, _) => TokenStream::from_ast(node),
125125
Annotatable::ForeignItem(node) => TokenStream::from_ast(node),
126126
Annotatable::Stmt(node) => {
127-
assert!(!matches!(node.kind, ast::StmtKind::Empty));
127+
assert!(!matches!(node.kind, ast::StmtKind::Empty(_)));
128128
TokenStream::from_ast(node)
129129
}
130130
Annotatable::Expr(node) => TokenStream::from_ast(node),

compiler/rustc_expand/src/expand.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ macro_rules! assign_id {
12041204
}
12051205

12061206
enum AddSemicolon {
1207-
Yes,
1207+
Yes(Span),
12081208
No,
12091209
}
12101210

@@ -1687,7 +1687,7 @@ impl InvocationCollectorNode for ast::Stmt {
16871687
StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
16881688
StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
16891689
StmtKind::Expr(..) => unreachable!(),
1690-
StmtKind::Let(..) | StmtKind::Empty => false,
1690+
StmtKind::Let(..) | StmtKind::Empty(_) => false,
16911691
}
16921692
}
16931693
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
@@ -1696,23 +1696,32 @@ impl InvocationCollectorNode for ast::Stmt {
16961696
let (add_semicolon, mac, attrs) = match self.kind {
16971697
StmtKind::MacCall(mac) => {
16981698
let ast::MacCallStmt { mac, style, attrs, .. } = *mac;
1699-
(style == MacStmtStyle::Semicolon, mac, attrs)
1699+
let add_semicolon = match style {
1700+
MacStmtStyle::Semicolon(span) => AddSemicolon::Yes(span),
1701+
MacStmtStyle::Braces | MacStmtStyle::NoBraces => AddSemicolon::No,
1702+
};
1703+
(add_semicolon, mac, attrs)
17001704
}
17011705
StmtKind::Item(item) => match *item {
17021706
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1703-
(mac.args.need_semicolon(), mac, attrs)
1707+
(AddSemicolon::No, mac, attrs)
17041708
}
17051709
_ => unreachable!(),
17061710
},
17071711
StmtKind::Semi(expr) => match *expr {
1708-
ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
1709-
(mac.args.need_semicolon(), mac, attrs)
1712+
ast::Expr { kind: ExprKind::MacCall(mac), attrs, span: expr_span, .. } => {
1713+
let add_semicolon = if mac.args.need_semicolon() {
1714+
AddSemicolon::Yes(expr_span.shrink_to_hi().to(self.span))
1715+
} else {
1716+
AddSemicolon::No
1717+
};
1718+
(add_semicolon, mac, attrs)
17101719
}
17111720
_ => unreachable!(),
17121721
},
17131722
_ => unreachable!(),
17141723
};
1715-
(mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
1724+
(mac, attrs, add_semicolon)
17161725
}
17171726
fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
17181727
match &self.kind {
@@ -1735,9 +1744,9 @@ impl InvocationCollectorNode for ast::Stmt {
17351744
fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
17361745
// If this is a macro invocation with a semicolon, then apply that
17371746
// semicolon to the final statement produced by expansion.
1738-
if matches!(add_semicolon, AddSemicolon::Yes) {
1747+
if let AddSemicolon::Yes(semi_span) = add_semicolon {
17391748
if let Some(stmt) = stmts.pop() {
1740-
stmts.push(stmt.add_trailing_semicolon());
1749+
stmts.push(stmt.add_trailing_semicolon(semi_span));
17411750
}
17421751
}
17431752
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ fn transcribe_metavar<'tx>(
431431
mk_delimited(block.span, MetaVarKind::Block, TokenStream::from_ast(block))
432432
}
433433
MatchedSingle(ParseNtResult::Stmt(stmt)) => {
434-
let stream = if let StmtKind::Empty = stmt.kind {
434+
let stream = if let StmtKind::Empty(_) = stmt.kind {
435435
// FIXME: Properly collect tokens for empty statements.
436436
TokenStream::token_alone(token::Semi, stmt.span)
437437
} else {

0 commit comments

Comments
 (0)