Skip to content

Confusing and incorrect lifetime diagnostic message #21642

Closed
@rtkrruvinskiy

Description

@rtkrruvinskiy

Trying to compile the code at this playpen gives the following error:

<anon>:18:20: 18:26 error: cannot infer an appropriate lifetime due to conflicting requirements
<anon>:18         match iter.next() {
                             ^~~~~~
<anon>:17:5: 25:6 help: consider using an explicit lifetime parameter as shown: fn lookup(&self, mut iter: Box<Iterator<Item = Key<'b>>>) -> bool

There are a couple of issues here from my perspective:

  1. The function signature suggested by the compiler cannot actually compile since it uses an undefined lifetime.
  2. It's really unclear to me what the "conflicting requirements" are that the error message alludes to. Intuitively, I would have thought that the code should work as long as the lifetime of the iterator item is as long as the scope of the lookup method. In other words, my expectation would be that I can test whether a key is a member of the container without the lifetime of the instance of the key used for testing having to be as long as the lifetime of the container. However, that appears not to be the case, which seems counter-intuitive and limiting (since there doesn't appear to be an actual safety issue as far as I can see). It would be helpful to have scenarios like this discussed somewhere in the documentation. If such discussion already exists and I have overlooked it, my apologies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions