From fac04a6ba39c20978a814e9c04c5fbdb39207933 Mon Sep 17 00:00:00 2001 From: surechen Date: Thu, 22 Aug 2024 17:41:15 +0800 Subject: [PATCH] Fixing span manipulation and indentation of the suggestion introduced by #126187 According to comments: https://github.com/rust-lang/rust/pull/128084#issuecomment-2295254576 https://github.com/rust-lang/rust/pull/126187/files#r1634897691 --- .../src/error_reporting/traits/suggestions.rs | 5 ++--- .../ui/return/return-from-residual-sugg-issue-125997.stderr | 6 ------ tests/ui/try-trait/try-operator-on-main.stderr | 1 - 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index a962be54c3d88..3443955211d34 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -4668,9 +4668,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && b.expr.is_none() { sugg_spans.push(( - // The span will point to the closing curly brace `}` of the block. - b.span.shrink_to_hi().with_lo(b.span.hi() - BytePos(1)), - "\n Ok(())\n}".to_string(), + self.tcx.sess.source_map().end_point(b.span).shrink_to_lo(), + "\n Ok(())\n".to_string(), )); } err.multipart_suggestion_verbose( diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.stderr b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr index a59f38c2ec644..d0838cd9be312 100644 --- a/tests/ui/return/return-from-residual-sugg-issue-125997.stderr +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr @@ -14,7 +14,6 @@ LL | let mut _file = File::create("foo.txt")?; LL | LL + LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) @@ -34,7 +33,6 @@ LL | LL | println!(); LL + LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`) @@ -53,7 +51,6 @@ LL | let mut _file = File::create("foo.txt")?; LL | LL ~ LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`) @@ -73,7 +70,6 @@ LL | LL | println!(); LL ~ LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) @@ -93,7 +89,6 @@ LL | LL | mac!(); LL + LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) @@ -117,7 +112,6 @@ LL | LL | println!(); LL ~ LL + Ok(()) -LL + } | error: aborting due to 6 previous errors diff --git a/tests/ui/try-trait/try-operator-on-main.stderr b/tests/ui/try-trait/try-operator-on-main.stderr index d22117165c12b..3ddddefae7fbc 100644 --- a/tests/ui/try-trait/try-operator-on-main.stderr +++ b/tests/ui/try-trait/try-operator-on-main.stderr @@ -16,7 +16,6 @@ LL | // error for a `Try` type on a non-`Try` fn LL | try_trait_generic::<()>(); LL + LL + Ok(()) -LL + } | error[E0277]: the `?` operator can only be applied to values that implement `Try`