Skip to content

Commit b8755a4

Browse files
Rollup merge of #152053 - TaKO8Ki:err-tail-semicolon-suggest, r=nnethercote
Avoid semicolon suggestion when tail expr is error Fixes #151610 When the tail expression is Err due to recovery, HIR constructs `StmtKind::Semi(Err(..))`. The suggestion path then uses `stmt.span.with_lo(tail_expr.span.hi())` to target the semicolon, but `stmt.span == tail_expr.span` so the derived span is empty/invalid.
2 parents 3bb8cd4 + d329971 commit b8755a4

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,9 @@ 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 !matches!(tail_expr.kind, hir::ExprKind::Err(_))
958+
&& self.predicate_must_hold_modulo_regions(&new_obligation)
959+
{
958960
err.span_suggestion_short(
959961
stmt.span.with_lo(tail_expr.span.hi()),
960962
"remove this semicolon",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: -Znext-solver=globally
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/151610
4+
5+
fn main() {
6+
let x_str = {
7+
x!("{}", x);
8+
//~^ ERROR cannot find macro `x` in this scope
9+
};
10+
println!("{}", x_str);
11+
//~^ ERROR `()` doesn't implement `std::fmt::Display`
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: cannot find macro `x` in this scope
2+
--> $DIR/recover-from-semicolon-trailing-undefined.rs:7:9
3+
|
4+
LL | x!("{}", x);
5+
| ^
6+
7+
error[E0277]: `()` doesn't implement `std::fmt::Display`
8+
--> $DIR/recover-from-semicolon-trailing-undefined.rs:10:20
9+
|
10+
LL | let x_str = {
11+
| _________________-
12+
LL | | x!("{}", x);
13+
LL | |
14+
LL | | };
15+
| |_____- this block is missing a tail expression
16+
LL | println!("{}", x_str);
17+
| -- ^^^^^ `()` cannot be formatted with the default formatter
18+
| |
19+
| required by this formatting parameter
20+
|
21+
= help: the trait `std::fmt::Display` is not implemented for `()`
22+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)