Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/macros/macro-expansion-empty-span-147255.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Regression test <https://github.com/rust-lang/rust/issues/147255>

fn main() {
let mut x = 4;
let x_str = {
format!("{}", x);
//()
};
println!("{}", x_str); //~ ERROR `()` doesn't implement `std::fmt::Display`
}
15 changes: 15 additions & 0 deletions tests/ui/macros/macro-expansion-empty-span-147255.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading