Point to lifetime in fn definition on lifetime error note

This commit is contained in:
Esteban Küber 2018-06-27 16:27:47 -07:00
parent cd8ca26257
commit 5436a5c55a
6 changed files with 51 additions and 27 deletions

View File

@ -189,6 +189,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self, self,
region: ty::Region<'tcx>, region: ty::Region<'tcx>,
) -> (String, Option<Span>) { ) -> (String, Option<Span>) {
let cm = self.sess.codemap();
let scope = region.free_region_binding_scope(self); let scope = region.free_region_binding_scope(self);
let node = self.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID); let node = self.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID);
let unknown; let unknown;
@ -219,10 +221,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
} }
}; };
let (prefix, span) = match *region { let (prefix, span) = match *region {
ty::ReEarlyBound(ref br) => ( ty::ReEarlyBound(ref br) => {
format!("the lifetime {} as defined on", br.name), let mut sp = cm.def_span(self.hir.span(node));
self.sess.codemap().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::ReFree(ref fr) => match fr.bound_region {
ty::BrAnon(idx) => ( ty::BrAnon(idx) => (
format!("the anonymous lifetime #{} defined on", idx + 1), format!("the anonymous lifetime #{} defined on", idx + 1),
@ -234,7 +243,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
), ),
_ => ( _ => (
format!("the lifetime {} as defined on", fr.bound_region), 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!(), _ => bug!(),

View File

@ -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>) { 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
//~| ERROR method not compatible with trait
// //
// Note: This is a terrible error message. It is caused // Note: This is a terrible error message. It is caused
// because, in the trait, 'b is early bound, and in the impl, // because, in the trait, 'b is early bound, and in the impl,

View File

@ -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>) { 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 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 --> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
| |
@ -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>) { LL | fn another_bound<'x: 't>(self, x: Inv<'x>, y: Inv<'t>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'x: '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. Some errors occurred: E0195, E0276, E0308.
For more information about an error, try `rustc --explain E0195`. For more information about an error, try `rustc --explain E0195`.

View File

@ -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> 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 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:26:1 --> $DIR/region-escape-via-bound.rs:28:7
| |
LL | / fn foo(x: Cell<&'x u32>) -> impl Trait<'y> LL | where 'x: '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 | | }
| |_^
error: aborting due to previous error error: aborting due to previous error

View File

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

View File

@ -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 {} 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... note: first, the lifetime cannot outlive the lifetime 'd as defined on the function body at 23:14...
--> $DIR/normalization-bounds-error.rs:23:1 --> $DIR/normalization-bounds-error.rs:23:14
| |
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {} 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... 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:1 --> $DIR/normalization-bounds-error.rs:23:18
| |
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {} LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^
= note: ...so that the types are compatible: = note: ...so that the types are compatible:
expected Visitor<'d> expected Visitor<'d>
found Visitor<'_> found Visitor<'_>