Skip to content

Commit 7af1ee3

Browse files
authored
Rollup merge of #147416 - Kivooeo:ice-fix23456, r=fmease
Early return if span is from expansion so we dont get empty span and ice later on Fixes #147255 The problem original was from that stmt.span was from expansion and it span was bigger than right part which is block.span, so it causes empty span and panic later on, I decided to add checks for both of them to be on the safe side r? `@fmease` (you were in discussion on this issue so I decided to assign you, feel free to reroll)
2 parents 47eeb00 + 62ccf14 commit 7af1ee3

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
@@ -937,6 +937,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
937937
err.span_label(block.span, "this empty block is missing a tail expression");
938938
return;
939939
};
940+
// FIXME expr and stmt have the same span if expr comes from expansion
941+
// cc: https://github.com/rust-lang/rust/pull/147416#discussion_r2499407523
942+
if stmt.span.from_expansion() {
943+
return;
944+
}
940945
let hir::StmtKind::Semi(tail_expr) = stmt.kind else {
941946
return;
942947
};
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)