Rollup merge of #152053 - TaKO8Ki:err-tail-semicolon-suggest, r=nnethercote
Avoid semicolon suggestion when tail expr is error Fixes rust-lang/rust#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.
This commit is contained in:
commit
b8755a44fc
3 changed files with 41 additions and 1 deletions
|
|
@ -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",
|
||||
|
|
|
|||
12
tests/ui/type/recover-from-semicolon-trailing-undefined.rs
Normal file
12
tests/ui/type/recover-from-semicolon-trailing-undefined.rs
Normal 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`
|
||||
}
|
||||
|
|
@ -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`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue