Skip to content

Commit 5436a5c

Browse files
committed
Point to lifetime in fn definition on lifetime error note
1 parent cd8ca26 commit 5436a5c

File tree

6 files changed

+51
-27
lines changed

6 files changed

+51
-27
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
189189
self,
190190
region: ty::Region<'tcx>,
191191
) -> (String, Option<Span>) {
192+
let cm = self.sess.codemap();
193+
192194
let scope = region.free_region_binding_scope(self);
193195
let node = self.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID);
194196
let unknown;
@@ -219,10 +221,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
219221
}
220222
};
221223
let (prefix, span) = match *region {
222-
ty::ReEarlyBound(ref br) => (
223-
format!("the lifetime {} as defined on", br.name),
224-
self.sess.codemap().def_span(self.hir.span(node)),
225-
),
224+
ty::ReEarlyBound(ref br) => {
225+
let mut sp = cm.def_span(self.hir.span(node));
226+
if let Some(generics) = self.hir.get_generics(scope) {
227+
for param in &generics.params {
228+
if param.name.name().as_str() == br.name.as_str() {
229+
sp = param.span;
230+
}
231+
}
232+
}
233+
(format!("the lifetime {} as defined on", br.name), sp)
234+
}
226235
ty::ReFree(ref fr) => match fr.bound_region {
227236
ty::BrAnon(idx) => (
228237
format!("the anonymous lifetime #{} defined on", idx + 1),
@@ -234,7 +243,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
234243
),
235244
_ => (
236245
format!("the lifetime {} as defined on", fr.bound_region),
237-
self.sess.codemap().def_span(self.hir.span(node)),
246+
cm.def_span(self.hir.span(node)),
238247
),
239248
},
240249
_ => bug!(),

src/test/ui/borrowck/regions-bound-missing-bound-in-impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl<'a, 't> Foo<'a, 't> for &'a isize {
3636

3737
fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
3838
//~^ ERROR method not compatible with trait
39+
//~| ERROR method not compatible with trait
3940
//
4041
// Note: This is a terrible error message. It is caused
4142
// because, in the trait, 'b is early bound, and in the impl,

src/test/ui/borrowck/regions-bound-missing-bound-in-impl.stderr

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ note: the lifetime 'c as defined on the method body at 37:5...
2929
|
3030
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 37:24
33+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
34+
|
35+
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
36+
| ^^
37+
38+
error[E0308]: method not compatible with trait
39+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
40+
|
41+
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
43+
|
44+
= note: expected type `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'d>)`
45+
found type `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'d>)`
46+
note: the lifetime 'c as defined on the method body at 37:24...
47+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
48+
|
49+
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
50+
| ^^
3251
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 37:5
3352
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
3453
|
@@ -53,7 +72,7 @@ LL | fn another_bound<'x: 'a>(self, x: Inv<'x>, y: Inv<'t>);
5372
LL | fn another_bound<'x: 't>(self, x: Inv<'x>, y: Inv<'t>) {
5473
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'x: 't`
5574

56-
error: aborting due to 5 previous errors
75+
error: aborting due to 6 previous errors
5776

5877
Some errors occurred: E0195, E0276, E0308.
5978
For more information about an error, try `rustc --explain E0195`.

src/test/ui/impl-trait/region-escape-via-bound.stderr

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea
44
LL | fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
55
| ^^^^^^^^^^^^^^
66
|
7-
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 26:1
8-
--> $DIR/region-escape-via-bound.rs:26:1
7+
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 28:7
8+
--> $DIR/region-escape-via-bound.rs:28:7
99
|
10-
LL | / fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
11-
LL | | //~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0700]
12-
LL | | where 'x: 'y
13-
LL | | {
14-
LL | | x
15-
LL | | }
16-
| |_^
10+
LL | where 'x: 'y
11+
| ^^
1712

1813
error: aborting due to previous error
1914

src/test/ui/issue-46472.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | &mut 4
77
LL | }
88
| - temporary value only lives until here
99
|
10-
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
11-
--> $DIR/issue-46472.rs:13:1
10+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:8...
11+
--> $DIR/issue-46472.rs:13:8
1212
|
1313
LL | fn bar<'a>() -> &'a mut u32 {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^
1515

1616
error[E0597]: borrowed value does not live long enough (Mir)
1717
--> $DIR/issue-46472.rs:14:10
@@ -22,11 +22,11 @@ LL | &mut 4
2222
LL | }
2323
| - temporary value only lives until here
2424
|
25-
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
26-
--> $DIR/issue-46472.rs:13:1
25+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:8...
26+
--> $DIR/issue-46472.rs:13:8
2727
|
2828
LL | fn bar<'a>() -> &'a mut u32 {
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
| ^^
3030

3131
error: aborting due to 2 previous errors
3232

src/test/ui/nll/normalization-bounds-error.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'d` d
44
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:1...
8-
--> $DIR/normalization-bounds-error.rs:23:1
7+
note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:14...
8+
--> $DIR/normalization-bounds-error.rs:23:14
99
|
1010
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the function body at 23:1...
13-
--> $DIR/normalization-bounds-error.rs:23:1
11+
| ^^
12+
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the function body at 23:18...
13+
--> $DIR/normalization-bounds-error.rs:23:18
1414
|
1515
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^^
1717
= note: ...so that the types are compatible:
1818
expected Visitor<'d>
1919
found Visitor<'_>

0 commit comments

Comments
 (0)