Skip to content

Commit c23fe81

Browse files
Use head span for rustc_on_unimplemented's enclosing_scope attr
1 parent 84f0c3f commit c23fe81

File tree

8 files changed

+120
-197
lines changed

8 files changed

+120
-197
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
526526
});
527527

528528
let enclosing_scope_span =
529-
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(body));
529+
tcx.hir().span(tcx.hir().local_def_id_to_hir_id(body));
530530

531531
err.span_label(enclosing_scope_span, s);
532532
}

src/test/ui/on-unimplemented/enclosing-scope.stderr

+26-43
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
error[E0277]: the trait bound `Foo: Trait` is not satisfied
22
--> $DIR/enclosing-scope.rs:14:11
33
|
4-
LL | let x = || {
5-
| _____________-
6-
LL | | f(Foo{});
7-
| | - ^^^^^ the trait `Trait` is not implemented for `Foo`
8-
| | |
9-
| | required by a bound introduced by this call
10-
LL | | let y = || {
11-
LL | | f(Foo{});
12-
LL | | };
13-
LL | | };
14-
| |_____- in this scope
4+
LL | let x = || {
5+
| -- in this scope
6+
LL | f(Foo{});
7+
| - ^^^^^ the trait `Trait` is not implemented for `Foo`
8+
| |
9+
| required by a bound introduced by this call
1510
|
1611
note: required by a bound in `f`
1712
--> $DIR/enclosing-scope.rs:10:9
@@ -22,14 +17,12 @@ LL | fn f<T: Trait>(x: T) {}
2217
error[E0277]: the trait bound `Foo: Trait` is not satisfied
2318
--> $DIR/enclosing-scope.rs:16:15
2419
|
25-
LL | let y = || {
26-
| _________________-
27-
LL | | f(Foo{});
28-
| | - ^^^^^ the trait `Trait` is not implemented for `Foo`
29-
| | |
30-
| | required by a bound introduced by this call
31-
LL | | };
32-
| |_________- in this scope
20+
LL | let y = || {
21+
| -- in this scope
22+
LL | f(Foo{});
23+
| - ^^^^^ the trait `Trait` is not implemented for `Foo`
24+
| |
25+
| required by a bound introduced by this call
3326
|
3427
note: required by a bound in `f`
3528
--> $DIR/enclosing-scope.rs:10:9
@@ -40,19 +33,13 @@ LL | fn f<T: Trait>(x: T) {}
4033
error[E0277]: the trait bound `Foo: Trait` is not satisfied
4134
--> $DIR/enclosing-scope.rs:22:15
4235
|
43-
LL | / fn main() {
44-
LL | | let x = || {
45-
LL | | f(Foo{});
46-
LL | | let y = || {
47-
... |
48-
LL | | f(Foo{});
49-
| | - ^^^^^ the trait `Trait` is not implemented for `Foo`
50-
| | |
51-
| | required by a bound introduced by this call
52-
... |
53-
LL | | f(Foo{});
54-
LL | | }
55-
| |_- in this scope
36+
LL | fn main() {
37+
| --------- in this scope
38+
...
39+
LL | f(Foo{});
40+
| - ^^^^^ the trait `Trait` is not implemented for `Foo`
41+
| |
42+
| required by a bound introduced by this call
5643
|
5744
note: required by a bound in `f`
5845
--> $DIR/enclosing-scope.rs:10:9
@@ -63,17 +50,13 @@ LL | fn f<T: Trait>(x: T) {}
6350
error[E0277]: the trait bound `Foo: Trait` is not satisfied
6451
--> $DIR/enclosing-scope.rs:26:7
6552
|
66-
LL | / fn main() {
67-
LL | | let x = || {
68-
LL | | f(Foo{});
69-
LL | | let y = || {
70-
... |
71-
LL | | f(Foo{});
72-
| | - ^^^^^ the trait `Trait` is not implemented for `Foo`
73-
| | |
74-
| | required by a bound introduced by this call
75-
LL | | }
76-
| |_- in this scope
53+
LL | fn main() {
54+
| --------- in this scope
55+
...
56+
LL | f(Foo{});
57+
| - ^^^^^ the trait `Trait` is not implemented for `Foo`
58+
| |
59+
| required by a bound introduced by this call
7760
|
7861
note: required by a bound in `f`
7962
--> $DIR/enclosing-scope.rs:10:9

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

+15-33
Original file line numberDiff line numberDiff line change
@@ -1493,17 +1493,11 @@ LL | if (let 0 = 0)? {}
14931493
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
14941494
--> $DIR/disallowed-positions.rs:132:19
14951495
|
1496-
LL | / fn nested_within_if_expr() {
1497-
LL | | if &let 0 = 0 {}
1498-
LL | |
1499-
LL | |
1500-
... |
1501-
LL | | if (let 0 = 0)? {}
1502-
| | ^ cannot use the `?` operator in a function that returns `()`
1503-
... |
1504-
LL | |
1505-
LL | | }
1506-
| |_- this function should return `Result` or `Option` to accept `?`
1496+
LL | fn nested_within_if_expr() {
1497+
| -------------------------- this function should return `Result` or `Option` to accept `?`
1498+
...
1499+
LL | if (let 0 = 0)? {}
1500+
| ^ cannot use the `?` operator in a function that returns `()`
15071501
|
15081502
= help: the trait `FromResidual<_>` is not implemented for `()`
15091503

@@ -1693,17 +1687,11 @@ LL | while (let 0 = 0)? {}
16931687
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
16941688
--> $DIR/disallowed-positions.rs:224:22
16951689
|
1696-
LL | / fn nested_within_while_expr() {
1697-
LL | | while &let 0 = 0 {}
1698-
LL | |
1699-
LL | |
1700-
... |
1701-
LL | | while (let 0 = 0)? {}
1702-
| | ^ cannot use the `?` operator in a function that returns `()`
1703-
... |
1704-
LL | |
1705-
LL | | }
1706-
| |_- this function should return `Result` or `Option` to accept `?`
1690+
LL | fn nested_within_while_expr() {
1691+
| ----------------------------- this function should return `Result` or `Option` to accept `?`
1692+
...
1693+
LL | while (let 0 = 0)? {}
1694+
| ^ cannot use the `?` operator in a function that returns `()`
17071695
|
17081696
= help: the trait `FromResidual<_>` is not implemented for `()`
17091697

@@ -1881,17 +1869,11 @@ LL | (let 0 = 0)?;
18811869
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
18821870
--> $DIR/disallowed-positions.rs:325:16
18831871
|
1884-
LL | / fn outside_if_and_while_expr() {
1885-
LL | | &let 0 = 0;
1886-
LL | |
1887-
LL | |
1888-
... |
1889-
LL | | (let 0 = 0)?;
1890-
| | ^ cannot use the `?` operator in a function that returns `()`
1891-
... |
1892-
LL | |
1893-
LL | | }
1894-
| |_- this function should return `Result` or `Option` to accept `?`
1872+
LL | fn outside_if_and_while_expr() {
1873+
| ------------------------------ this function should return `Result` or `Option` to accept `?`
1874+
...
1875+
LL | (let 0 = 0)?;
1876+
| ^ cannot use the `?` operator in a function that returns `()`
18951877
|
18961878
= help: the trait `FromResidual<_>` is not implemented for `()`
18971879

src/test/ui/try-trait/bad-interconversion.stderr

+28-45
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ LL | Ok(Err(123_i32)?)
2222
error[E0277]: the `?` operator can only be used on `Result`s, not `Option`s, in a function that returns `Result`
2323
--> $DIR/bad-interconversion.rs:11:12
2424
|
25-
LL | / fn option_to_result() -> Result<u64, String> {
26-
LL | | Some(3)?;
27-
| | ^ use `.ok_or(...)?` to provide an error compatible with `Result<u64, String>`
28-
LL | |
29-
LL | | Ok(10)
30-
LL | | }
31-
| |_- this function returns a `Result`
25+
LL | fn option_to_result() -> Result<u64, String> {
26+
| -------------------------------------------- this function returns a `Result`
27+
LL | Some(3)?;
28+
| ^ use `.ok_or(...)?` to provide an error compatible with `Result<u64, String>`
3229
|
3330
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<u64, String>`
3431
= help: the following other types implement trait `FromResidual<R>`:
@@ -38,12 +35,10 @@ LL | | }
3835
error[E0277]: the `?` operator can only be used on `Result`s in a function that returns `Result`
3936
--> $DIR/bad-interconversion.rs:17:31
4037
|
41-
LL | / fn control_flow_to_result() -> Result<u64, String> {
42-
LL | | Ok(ControlFlow::Break(123)?)
43-
| | ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Result<u64, String>`
44-
LL | |
45-
LL | | }
46-
| |_- this function returns a `Result`
38+
LL | fn control_flow_to_result() -> Result<u64, String> {
39+
| -------------------------------------------------- this function returns a `Result`
40+
LL | Ok(ControlFlow::Break(123)?)
41+
| ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Result<u64, String>`
4742
|
4843
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Result<u64, String>`
4944
= help: the following other types implement trait `FromResidual<R>`:
@@ -53,12 +48,10 @@ LL | | }
5348
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
5449
--> $DIR/bad-interconversion.rs:22:22
5550
|
56-
LL | / fn result_to_option() -> Option<u16> {
57-
LL | | Some(Err("hello")?)
58-
| | ^ use `.ok()?` if you want to discard the `Result<Infallible, &str>` error information
59-
LL | |
60-
LL | | }
61-
| |_- this function returns an `Option`
51+
LL | fn result_to_option() -> Option<u16> {
52+
| ------------------------------------ this function returns an `Option`
53+
LL | Some(Err("hello")?)
54+
| ^ use `.ok()?` if you want to discard the `Result<Infallible, &str>` error information
6255
|
6356
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `Option<u16>`
6457
= help: the following other types implement trait `FromResidual<R>`:
@@ -68,12 +61,10 @@ LL | | }
6861
error[E0277]: the `?` operator can only be used on `Option`s in a function that returns `Option`
6962
--> $DIR/bad-interconversion.rs:27:33
7063
|
71-
LL | / fn control_flow_to_option() -> Option<u64> {
72-
LL | | Some(ControlFlow::Break(123)?)
73-
| | ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Option<u64>`
74-
LL | |
75-
LL | | }
76-
| |_- this function returns an `Option`
64+
LL | fn control_flow_to_option() -> Option<u64> {
65+
| ------------------------------------------ this function returns an `Option`
66+
LL | Some(ControlFlow::Break(123)?)
67+
| ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Option<u64>`
7768
|
7869
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Option<u64>`
7970
= help: the following other types implement trait `FromResidual<R>`:
@@ -83,40 +74,32 @@ LL | | }
8374
error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
8475
--> $DIR/bad-interconversion.rs:32:39
8576
|
86-
LL | / fn result_to_control_flow() -> ControlFlow<String> {
87-
LL | | ControlFlow::Continue(Err("hello")?)
88-
| | ^ this `?` produces `Result<Infallible, &str>`, which is incompatible with `ControlFlow<String>`
89-
LL | |
90-
LL | | }
91-
| |_- this function returns a `ControlFlow`
77+
LL | fn result_to_control_flow() -> ControlFlow<String> {
78+
| -------------------------------------------------- this function returns a `ControlFlow`
79+
LL | ControlFlow::Continue(Err("hello")?)
80+
| ^ this `?` produces `Result<Infallible, &str>`, which is incompatible with `ControlFlow<String>`
9281
|
9382
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `ControlFlow<String>`
9483
= help: the trait `FromResidual` is implemented for `ControlFlow<B, C>`
9584

9685
error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
9786
--> $DIR/bad-interconversion.rs:37:12
9887
|
99-
LL | / fn option_to_control_flow() -> ControlFlow<u64> {
100-
LL | | Some(3)?;
101-
| | ^ this `?` produces `Option<Infallible>`, which is incompatible with `ControlFlow<u64>`
102-
LL | |
103-
LL | | ControlFlow::Break(10)
104-
LL | | }
105-
| |_- this function returns a `ControlFlow`
88+
LL | fn option_to_control_flow() -> ControlFlow<u64> {
89+
| ----------------------------------------------- this function returns a `ControlFlow`
90+
LL | Some(3)?;
91+
| ^ this `?` produces `Option<Infallible>`, which is incompatible with `ControlFlow<u64>`
10692
|
10793
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `ControlFlow<u64>`
10894
= help: the trait `FromResidual` is implemented for `ControlFlow<B, C>`
10995

11096
error[E0277]: the `?` operator in a function that returns `ControlFlow<B, _>` can only be used on other `ControlFlow<B, _>`s (with the same Break type)
11197
--> $DIR/bad-interconversion.rs:43:29
11298
|
113-
LL | / fn control_flow_to_control_flow() -> ControlFlow<i64> {
114-
LL | | ControlFlow::Break(4_u8)?;
115-
| | ^ this `?` produces `ControlFlow<u8, Infallible>`, which is incompatible with `ControlFlow<i64>`
116-
LL | |
117-
LL | | ControlFlow::Continue(())
118-
LL | | }
119-
| |_- this function returns a `ControlFlow`
99+
LL | fn control_flow_to_control_flow() -> ControlFlow<i64> {
100+
| ----------------------------------------------------- this function returns a `ControlFlow`
101+
LL | ControlFlow::Break(4_u8)?;
102+
| ^ this `?` produces `ControlFlow<u8, Infallible>`, which is incompatible with `ControlFlow<i64>`
120103
|
121104
= help: the trait `FromResidual<ControlFlow<u8, Infallible>>` is not implemented for `ControlFlow<i64>`
122105
= note: unlike `Result`, there's no `From`-conversion performed for `ControlFlow`

src/test/ui/try-trait/option-to-result.stderr

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
error[E0277]: the `?` operator can only be used on `Result`s, not `Option`s, in a function that returns `Result`
22
--> $DIR/option-to-result.rs:5:6
33
|
4-
LL | / fn test_result() -> Result<(),()> {
5-
LL | | let a:Option<()> = Some(());
6-
LL | | a?;
7-
| | ^ use `.ok_or(...)?` to provide an error compatible with `Result<(), ()>`
8-
LL | | Ok(())
9-
LL | | }
10-
| |_- this function returns a `Result`
4+
LL | fn test_result() -> Result<(),()> {
5+
| --------------------------------- this function returns a `Result`
6+
LL | let a:Option<()> = Some(());
7+
LL | a?;
8+
| ^ use `.ok_or(...)?` to provide an error compatible with `Result<(), ()>`
119
|
1210
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<(), ()>`
1311
= help: the following other types implement trait `FromResidual<R>`:
@@ -17,13 +15,11 @@ LL | | }
1715
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
1816
--> $DIR/option-to-result.rs:11:6
1917
|
20-
LL | / fn test_option() -> Option<i32>{
21-
LL | | let a:Result<i32, i32> = Ok(5);
22-
LL | | a?;
23-
| | ^ use `.ok()?` if you want to discard the `Result<Infallible, i32>` error information
24-
LL | | Some(5)
25-
LL | | }
26-
| |_- this function returns an `Option`
18+
LL | fn test_option() -> Option<i32>{
19+
| ------------------------------- this function returns an `Option`
20+
LL | let a:Result<i32, i32> = Ok(5);
21+
LL | a?;
22+
| ^ use `.ok()?` if you want to discard the `Result<Infallible, i32>` error information
2723
|
2824
= help: the trait `FromResidual<Result<Infallible, i32>>` is not implemented for `Option<i32>`
2925
= help: the following other types implement trait `FromResidual<R>`:

0 commit comments

Comments
 (0)