1
Fork 0

Rollup merge of #129405 - surechen:fix_span_x, r=cjgillot

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
This commit is contained in:
Matthias Krüger 2024-08-26 01:49:00 +02:00 committed by GitHub
commit 6228bd6ef6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 32 deletions

View file

@ -4702,10 +4702,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let hir::ExprKind::Block(b, _) = body.value.kind if let hir::ExprKind::Block(b, _) = body.value.kind
&& b.expr.is_none() && 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(( sugg_spans.push((
// The span will point to the closing curly brace `}` of the block. span.shrink_to_lo(),
b.span.shrink_to_hi().with_lo(b.span.hi() - BytePos(1)), format!(
"\n Ok(())\n}".to_string(), "{}{}",
" Ok(())\n",
self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
),
)); ));
} }
err.multipart_suggestion_verbose( err.multipart_suggestion_verbose(

View file

@ -9,7 +9,6 @@ use std::io::prelude::*;
fn test1() -> Result<(), Box<dyn std::error::Error>> { fn test1() -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function //~^ ERROR the `?` operator can only be used in a function
Ok(()) Ok(())
} }
@ -17,7 +16,6 @@ fn test2() -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function //~^ ERROR the `?` operator can only be used in a function
println!(); println!();
Ok(()) Ok(())
} }
@ -27,9 +25,8 @@ macro_rules! mac {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function //~^ ERROR the `?` operator can only be used in a function
println!(); println!();
Ok(()) Ok(())
} }
}; };
} }
@ -39,23 +36,20 @@ impl A {
fn test4(&self) -> Result<(), Box<dyn std::error::Error>> { fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method //~^ ERROR the `?` operator can only be used in a method
Ok(()) Ok(())
} }
fn test5(&self) -> Result<(), Box<dyn std::error::Error>> { fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a method //~^ ERROR the `?` operator can only be used in a method
println!(); println!();
Ok(()) Ok(())
} }
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut _file = File::create("foo.txt")?; let mut _file = File::create("foo.txt")?;
//~^ ERROR the `?` operator can only be used in a function //~^ ERROR the `?` operator can only be used in a function
mac!(); mac!();
Ok(()) Ok(())
} }

View file

@ -12,9 +12,7 @@ help: consider adding return type
LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> { LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> {
LL | let mut _file = File::create("foo.txt")?; LL | let mut _file = File::create("foo.txt")?;
LL | LL |
LL +
LL + Ok(()) 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`) 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 | let mut _file = File::create("foo.txt")?;
LL | LL |
LL | println!(); LL | println!();
LL +
LL + Ok(()) 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`) 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 ~ fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
LL | let mut _file = File::create("foo.txt")?; LL | let mut _file = File::create("foo.txt")?;
LL | LL |
LL ~ LL ~ Ok(())
LL + Ok(()) LL ~ }
LL + }
| |
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`) 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 | let mut _file = File::create("foo.txt")?;
LL | LL |
LL | println!(); LL | println!();
LL ~ LL ~ Ok(())
LL + Ok(()) LL ~ }
LL + }
| |
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) 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 | let mut _file = File::create("foo.txt")?;
LL | LL |
LL | mac!(); LL | mac!();
LL +
LL + Ok(()) 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`) 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 | let mut _file = File::create("foo.txt")?;
LL | LL |
LL | println!(); LL | println!();
LL ~ LL ~ Ok(())
LL + Ok(()) LL ~ }
LL + }
| |
error: aborting due to 6 previous errors error: aborting due to 6 previous errors

View file

@ -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 | // error for a `Try` type on a non-`Try` fn
... ...
LL | try_trait_generic::<()>(); LL | try_trait_generic::<()>();
LL +
LL + Ok(()) LL + Ok(())
LL + }
| |
error[E0277]: the `?` operator can only be applied to values that implement `Try` error[E0277]: the `?` operator can only be applied to values that implement `Try`