Skip to content

Commit 17176cc

Browse files
committed
Add indication of anonymous lifetime position
1 parent fece59b commit 17176cc

File tree

5 files changed

+43
-50
lines changed

5 files changed

+43
-50
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use super::region_constraints::GenericKind;
5050
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
5151

5252
use crate::infer;
53+
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
5354
use crate::traits::error_reporting::report_object_safety_error;
5455
use crate::traits::{
5556
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
@@ -179,7 +180,14 @@ fn msg_span_from_early_bound_and_free_regions(
179180
}
180181
ty::ReFree(ref fr) => match fr.bound_region {
181182
ty::BrAnon(idx) => {
182-
(format!("the anonymous lifetime #{} defined on", idx + 1), tcx.hir().span(node))
183+
if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) {
184+
("the anonymous lifetime defined on".to_string(), ty.span)
185+
} else {
186+
(
187+
format!("the anonymous lifetime #{} defined on", idx + 1),
188+
tcx.hir().span(node),
189+
)
190+
}
183191
}
184192
_ => (
185193
format!("the lifetime `{}` as defined on", region),

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | | t.test();
77
LL | | });
88
| |______^
99
|
10-
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
11-
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
10+
note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24...
11+
--> $DIR/missing-lifetimes-in-signature-2.rs:19:24
1212
|
1313
LL | fn func<T: Test>(foo: &Foo, t: T) {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^^
1515

1616
error: aborting due to previous error
1717

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | fn func<T: Test>(foo: &Foo, t: T) {
66
LL | foo.bar(move |_| {
77
| ^^^
88
|
9-
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
10-
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
9+
note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24...
10+
--> $DIR/missing-lifetimes-in-signature-2.rs:19:24
1111
|
1212
LL | fn func<T: Test>(foo: &Foo, t: T) {
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
| ^^^
1414
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature-2.rs:20:13: 23:6]` will meet its required lifetime bounds
1515
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
1616
|

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.nll.stderr

+16-25
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,47 @@ error[E0311]: the parameter type `G` may not live long enough
2525
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
2626
| ^^^^^^^^^^^^^^^^^^
2727
|
28-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1...
29-
--> $DIR/missing-lifetimes-in-signature.rs:25:1
30-
|
31-
LL | / fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
32-
LL | |
33-
LL | | where
34-
LL | | G: Get<T>
35-
| |_____________^
28+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26...
29+
--> $DIR/missing-lifetimes-in-signature.rs:25:26
30+
|
31+
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
32+
| ^^^^^^
3633

3734
error[E0311]: the parameter type `G` may not live long enough
3835
--> $DIR/missing-lifetimes-in-signature.rs:47:45
3936
|
4037
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
4138
| ^^^^^^^^^^^^^^^^^^
4239
|
43-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1...
44-
--> $DIR/missing-lifetimes-in-signature.rs:47:1
40+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34...
41+
--> $DIR/missing-lifetimes-in-signature.rs:47:34
4542
|
46-
LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
47-
LL | |
48-
LL | | where
49-
LL | | G: Get<T>
50-
| |_____________^
43+
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
44+
| ^^^^^^
5145

5246
error[E0311]: the parameter type `G` may not live long enough
5347
--> $DIR/missing-lifetimes-in-signature.rs:59:58
5448
|
5549
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
5650
| ^^^^^^^^^^^^^^^^^^
5751
|
58-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5...
59-
--> $DIR/missing-lifetimes-in-signature.rs:59:5
52+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47...
53+
--> $DIR/missing-lifetimes-in-signature.rs:59:47
6054
|
6155
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
| ^^^^^^
6357

6458
error[E0311]: the parameter type `G` may not live long enough
6559
--> $DIR/missing-lifetimes-in-signature.rs:68:45
6660
|
6761
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
6862
| ^^^^^^^^^^^^^^^^^^^^^^^
6963
|
70-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 68:1...
71-
--> $DIR/missing-lifetimes-in-signature.rs:68:1
64+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 68:34...
65+
--> $DIR/missing-lifetimes-in-signature.rs:68:34
7266
|
73-
LL | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
74-
LL | |
75-
LL | | where
76-
LL | | G: Get<T>
77-
| |_____________^
67+
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
68+
| ^^^^^^
7869

7970
error[E0621]: explicit lifetime required in the type of `dest`
8071
--> $DIR/missing-lifetimes-in-signature.rs:73:5

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr

+12-18
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@ error[E0311]: the parameter type `G` may not live long enough
3333
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
3434
| ^^^^^^^^^^^^^^^^^^
3535
|
36-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1...
37-
--> $DIR/missing-lifetimes-in-signature.rs:25:1
38-
|
39-
LL | / fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
40-
LL | |
41-
LL | | where
42-
LL | | G: Get<T>
43-
| |_____________^
36+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26...
37+
--> $DIR/missing-lifetimes-in-signature.rs:25:26
38+
|
39+
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
40+
| ^^^^^^
4441
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5: 32:6]` will meet its required lifetime bounds
4542
--> $DIR/missing-lifetimes-in-signature.rs:25:37
4643
|
@@ -57,14 +54,11 @@ error[E0311]: the parameter type `G` may not live long enough
5754
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
5855
| ^^^^^^^^^^^^^^^^^^
5956
|
60-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1...
61-
--> $DIR/missing-lifetimes-in-signature.rs:47:1
57+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34...
58+
--> $DIR/missing-lifetimes-in-signature.rs:47:34
6259
|
63-
LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
64-
LL | |
65-
LL | | where
66-
LL | | G: Get<T>
67-
| |_____________^
60+
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
61+
| ^^^^^^
6862
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5: 54:6]` will meet its required lifetime bounds
6963
--> $DIR/missing-lifetimes-in-signature.rs:47:45
7064
|
@@ -81,11 +75,11 @@ error[E0311]: the parameter type `G` may not live long enough
8175
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
8276
| ^^^^^^^^^^^^^^^^^^
8377
|
84-
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5...
85-
--> $DIR/missing-lifetimes-in-signature.rs:59:5
78+
note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47...
79+
--> $DIR/missing-lifetimes-in-signature.rs:59:47
8680
|
8781
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
88-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
| ^^^^^^
8983
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:61:9: 63:10]` will meet its required lifetime bounds
9084
--> $DIR/missing-lifetimes-in-signature.rs:59:58
9185
|

0 commit comments

Comments
 (0)