Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8750e24

Browse files
committedAug 25, 2024
Fixing span manipulation and indentation of the suggestion introduced by rust-lang#126187
According to comments: rust-lang#128084 (comment) https://github.com/rust-lang/rust/pull/126187/files#r1634897691
1 parent 739b1fd commit 8750e24

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed
 

‎compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -4667,10 +4667,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
46674667
if let hir::ExprKind::Block(b, _) = body.value.kind
46684668
&& b.expr.is_none()
46694669
{
4670+
// The span of '}' in the end of block.
4671+
let span = self.tcx.sess.source_map().end_point(b.span);
46704672
sugg_spans.push((
4671-
// The span will point to the closing curly brace `}` of the block.
4672-
b.span.shrink_to_hi().with_lo(b.span.hi() - BytePos(1)),
4673-
"\n Ok(())\n}".to_string(),
4673+
span.shrink_to_lo(),
4674+
format!(
4675+
"{}{}",
4676+
" Ok(())\n",
4677+
self.tcx.sess.source_map().indentation_before(span).unwrap_or_default(),
4678+
),
46744679
));
46754680
}
46764681
err.multipart_suggestion_verbose(

‎tests/ui/return/return-from-residual-sugg-issue-125997.fixed

+6-12
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ use std::io::prelude::*;
99
fn test1() -> Result<(), Box<dyn std::error::Error>> {
1010
let mut _file = File::create("foo.txt")?;
1111
//~^ ERROR the `?` operator can only be used in a function
12-
1312
Ok(())
1413
}
1514

1615
fn test2() -> Result<(), Box<dyn std::error::Error>> {
1716
let mut _file = File::create("foo.txt")?;
1817
//~^ ERROR the `?` operator can only be used in a function
1918
println!();
20-
2119
Ok(())
2220
}
2321

@@ -27,9 +25,8 @@ macro_rules! mac {
2725
let mut _file = File::create("foo.txt")?;
2826
//~^ ERROR the `?` operator can only be used in a function
2927
println!();
30-
31-
Ok(())
32-
}
28+
Ok(())
29+
}
3330
};
3431
}
3532

@@ -39,23 +36,20 @@ impl A {
3936
fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
4037
let mut _file = File::create("foo.txt")?;
4138
//~^ ERROR the `?` operator can only be used in a method
42-
43-
Ok(())
44-
}
39+
Ok(())
40+
}
4541

4642
fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
4743
let mut _file = File::create("foo.txt")?;
4844
//~^ ERROR the `?` operator can only be used in a method
4945
println!();
50-
51-
Ok(())
52-
}
46+
Ok(())
47+
}
5348
}
5449

5550
fn main() -> Result<(), Box<dyn std::error::Error>> {
5651
let mut _file = File::create("foo.txt")?;
5752
//~^ ERROR the `?` operator can only be used in a function
5853
mac!();
59-
6054
Ok(())
6155
}

‎tests/ui/return/return-from-residual-sugg-issue-125997.stderr

+6-15
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ help: consider adding return type
1212
LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> {
1313
LL | let mut _file = File::create("foo.txt")?;
1414
LL |
15-
LL +
1615
LL + Ok(())
17-
LL + }
1816
|
1917

2018
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>> {
3230
LL | let mut _file = File::create("foo.txt")?;
3331
LL |
3432
LL | println!();
35-
LL +
3633
LL + Ok(())
37-
LL + }
3834
|
3935

4036
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
5147
LL ~ fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
5248
LL | let mut _file = File::create("foo.txt")?;
5349
LL |
54-
LL ~
55-
LL + Ok(())
56-
LL + }
50+
LL ~ Ok(())
51+
LL ~ }
5752
|
5853

5954
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>> {
7166
LL | let mut _file = File::create("foo.txt")?;
7267
LL |
7368
LL | println!();
74-
LL ~
75-
LL + Ok(())
76-
LL + }
69+
LL ~ Ok(())
70+
LL ~ }
7771
|
7872

7973
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>> {
9185
LL | let mut _file = File::create("foo.txt")?;
9286
LL |
9387
LL | mac!();
94-
LL +
9588
LL + Ok(())
96-
LL + }
9789
|
9890

9991
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>> {
115107
LL | let mut _file = File::create("foo.txt")?;
116108
LL |
117109
LL | println!();
118-
LL ~
119-
LL + Ok(())
120-
LL + }
110+
LL ~ Ok(())
111+
LL ~ }
121112
|
122113

123114
error: aborting due to 6 previous errors

‎tests/ui/try-trait/try-operator-on-main.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
1414
LL | // error for a `Try` type on a non-`Try` fn
1515
...
1616
LL | try_trait_generic::<()>();
17-
LL +
1817
LL + Ok(())
19-
LL + }
2018
|
2119

2220
error[E0277]: the `?` operator can only be applied to values that implement `Try`

0 commit comments

Comments
 (0)
Failed to load comments.