Rust: Subpar error when lifetime checking for `dyn Trait` evaluates to `'static`

Created on 24 Aug 2019  路  2Comments  路  Source: rust-lang/rust

fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T>> {
    Box::new(x.iter())
}

fn main() {
    let x: Vec<usize> = vec![1, 2, 3];
    let _ = foo(&x);
}

the rustc output is

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
 --> src/main.rs:4:16
  |
4 |     Box::new(x.iter())
  |                ^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 3:1...
 --> src/main.rs:3:1
  |
3 | / fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T>> {
4 | |     Box::new(x.iter())
5 | | }
  | |_^
note: ...so that reference does not outlive borrowed content
 --> src/main.rs:4:14
  |
4 |     Box::new(x.iter())
  |              ^
  = note: but, the lifetime must be valid for the static lifetime...
  = note: ...so that the expression is assignable:
          expected std::boxed::Box<(dyn std::iter::Iterator<Item = &T> + 'static)>
             found std::boxed::Box<dyn std::iter::Iterator<Item = &T>>

and with NLL enabled

error: lifetime may not live long enough
 --> src/main.rs:4:5
  |
3 | fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T>> {
  |              - let's call the lifetime of this reference `'1`
4 |     Box::new(x.iter())
  |     ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`

The output should be similar to what we suggest in the impl Trait case:

error: lifetime may not live long enough
 --> src/main.rs:4:5
  |
3 | fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T>> {
  |              |               -------------------- help: you can constrain this trait to comply with '1: `dyn Iterator<Item=&T> + '_`
  |              |
  |              let's call the lifetime of this reference `'1`
4 |     Box::new(x.iter())
  |     ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
  help: for more details, visit https://doc.rust-lang.org/stable/reference/lifetime-elision.html#default-trait-object-lifetimes

CC @obliquemotion @yaahc

A-NLL A-diagnostics A-lifetimes A-suggestion-diagnostics C-enhancement D-newcomer-roadblock NLL-diagnostics T-compiler

Most helpful comment

:heart_eyes:

All 2 comments

Current output:

error: cannot infer an appropriate lifetime
 --> src/main.rs:2:16
  |
1 | fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T>> {
  |              ------- data with this lifetime...
2 |     Box::new(x.iter())
  |     -----------^^^^--- ...is captured and required to be `'static` here
  |
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 1:1
  |
1 | fn foo<T>(x: &Vec<T>) -> Box<dyn Iterator<Item=&T> + '_> {
  |                                                    ^^^^

:heart_eyes:

Was this page helpful?
0 / 5 - 0 ratings