about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsurechen <chenshuo17@huawei.com>2024-08-22 17:41:15 +0800
committersurechen <chenshuo17@huawei.com>2024-08-25 20:30:06 +0800
commit8750e2424744f6365fbbfd3a42059eb51091e736 (patch)
tree1733fa59b64a57100a1956eb4d41a610b02c2e3b
parent739b1fdb158a3216d1b592d0d79d77d256f59815 (diff)
downloadrust-8750e2424744f6365fbbfd3a42059eb51091e736.tar.gz
rust-8750e2424744f6365fbbfd3a42059eb51091e736.zip
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
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs11
-rw-r--r--tests/ui/return/return-from-residual-sugg-issue-125997.fixed18
-rw-r--r--tests/ui/return/return-from-residual-sugg-issue-125997.stderr21
-rw-r--r--tests/ui/try-trait/try-operator-on-main.stderr2
4 files changed, 20 insertions, 32 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 a962be54c3d..cc5b731a8d8 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
@@ -4667,10 +4667,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
             if let hir::ExprKind::Block(b, _) = body.value.kind
                 && b.expr.is_none()
             {
+                // The span of '}' in the end of block.
+                let span = self.tcx.sess.source_map().end_point(b.span);
                 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(),
+                    span.shrink_to_lo(),
+                    format!(
+                        "{}{}",
+                        "    Ok(())\n",
+                        self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
+                    ),
                 ));
             }
             err.multipart_suggestion_verbose(
diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.fixed b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed
index a5a13399825..43a9907a9b4 100644
--- a/tests/ui/return/return-from-residual-sugg-issue-125997.fixed
+++ b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed
@@ -9,7 +9,6 @@ use std::io::prelude::*;
 fn test1() -> Result<(), Box<dyn std::error::Error>> {
     let mut _file = File::create("foo.txt")?;
     //~^ ERROR the `?` operator can only be used in a function
-
     Ok(())
 }
 
@@ -17,7 +16,6 @@ fn test2() -> Result<(), Box<dyn std::error::Error>> {
     let mut _file = File::create("foo.txt")?;
     //~^ ERROR the `?` operator can only be used in a function
     println!();
-
     Ok(())
 }
 
@@ -27,9 +25,8 @@ macro_rules! mac {
             let mut _file = File::create("foo.txt")?;
             //~^ ERROR the `?` operator can only be used in a function
             println!();
-        
-    Ok(())
-}
+            Ok(())
+        }
     };
 }
 
@@ -39,23 +36,20 @@ impl A {
     fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
         let mut _file = File::create("foo.txt")?;
         //~^ ERROR the `?` operator can only be used in a method
-    
-    Ok(())
-}
+        Ok(())
+    }
 
     fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
         let mut _file = File::create("foo.txt")?;
         //~^ ERROR the `?` operator can only be used in a method
         println!();
-    
-    Ok(())
-}
+        Ok(())
+    }
 }
 
 fn main() -> Result<(), Box<dyn std::error::Error>> {
     let mut _file = File::create("foo.txt")?;
     //~^ ERROR the `?` operator can only be used in a function
     mac!();
-
     Ok(())
 }
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 a59f38c2ec6..e22f33fd242 100644
--- a/tests/ui/return/return-from-residual-sugg-issue-125997.stderr
+++ b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr
@@ -12,9 +12,7 @@ help: consider adding return type
 LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> {
 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`)
@@ -32,9 +30,7 @@ LL ~ fn test2() -> Result<(), Box<dyn std::error::Error>> {
 LL |     let mut _file = File::create("foo.txt")?;
 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`)
@@ -51,9 +47,8 @@ help: consider adding return type
 LL ~     fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
 LL |         let mut _file = File::create("foo.txt")?;
 LL |
-LL ~     
-LL +     Ok(())
-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`)
@@ -71,9 +66,8 @@ LL ~     fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
 LL |         let mut _file = File::create("foo.txt")?;
 LL |
 LL |         println!();
-LL ~     
-LL +     Ok(())
-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`)
@@ -91,9 +85,7 @@ LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
 LL |     let mut _file = File::create("foo.txt")?;
 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`)
@@ -115,9 +107,8 @@ LL ~         fn test3() -> Result<(), Box<dyn std::error::Error>> {
 LL |             let mut _file = File::create("foo.txt")?;
 LL |
 LL |             println!();
-LL ~         
-LL +     Ok(())
-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 d22117165c1..311e8076ed4 100644
--- a/tests/ui/try-trait/try-operator-on-main.stderr
+++ b/tests/ui/try-trait/try-operator-on-main.stderr
@@ -14,9 +14,7 @@ LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
 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`