Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"borrowed value does not live long enough" with a generic function that returns impl trait

I get an unexpected error from this Rust code:

struct Container<'a> {
    x: &'a i32,
}

trait Reply {}
impl Reply for i32 {}

fn json<T>(_val: &T) -> impl Reply {
    3
}

fn f() -> impl Reply {
    let i = 123;
    let a = Container { x: &i };
    json(&a)
}

Playground

The error is:

error[E0597]: `i` does not live long enough
  --> src/lib.rs:14:28
   |
12 | fn f() -> impl Reply {
   |           ---------- opaque type requires that `i` is borrowed for `'static`
13 |     let i = 123;
14 |     let a = Container { x: &i };
   |                            ^^ borrowed value does not live long enough
15 |     json(&a)
16 | }
   | - `i` dropped here while still borrowed

Why?

If I change the declaration of json() to either of these versions, the code compiles:

fn json(val: &Container) -> impl Reply
fn json<T>(val: &T) -> i32

It is only when there is both a type parameter and a returned trait object that the compiler rejects the code.

This is a reduction from a real issue we had with warp::reply::json(), but I would prefer to understand it in general.

like image 813
Daniel Darabos Avatar asked Sep 17 '19 14:09

Daniel Darabos


1 Answers

When the arguments and the return type of a function are generic, the Rust compiler assumes that the return type could, potentially, borrow the arguments. That's why it assumes that f() returns a value referencing the local variable i.

I'm not entirely sure, but I think this is desired, because someone could implement Reply for a type where this would be problematic.

EDIT: This doesn't work because of a bug. It has already been reported on GitHub.

like image 105
Aloso Avatar answered Oct 05 '22 01:10

Aloso