avoid semicolon suggestion when tail expr is error

add a link to the issue

fix test stderr
This commit is contained in:
Takayuki Maeda 2026-02-04 00:22:14 +09:00
parent 78865ca937
commit d329971fc2
3 changed files with 41 additions and 1 deletions

View file

@ -954,7 +954,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let new_obligation =
self.mk_trait_obligation_with_new_self_ty(obligation.param_env, trait_pred_and_self);
if self.predicate_must_hold_modulo_regions(&new_obligation) {
if !matches!(tail_expr.kind, hir::ExprKind::Err(_))
&& self.predicate_must_hold_modulo_regions(&new_obligation)
{
err.span_suggestion_short(
stmt.span.with_lo(tail_expr.span.hi()),
"remove this semicolon",

View file

@ -0,0 +1,12 @@
//@ compile-flags: -Znext-solver=globally
// Regression test for https://github.com/rust-lang/rust/issues/151610
fn main() {
let x_str = {
x!("{}", x);
//~^ ERROR cannot find macro `x` in this scope
};
println!("{}", x_str);
//~^ ERROR `()` doesn't implement `std::fmt::Display`
}

View file

@ -0,0 +1,26 @@
error: cannot find macro `x` in this scope
--> $DIR/recover-from-semicolon-trailing-undefined.rs:7:9
|
LL | x!("{}", x);
| ^
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> $DIR/recover-from-semicolon-trailing-undefined.rs:10:20
|
LL | let x_str = {
| _________________-
LL | | x!("{}", x);
LL | |
LL | | };
| |_____- this block is missing a tail expression
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
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.