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

impl trait error message around `'static` is probably confusing #48467

Open
nikomatsakis opened this Issue Feb 23, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@nikomatsakis
Copy link
Contributor

nikomatsakis commented Feb 23, 2018

The error message for this example seems potentially confusing:

#![feature(dyn_trait)]
#![feature(conservative_impl_trait)]
#![feature(underscore_lifetimes)]

fn a<T>(items: &[T]) -> Box<impl Iterator> {
    Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
}

fn main() { }

I get:

error[E0621]: explicit lifetime required in the type of `items`
 --> src/main.rs:5:29
  |
5 | fn a<T>(items: &[T]) -> Box<impl Iterator> {
  |         -----               ^^^^^^^^^^^^^ lifetime `'static` required
  |         |
  |         consider changing the type of `items` to `&'static [T]`

What's going wrong here is that impl Iterator can only capture lifetimes that it names, and this one doesn't name any lifetimes. It can be fixed by changing to impl Iterator + '_ -- this is probably what we want to suggest? I'm not sure how best to phrase this, suggestions welcome.

cc @cramertj @estebank

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Feb 23, 2018

I've seen other cases of this too, it'd be great to improve it.

error[E0XXX]: ambiguous lifetime for `impl Iterator`
 --> src/main.rs:5:29
  |
5 | fn a<T>(items: &[T]) -> Box<impl Iterator> {
  |                             ^^^^^^^^^^^^^ this trait bound implies lifetime `'static`
help: add a lifetime binding to remove the `'static` requirement
  |
5 | fn a<T>(items: &[T]) -> Box<impl Iterator + '_> {
  |                             ^^^^^^^^^^^^^^^^^^
help: otherwise, change the type of `items` to `&'static [T]` to satisfy the `'static` requirement
  |
5 | fn a<T>(items: &'static [T]) -> Box<impl Iterator> {
  |                ^^^^^^^^^^^^

@nikomatsakis

This comment has been minimized.

Copy link
Contributor Author

nikomatsakis commented Feb 23, 2018

@estebank

Suggesting both might be good. However, this label is inaccurate:

this trait bound implies lifetime 'static

That trait bound forbids us from capturing the anonymous lifetime, because it is not mentioned anywhere, but it does not imply 'static. For example, we could capture values of type T, which is not known to be static. Obviously we won't be able to express all that subtlety, but maybe we can say something like "impl Trait types can only capture lifetimes that they reference"?

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Feb 23, 2018

Your suggested text seems great to me for the label, while this is one of the error messages that would benefit greatly from --teach.

@kornelski

This comment has been minimized.

Copy link
Contributor

kornelski commented Jan 5, 2019

This comes up in Futures-heavy code a lot. In such cases the real solution is to use Arc, but users are sent chasing impossible lifetime annotations.

My suggestion is: No matter what, don't say 'static.

Users don't write Box< + 'static> explicitly by themselves. They see it only in the context of &'static str or static FOO: &'static.

When Rust points to a &-argument, and says it has to be 'static, users understand it literally that arg: &'static Foo is the only allowed solution, which leads to despair.

"Temporary references are not allowed here" may be guiding them towards better solutions.

@kornelski

This comment has been minimized.

Copy link
Contributor

kornelski commented Jan 5, 2019

Another case from user forum:

type EvalFn = Fn() -> (u8);                            
fn take_callback(f: &EvalFn) -> u8 {
    1
} 

fn test(arg: Option<&u8>) -> (u8) {
    let closure = || arg.cloned().unwrap();
    take_callback(&closure)
}  

The error points that the argument should be 'static rather than the real (quite unobvious) solution of type EvalFn<'a> = Fn() -> (u8) + 'a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment