Skip to content

Commit 62ccf14

Browse files
committed
add check if macro from expansion
1 parent 3d8c1c1 commit 62ccf14

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
942942
err.span_label(block.span, "this empty block is missing a tail expression");
943943
return;
944944
};
945+
// FIXME expr and stmt have the same span if expr comes from expansion
946+
// cc: https://github.com/rust-lang/rust/pull/147416#discussion_r2499407523
947+
if stmt.span.from_expansion() {
948+
return;
949+
}
945950
let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
946951
return;
947952
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/147255>
2+
3+
fn main() {
4+
let mut x = 4;
5+
let x_str = {
6+
format!("{}", x);
7+
//()
8+
};
9+
println!("{}", x_str); //~ ERROR `()` doesn't implement `std::fmt::Display`
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: `()` doesn't implement `std::fmt::Display`
2+
--> $DIR/macro-expansion-empty-span-147255.rs:9:20
3+
|
4+
LL | println!("{}", x_str);
5+
| -- ^^^^^ `()` cannot be formatted with the default formatter
6+
| |
7+
| required by this formatting parameter
8+
|
9+
= help: the trait `std::fmt::Display` is not implemented for `()`
10+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
11+
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)