Skip to content

Suggest adding a lifetime constraint for opaque type#67595

Merged
bors merged 2 commits intorust-lang:masterfrom
ohadravid:impl-trait-does-not-live-long-enough
Jan 3, 2020
Merged

Suggest adding a lifetime constraint for opaque type#67595
bors merged 2 commits intorust-lang:masterfrom
ohadravid:impl-trait-does-not-live-long-enough

Conversation

@ohadravid
Copy link
Copy Markdown
Contributor

Fixes #67577, where code like this:

struct List {
  data: Vec<String>,
}
impl List {
  fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
    self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
  }
}

will show this error:

   Compiling playground v0.0.1 (/playground)
error[E0597]: `prefix` does not live long enough
 --> src/lib.rs:6:47
  |
5 |   fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
  |                   -- lifetime `'a` defined here     --------------------------- opaque type requires that `prefix` is borrowed for `'a`
...

but without suggesting the lovely help: you can add a constraint...

r? @estebank

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-sense diagnostic message from lifetime checker

8 participants