Description
Concretely, consider the following code which compiles perfectly fine under both stable and nightly rust
use std::marker::PhantomData;
const CONSTANT_M: usize = 4;
struct Foo<T, const GENERIC_M: usize> {
content: PhantomData<[T; GENERIC_M]>
}
struct Foobar<'a, T>
where T: 'a
{
data: &'a T
}
fn create_foo<'a, F>(_: F) -> &'a Foo<i32, CONSTANT_M>
where F: FnMut(i64) -> ()
{
Box::leak(Box::new(Foo { content: PhantomData }))
}
fn create_foobar() -> Foobar<'static, Foo<i32, CONSTANT_M>> {
let create = |_: i64| ();
Foobar { data: create_foo::<'static, _>(|y| create(y)) }
}
This makes complete sense, since the lifetime parameters on the function create_foo()
clearly specify that F
must be valid in the body of the function, but not necessarily for the whole lifetime 'a
. Note also that the code uses const generics, but only the subset that has been stabilized.
However, just adding #![feature(generic_const_exprs)]
without changing anything else gives a compiler error
closure may outlive the current function, but it borrows `create`, which is owned by the current function
within create_foobar()
. This should not be the case, as the closure stored in the variable create
does not have to outlive the current function, or even live for 'static
.
Playground
I am using rustc 1.77.0-nightly.
Edit Significantly simplified example code.