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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With