diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 37e622102e70f..ac2219fd01944 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -935,13 +935,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let hir::ExprKind::Block(block, None) = init.kind else { return; }; - if block.expr.is_some() { + if block.expr.is_some() || block.span.from_expansion() { return; } let [.., stmt] = block.stmts else { err.span_label(block.span, "this empty block is missing a tail expression"); return; }; + if stmt.span.from_expansion() { + return; + } let hir::StmtKind::Semi(tail_expr) = stmt.kind else { return; }; diff --git a/tests/ui/macros/macro-expansion-empty-span-147255.rs b/tests/ui/macros/macro-expansion-empty-span-147255.rs new file mode 100644 index 0000000000000..1ebda0e8a210f --- /dev/null +++ b/tests/ui/macros/macro-expansion-empty-span-147255.rs @@ -0,0 +1,10 @@ +//! Regression test + +fn main() { + let mut x = 4; + let x_str = { + format!("{}", x); + //() + }; + println!("{}", x_str); //~ ERROR `()` doesn't implement `std::fmt::Display` +} diff --git a/tests/ui/macros/macro-expansion-empty-span-147255.stderr b/tests/ui/macros/macro-expansion-empty-span-147255.stderr new file mode 100644 index 0000000000000..99396622b34eb --- /dev/null +++ b/tests/ui/macros/macro-expansion-empty-span-147255.stderr @@ -0,0 +1,15 @@ +error[E0277]: `()` doesn't implement `std::fmt::Display` + --> $DIR/macro-expansion-empty-span-147255.rs:9:20 + | +LL | println!("{}", x_str); + | -- ^^^^^ `()` cannot be formatted with the default formatter + | | + | required by this formatting parameter + | + = help: the trait `std::fmt::Display` is not implemented for `()` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + = 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) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.