Skip to content

Commit

Permalink
Point to lifetime in fn definition on lifetime error note
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jun 28, 2018
1 parent cd8ca26 commit 5436a5c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
19 changes: 14 additions & 5 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -189,6 +189,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self,
region: ty::Region<'tcx>,
) -> (String, Option<Span>) {
let cm = self.sess.codemap();

let scope = region.free_region_binding_scope(self);
let node = self.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID);
let unknown;
Expand Down Expand Up @@ -219,10 +221,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
};
let (prefix, span) = match *region {
ty::ReEarlyBound(ref br) => (
format!("the lifetime {} as defined on", br.name),
self.sess.codemap().def_span(self.hir.span(node)),
),
ty::ReEarlyBound(ref br) => {
let mut sp = cm.def_span(self.hir.span(node));
if let Some(generics) = self.hir.get_generics(scope) {
for param in &generics.params {
if param.name.name().as_str() == br.name.as_str() {
sp = param.span;
}
}
}
(format!("the lifetime {} as defined on", br.name), sp)
}
ty::ReFree(ref fr) => match fr.bound_region {
ty::BrAnon(idx) => (
format!("the anonymous lifetime #{} defined on", idx + 1),
Expand All @@ -234,7 +243,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
),
_ => (
format!("the lifetime {} as defined on", fr.bound_region),
self.sess.codemap().def_span(self.hir.span(node)),
cm.def_span(self.hir.span(node)),
),
},
_ => bug!(),
Expand Down
Expand Up @@ -36,6 +36,7 @@ impl<'a, 't> Foo<'a, 't> for &'a isize {

fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
//~^ ERROR method not compatible with trait
//~| ERROR method not compatible with trait
//
// Note: This is a terrible error message. It is caused
// because, in the trait, 'b is early bound, and in the impl,
Expand Down
21 changes: 20 additions & 1 deletion src/test/ui/borrowck/regions-bound-missing-bound-in-impl.stderr
Expand Up @@ -29,6 +29,25 @@ note: the lifetime 'c as defined on the method body at 37:5...
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 37:24
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^

error[E0308]: method not compatible with trait
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'d>)`
found type `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'d>)`
note: the lifetime 'c as defined on the method body at 37:24...
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 37:5
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
|
Expand All @@ -53,7 +72,7 @@ LL | fn another_bound<'x: 'a>(self, x: Inv<'x>, y: Inv<'t>);
LL | fn another_bound<'x: 't>(self, x: Inv<'x>, y: Inv<'t>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'x: 't`

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

Some errors occurred: E0195, E0276, E0308.
For more information about an error, try `rustc --explain E0195`.
13 changes: 4 additions & 9 deletions src/test/ui/impl-trait/region-escape-via-bound.stderr
Expand Up @@ -4,16 +4,11 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea
LL | fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
| ^^^^^^^^^^^^^^
|
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 26:1
--> $DIR/region-escape-via-bound.rs:26:1
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 28:7
--> $DIR/region-escape-via-bound.rs:28:7
|
LL | / fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
LL | | //~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0700]
LL | | where 'x: 'y
LL | | {
LL | | x
LL | | }
| |_^
LL | where 'x: 'y
| ^^

error: aborting due to previous error

Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/issue-46472.stderr
Expand Up @@ -7,11 +7,11 @@ LL | &mut 4
LL | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:1...
--> $DIR/issue-46472.rs:13:1
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 13:8...
--> $DIR/issue-46472.rs:13:8
|
LL | fn bar<'a>() -> &'a mut u32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^

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

error: aborting due to 2 previous errors

Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/nll/normalization-bounds-error.stderr
Expand Up @@ -4,16 +4,16 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'d` d
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:1...
--> $DIR/normalization-bounds-error.rs:23:1
note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:14...
--> $DIR/normalization-bounds-error.rs:23:14
|
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the function body at 23:1...
--> $DIR/normalization-bounds-error.rs:23:1
| ^^
note: ...but the lifetime must also be valid for the lifetime 'a as defined on the function body at 23:18...
--> $DIR/normalization-bounds-error.rs:23:18
|
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^
= note: ...so that the types are compatible:
expected Visitor<'d>
found Visitor<'_>
Expand Down

0 comments on commit 5436a5c

Please sign in to comment.