Skip to content

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

Closed

Description

@nikomatsakis

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    on Feb 23, 2018
  2. estebank commented on Feb 23, 2018

    @estebank
    Contributor

    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> {
      |                ^^^^^^^^^^^^
    
    
  3. nikomatsakis commented on Feb 23, 2018

    @nikomatsakis
    ContributorAuthor

    @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"?

  4. estebank commented on Feb 23, 2018

    @estebank
    Contributor

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

  5. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Apr 23, 2018
  6. kornelski commented on Jan 5, 2019

    @kornelski
    Contributor

    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.

  7. kornelski commented on Jan 5, 2019

    @kornelski
    Contributor

    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.

  8. nox commented on Mar 4, 2019

    @nox
    Contributor

    I hit this when implementing inert_derive.

    https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dcbcd6a9d8e7b989ac3f574044557c64

    In my case I can't seem to find a way to tell rustc that my impl ToTokens value is 'static.

  9. estebank commented on Mar 4, 2019

    @estebank
    Contributor

    A separate situation that would need to be handled is the following:

    #![feature(dyn_trait)]
    #![feature(conservative_impl_trait)]
    #![feature(underscore_lifetimes)]
    
    fn a<'a, T>(items: &'a [T]) -> Box<impl Iterator> {
        Box::new(items.iter())
    }
    
    fn main() { }
    

    where the suggestion would need to be to add + 'a constraint, but currently outputs the general lifetime error:

    error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
     --> file.rs:6:20
      |
    6 |     Box::new(items.iter())
      |                    ^^^^
      |
    note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 5:6...
     --> file.rs:5:6
      |
    5 | fn a<'a, T>(items: &'a [T]) -> Box<impl Iterator> {
      |      ^^
    note: ...so that reference does not outlive borrowed content
     --> file.rs:6:14
      |
    6 |     Box::new(items.iter())
      |              ^^^^^
      = note: but, the lifetime must be valid for the static lifetime...
    note: ...so that return value is valid for the call
     --> file.rs:5:36
      |
    5 | fn a<'a, T>(items: &'a [T]) -> Box<impl Iterator> {
      |                                    ^^^^^^^^^^^^^
    
  10. added 2 commits that reference this issue on Apr 1, 2019
    4a9e791
    0d01fba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions