Skip to content

Commit b51eba3

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

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
954954

955955
let new_obligation =
956956
self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
957-
if self.predicate_must_hold_modulo_regions(&new_obligation) {
957+
if self.predicate_must_hold_modulo_regions(&new_obligation)
958+
// FIXME expr and stmt have the same span if expr comes from expansion
959+
// cc: https://github.com/rust-lang/rust/pull/147416#discussion_r2499407523
960+
&& stmt.span.eq_ctxt(tail_expr.span)
961+
{
958962
err.span_suggestion_short(
959963
stmt.span.with_lo(tail_expr.span.hi()),
960964
"remove this semicolon",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Regression test <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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: `()` doesn't implement `std::fmt::Display`
2+
--> $DIR/macro-expansion-empty-span-147255.rs:9:20
3+
|
4+
LL | format!("{}", x);
5+
| ---------------- help: remove this semicolon
6+
...
7+
LL | println!("{}", x_str);
8+
| -- ^^^^^ `()` cannot be formatted with the default formatter
9+
| |
10+
| required by this formatting parameter
11+
|
12+
= help: the trait `std::fmt::Display` is not implemented for `()`
13+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
14+
= 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)
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)