Skip to content

Commit 4e89811

Browse files
committed
Auto merge of #89860 - camsteffen:macro-semi, r=petrochenkov
Remove trailing semicolon from macro call span Macro call site spans are now less surprising/more consistent since they no longer contain a semicolon after the macro call. The downside is that we need to do a little guesswork to get the semicolon in diagnostics. But this should not be noticeable since it is rare for the semicolon to not immediately follow the macro call.
2 parents 7fbd4ce + cc4bc57 commit 4e89811

File tree

251 files changed

+775
-729
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

251 files changed

+775
-729
lines changed

compiler/rustc_expand/src/expand.rs

+29-43
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,10 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10241024
placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
10251025
}
10261026

1027-
fn collect_bang(
1028-
&mut self,
1029-
mac: ast::MacCall,
1030-
span: Span,
1031-
kind: AstFragmentKind,
1032-
) -> AstFragment {
1027+
fn collect_bang(&mut self, mac: ast::MacCall, kind: AstFragmentKind) -> AstFragment {
1028+
// cache the macro call span so that it can be
1029+
// easily adjusted for incremental compilation
1030+
let span = mac.span();
10331031
self.collect(kind, InvocationKind::Bang { mac, span })
10341032
}
10351033

@@ -1087,25 +1085,19 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10871085
let MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
10881086
Ok((style == MacStmtStyle::Semicolon, mac, attrs.into()))
10891087
}
1090-
StmtKind::Item(ref item) if matches!(item.kind, ItemKind::MacCall(..)) => {
1091-
match stmt.kind {
1092-
StmtKind::Item(item) => match item.into_inner() {
1093-
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1094-
Ok((mac.args.need_semicolon(), mac, attrs))
1095-
}
1096-
_ => unreachable!(),
1097-
},
1088+
StmtKind::Item(item) if matches!(item.kind, ItemKind::MacCall(..)) => {
1089+
match item.into_inner() {
1090+
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
1091+
Ok((mac.args.need_semicolon(), mac, attrs))
1092+
}
10981093
_ => unreachable!(),
10991094
}
11001095
}
1101-
StmtKind::Semi(ref expr) if matches!(expr.kind, ast::ExprKind::MacCall(..)) => {
1102-
match stmt.kind {
1103-
StmtKind::Semi(expr) => match expr.into_inner() {
1104-
ast::Expr { kind: ast::ExprKind::MacCall(mac), attrs, .. } => {
1105-
Ok((mac.args.need_semicolon(), mac, attrs.into()))
1106-
}
1107-
_ => unreachable!(),
1108-
},
1096+
StmtKind::Semi(expr) if matches!(expr.kind, ast::ExprKind::MacCall(..)) => {
1097+
match expr.into_inner() {
1098+
ast::Expr { kind: ast::ExprKind::MacCall(mac), attrs, .. } => {
1099+
Ok((mac.args.need_semicolon(), mac, attrs.into()))
1100+
}
11091101
_ => unreachable!(),
11101102
}
11111103
}
@@ -1222,7 +1214,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12221214

12231215
if let ast::ExprKind::MacCall(mac) = expr.kind {
12241216
self.check_attributes(&expr.attrs, &mac);
1225-
self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr().into_inner()
1217+
self.collect_bang(mac, AstFragmentKind::Expr).make_expr().into_inner()
12261218
} else {
12271219
assign_id!(self, &mut expr.id, || {
12281220
ensure_sufficient_stack(|| noop_visit_expr(&mut expr, self));
@@ -1318,7 +1310,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13181310

13191311
if let ast::ExprKind::MacCall(mac) = expr.kind {
13201312
self.check_attributes(&expr.attrs, &mac);
1321-
self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
1313+
self.collect_bang(mac, AstFragmentKind::OptExpr)
13221314
.make_opt_expr()
13231315
.map(|expr| expr.into_inner())
13241316
} else {
@@ -1339,9 +1331,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13391331
}
13401332

13411333
visit_clobber(pat, |mut pat| match mem::replace(&mut pat.kind, PatKind::Wild) {
1342-
PatKind::MacCall(mac) => {
1343-
self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat()
1344-
}
1334+
PatKind::MacCall(mac) => self.collect_bang(mac, AstFragmentKind::Pat).make_pat(),
13451335
_ => unreachable!(),
13461336
});
13471337
}
@@ -1360,12 +1350,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13601350
.make_stmts();
13611351
}
13621352

1363-
let span = stmt.span;
13641353
match self.take_stmt_bang(stmt) {
13651354
Ok((add_semicolon, mac, attrs)) => {
13661355
self.check_attributes(&attrs, &mac);
1367-
let mut stmts =
1368-
self.collect_bang(mac, span, AstFragmentKind::Stmts).make_stmts();
1356+
let mut stmts = self.collect_bang(mac, AstFragmentKind::Stmts).make_stmts();
13691357

13701358
// If this is a macro invocation with a semicolon, then apply that
13711359
// semicolon to the final statement produced by expansion.
@@ -1433,7 +1421,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14331421
item.attrs = attrs;
14341422
item.and_then(|item| match item.kind {
14351423
ItemKind::MacCall(mac) => {
1436-
self.collect_bang(mac, span, AstFragmentKind::Items).make_items()
1424+
self.collect_bang(mac, AstFragmentKind::Items).make_items()
14371425
}
14381426
_ => unreachable!(),
14391427
})
@@ -1542,9 +1530,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15421530
ast::AssocItemKind::MacCall(ref mac) => {
15431531
self.check_attributes(&item.attrs, &mac);
15441532
item.and_then(|item| match item.kind {
1545-
ast::AssocItemKind::MacCall(mac) => self
1546-
.collect_bang(mac, item.span, AstFragmentKind::TraitItems)
1547-
.make_trait_items(),
1533+
ast::AssocItemKind::MacCall(mac) => {
1534+
self.collect_bang(mac, AstFragmentKind::TraitItems).make_trait_items()
1535+
}
15481536
_ => unreachable!(),
15491537
})
15501538
}
@@ -1567,9 +1555,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15671555
ast::AssocItemKind::MacCall(ref mac) => {
15681556
self.check_attributes(&item.attrs, &mac);
15691557
item.and_then(|item| match item.kind {
1570-
ast::AssocItemKind::MacCall(mac) => self
1571-
.collect_bang(mac, item.span, AstFragmentKind::ImplItems)
1572-
.make_impl_items(),
1558+
ast::AssocItemKind::MacCall(mac) => {
1559+
self.collect_bang(mac, AstFragmentKind::ImplItems).make_impl_items()
1560+
}
15731561
_ => unreachable!(),
15741562
})
15751563
}
@@ -1586,9 +1574,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15861574
};
15871575

15881576
visit_clobber(ty, |mut ty| match mem::replace(&mut ty.kind, ast::TyKind::Err) {
1589-
ast::TyKind::MacCall(mac) => {
1590-
self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty()
1591-
}
1577+
ast::TyKind::MacCall(mac) => self.collect_bang(mac, AstFragmentKind::Ty).make_ty(),
15921578
_ => unreachable!(),
15931579
});
15941580
}
@@ -1613,9 +1599,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
16131599
ast::ForeignItemKind::MacCall(ref mac) => {
16141600
self.check_attributes(&foreign_item.attrs, &mac);
16151601
foreign_item.and_then(|item| match item.kind {
1616-
ast::ForeignItemKind::MacCall(mac) => self
1617-
.collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
1618-
.make_foreign_items(),
1602+
ast::ForeignItemKind::MacCall(mac) => {
1603+
self.collect_bang(mac, AstFragmentKind::ForeignItems).make_foreign_items()
1604+
}
16191605
_ => unreachable!(),
16201606
})
16211607
}

compiler/rustc_span/src/source_map.rs

+38
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,18 @@ impl SourceMap {
653653
})
654654
}
655655

656+
/// Extends the given `Span` while the next character matches the predicate
657+
pub fn span_extend_while(
658+
&self,
659+
span: Span,
660+
f: impl Fn(char) -> bool,
661+
) -> Result<Span, SpanSnippetError> {
662+
self.span_to_source(span, |s, _start, end| {
663+
let n = s[end..].char_indices().find(|&(_, c)| !f(c)).map_or(s.len() - end, |(i, _)| i);
664+
Ok(span.with_hi(span.hi() + BytePos(n as u32)))
665+
})
666+
}
667+
656668
/// Extends the given `Span` to just after the next occurrence of `c`.
657669
pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
658670
if let Ok(next_source) = self.span_to_next_source(sp) {
@@ -1013,6 +1025,32 @@ impl SourceMap {
10131025
let source_file = &self.files()[source_file_index];
10141026
source_file.is_imported()
10151027
}
1028+
1029+
/// Gets the span of a statement. If the statement is a macro expansion, the
1030+
/// span in the context of the block span is found. The trailing semicolon is included
1031+
/// on a best-effort basis.
1032+
pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
1033+
if !stmt_span.from_expansion() {
1034+
return stmt_span;
1035+
}
1036+
let mac_call = original_sp(stmt_span, block_span);
1037+
self.mac_call_stmt_semi_span(mac_call).map_or(mac_call, |s| mac_call.with_hi(s.hi()))
1038+
}
1039+
1040+
/// Tries to find the span of the semicolon of a macro call statement.
1041+
/// The input must be the *call site* span of a statement from macro expansion.
1042+
///
1043+
/// v output
1044+
/// mac!();
1045+
/// ^^^^^^ input
1046+
pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
1047+
let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?;
1048+
let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?));
1049+
if self.span_to_snippet(span).as_deref() != Ok(";") {
1050+
return None;
1051+
}
1052+
Some(span)
1053+
}
10161054
}
10171055

10181056
#[derive(Clone)]

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11711171
{
11721172
return None;
11731173
}
1174-
let original_span = original_sp(last_stmt.span, blk.span);
1175-
Some((original_span.with_lo(original_span.hi() - BytePos(1)), needs_box))
1174+
let span = if last_stmt.span.from_expansion() {
1175+
let mac_call = original_sp(last_stmt.span, blk.span);
1176+
self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)?
1177+
} else {
1178+
last_stmt.span.with_lo(last_stmt.span.hi() - BytePos(1))
1179+
};
1180+
Some((span, needs_box))
11761181
}
11771182

11781183
// Instantiates the given path, which must refer to an item with the given

src/test/mir-opt/unreachable_asm.main.UnreachablePropagation.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
StorageDead(_6); // scope 2 at $DIR/unreachable_asm.rs:18:9: 18:10
6060
StorageDead(_5); // scope 2 at $DIR/unreachable_asm.rs:18:9: 18:10
6161
StorageLive(_7); // scope 2 at $DIR/unreachable_asm.rs:21:9: 21:37
62-
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 3 at $DIR/unreachable_asm.rs:21:18: 21:35
62+
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 3 at $DIR/unreachable_asm.rs:21:18: 21:34
6363
_7 = const (); // scope 3 at $DIR/unreachable_asm.rs:21:9: 21:37
6464
StorageDead(_7); // scope 2 at $DIR/unreachable_asm.rs:21:36: 21:37
6565
StorageLive(_8); // scope 2 at $DIR/unreachable_asm.rs:22:9: 22:21

src/test/mir-opt/unreachable_asm_2.main.UnreachablePropagation.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
bb3: {
5151
StorageLive(_7); // scope 2 at $DIR/unreachable_asm_2.rs:16:13: 16:41
52-
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 3 at $DIR/unreachable_asm_2.rs:16:22: 16:39
52+
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 3 at $DIR/unreachable_asm_2.rs:16:22: 16:38
5353
_7 = const (); // scope 3 at $DIR/unreachable_asm_2.rs:16:13: 16:41
5454
StorageDead(_7); // scope 2 at $DIR/unreachable_asm_2.rs:16:40: 16:41
5555
_4 = const 21_i32; // scope 2 at $DIR/unreachable_asm_2.rs:17:13: 17:20
@@ -60,7 +60,7 @@
6060

6161
bb4: {
6262
StorageLive(_8); // scope 2 at $DIR/unreachable_asm_2.rs:20:13: 20:41
63-
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 4 at $DIR/unreachable_asm_2.rs:20:22: 20:39
63+
llvm_asm!(LlvmInlineAsmInner { asm: "NOP", asm_str_style: Cooked, outputs: [], inputs: [], clobbers: [], volatile: true, alignstack: false, dialect: Att } : [] : []); // scope 4 at $DIR/unreachable_asm_2.rs:20:22: 20:38
6464
_8 = const (); // scope 4 at $DIR/unreachable_asm_2.rs:20:13: 20:41
6565
StorageDead(_8); // scope 2 at $DIR/unreachable_asm_2.rs:20:40: 20:41
6666
_4 = const 42_i32; // scope 2 at $DIR/unreachable_asm_2.rs:21:13: 21:20

src/test/rustdoc-ui/intra-doc/warning.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ LL | #[doc = $f]
8888
| ^^^^^^^^^^^
8989
...
9090
LL | f!("Foo\nbar [BarF] bar\nbaz");
91-
| ------------------------------- in this macro invocation
91+
| ------------------------------ in this macro invocation
9292
|
9393
= note: the link appears in this line:
9494

src/test/ui-fulldeps/internal-lints/lint_pass_impl_without_macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | impl LintPass for Custom {
1818
| ^^^^^^^^
1919
...
2020
LL | custom_lint_pass_macro!();
21-
| -------------------------- in this macro invocation
21+
| ------------------------- in this macro invocation
2222
|
2323
= help: try using `declare_lint_pass!` or `impl_lint_pass!` instead
2424
= note: this error originates in the macro `custom_lint_pass_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

src/test/ui-fulldeps/issue-76270-panic-in-libproc-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: proc macro panicked
22
--> $DIR/issue-76270-panic-in-libproc-macro.rs:15:1
33
|
44
LL | proc_macro_panic::panic_in_libproc_macro!();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: message: `""` is not a valid identifier
88

src/test/ui/annotate-snippet/multispan.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@ error: hello to you, too!
22
--> $DIR/multispan.rs:15:5
33
|
44
LL | hello!(hi);
5-
| ^^^^^^^^^^^
5+
| ^^^^^^^^^^
66
|
77
error: hello to you, too!
88
--> $DIR/multispan.rs:18:5
99
|
1010
LL | hello!(hi hi);
11-
| ^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^
1212
|
1313
error: hello to you, too!
1414
--> $DIR/multispan.rs:21:5
1515
|
1616
LL | hello!(hi hi hi);
17-
| ^^^^^^^^^^^^^^^^^
17+
| ^^^^^^^^^^^^^^^^
1818
|
1919
error: hello to you, too!
2020
--> $DIR/multispan.rs:24:5
2121
|
2222
LL | hello!(hi hey hi yo hi beep beep hi hi);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424
|
2525
error: hello to you, too!
2626
--> $DIR/multispan.rs:25:5
2727
|
2828
LL | hello!(hi there, hi how are you? hi... hi.);
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030
|
3131
error: hello to you, too!
3232
--> $DIR/multispan.rs:26:5
3333
|
3434
LL | hello!(whoah. hi di hi di ho);
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3636
|
3737
error: hello to you, too!
3838
--> $DIR/multispan.rs:27:5
3939
|
4040
LL | hello!(hi good hi and good bye);
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4242
|

src/test/ui/asm/aarch64/interpolated-idents.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | $options($pure, $nomem, $readonly, $preserves_flags, $noretur
77
LL | / m!(in out lateout inout inlateout const sym
88
LL | | pure nomem readonly preserves_flags
99
LL | | noreturn nostack options);
10-
| |_________________________________- in this macro invocation
10+
| |________________________________- in this macro invocation
1111
|
1212
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

@@ -20,7 +20,7 @@ LL | $options($pure, $nomem, $readonly, $preserves_flags, $noretur
2020
LL | / m!(in out lateout inout inlateout const sym
2121
LL | | pure nomem readonly preserves_flags
2222
LL | | noreturn nostack options);
23-
| |_________________________________- in this macro invocation
23+
| |________________________________- in this macro invocation
2424
|
2525
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
2626

@@ -38,12 +38,12 @@ LL | m!(in out lateout inout inlateout const sym
3838
| |
3939
LL | | pure nomem readonly preserves_flags
4040
LL | | noreturn nostack options);
41-
| | -
42-
| |_________________________________|
43-
| |_________________________________in this macro invocation
44-
| |_________________________________in this macro invocation
45-
| |_________________________________in this macro invocation
46-
| in this macro invocation
41+
| | -
42+
| |________________________________|
43+
| |________________________________in this macro invocation
44+
| |________________________________in this macro invocation
45+
| |________________________________in this macro invocation
46+
| in this macro invocation
4747
|
4848
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
4949

src/test/ui/asm/aarch64/parse-error.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: requires at least a template string argument
22
--> $DIR/parse-error.rs:9:9
33
|
44
LL | asm!();
5-
| ^^^^^^^
5+
| ^^^^^^
66

77
error: asm template must be a string literal
88
--> $DIR/parse-error.rs:11:14
@@ -236,7 +236,7 @@ error: requires at least a template string argument
236236
--> $DIR/parse-error.rs:90:1
237237
|
238238
LL | global_asm!();
239-
| ^^^^^^^^^^^^^^
239+
| ^^^^^^^^^^^^^
240240

241241
error: asm template must be a string literal
242242
--> $DIR/parse-error.rs:92:13

src/test/ui/asm/aarch64/type-check-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ error[E0381]: use of possibly-uninitialized variable: `y`
7777
--> $DIR/type-check-2.rs:20:9
7878
|
7979
LL | asm!("{}", inout(reg) y);
80-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `y`
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `y`
8181

8282
error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable
8383
--> $DIR/type-check-2.rs:28:29

0 commit comments

Comments
 (0)