Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add long error explanation for E0495 #64404

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/librustc/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,47 @@ where
```
"##,

E0495: r##"
A lifetime cannot be determined in the given situation.

Erroneous code example:

```compile_fail,E0495
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the single-field tuple necessary here? and the match (could it be a let)? It seems like they're adding extra confusion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, I was too focused on simplifying the lifetime change.

match (&t,) { // error!
((u,),) => u,
}
}

let y = Box::new((42,));
let x = transmute_lifetime(&y);
```

In this code, you have two ways to solve this issue:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this example is representative of the majority of places where users encounter this error-- if users are hitting an error due to mismatched lifetimes, IME it's rare that it's easily solvable by just adding a bound in one location.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have another example in mind by any chance? Maybe another example would be the best thing to do to avoid confusing users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an easy one that would be representative offhand, sorry. There are a number of things I've seen cause this error, and I don't have a good idea which is the most important to cover, nor what broader categories issues fall into.

1. Enforce that `'a` lives at least as long as `'b`.
2. Use the same lifetime requirement for both input and output values.

So for the first solution, you can do it by replacing `'a` with `'a: 'b`:

```
fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T {
match (&t,) { // ok!
((u,),) => u,
}
}
```

In the second you can do it by simply removing `'b` so they both use `'a`:

```
fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T {
match (&t,) { // ok!
((u,),) => u,
}
}
```
"##,

E0496: r##"
A lifetime name is shadowing another lifetime name. Erroneous code example:

Expand Down Expand Up @@ -2116,8 +2157,6 @@ rejected in your own crates.
E0488, // lifetime of variable does not enclose its declaration
E0489, // type/lifetime parameter not in scope here
E0490, // a value of type `..` is borrowed for too long
E0495, // cannot infer an appropriate lifetime due to conflicting
// requirements
E0623, // lifetime mismatch where both parameters are anonymous regions
E0628, // generators cannot have explicit parameters
E0631, // type mismatch in closure arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ LL | bar(foo, x)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ LL | fn baz<'a,'b>(x: Type<'a>) -> Type<'static> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
3 changes: 2 additions & 1 deletion src/test/ui/c-variadic/variadic-ffi-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,5 @@ LL | | }

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0308`.
Some errors have detailed explanations: E0308, E0495.
For more information about an error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | invoke(&x, |a, b| if a > b { a } else { b });

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/impl-header-lifetime-elision/dyn-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ LL | | }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ LL | fn foo(&self, x: &u32, y: &'a u32) -> &'a u32 {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/in-band-lifetimes/mismatched_trait_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ LL | x

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-16683.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | trait T<'a> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-17758.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ LL | trait Foo<'a> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
3 changes: 2 additions & 1 deletion src/test/ui/issues/issue-20831-debruijn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ LL | impl<'a> Publisher<'a> for MyStruct<'a> {

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
Some errors have detailed explanations: E0308, E0495.
For more information about an error, try `rustc --explain E0308`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-52213.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | ((u,),) => u,

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-55796.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ LL | Box::new(self.in_edges(u).map(|e| e.target()))

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/nll/issue-55394.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | impl Foo<'_> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/nll/normalization-bounds-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/nll/type-alias-free-regions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ LL | impl<'a> FromTuple<'a> for C<'a> {

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ LL | <Foo<'a>>::C

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ LL | T::C

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ LL | fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait {

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/region-object-lifetime-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | x.borrowed()

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/region-object-lifetime-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | x.borrowed()

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ LL | fn d<'a,'b>(v: &'a [u8]) -> Box<dyn Foo+'b> {

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0621`.
Some errors have detailed explanations: E0495, E0621.
For more information about an error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-addr-of-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ LL | let p: &'static mut usize = &mut self.cats_chased;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-addr-of-upvar-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ LL | let p: &'static mut usize = &mut self.food;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ LL | impl<'a,'b> Foo<'b> for &'a i64 {

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ LL | impl<'a> Foo for &'a i32 {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ LL | box B(&*v) as Box<dyn X>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ LL | box B(&*v) as Box<dyn X>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | fn make_object_bad<'a,'b,'c,A:SomeTrait+'a+'b>(v: A) -> Box<dyn SomeTrait +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-creating-enums4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ LL | fn mk_add_bad2<'a,'b>(x: &'a Ast<'a>, y: &'a Ast<'a>, z: &Ast) -> Ast<'b> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-escape-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | s.f(|p| p)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-escape-via-trait-or-not.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | with(|o| o)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ LL | | }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-infer-call-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | let z = with(|y| { select(x, y) });

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
3 changes: 2 additions & 1 deletion src/test/ui/regions/regions-nested-fns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ LL | fn nested<'x>(x: &'x isize) {

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0312`.
Some errors have detailed explanations: E0312, E0495.
For more information about an error, try `rustc --explain E0312`.
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ LL | fn bar<'a, 'b>()

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-ret-borrowed-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | with(|o| o)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-ret-borrowed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ LL | with(|o| o)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ LL | let y = f();

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
2 changes: 1 addition & 1 deletion src/test/ui/regions/regions-trait-object-subtyping.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ LL | fn foo4<'a:'b,'b>(x: Wrapper<&'a mut dyn Dummy>) -> Wrapper<&'b mut dyn Dum

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0478.
Some errors have detailed explanations: E0308, E0478, E0495.
For more information about an error, try `rustc --explain E0308`.
2 changes: 1 addition & 1 deletion src/test/ui/reject-specialized-drops-8142.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0308, E0366, E0367.
Some errors have detailed explanations: E0308, E0366, E0367, E0495.
For more information about an error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> {

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ LL | Box::new(items.iter())

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
2 changes: 1 addition & 1 deletion src/test/ui/wf/wf-static-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ LL | <IndirectEvil>::static_evil(b)

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0312, E0478.
Some errors have detailed explanations: E0312, E0478, E0495.
For more information about an error, try `rustc --explain E0312`.