Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#![feature(generic_const_exprs)] does not compile stable code related to lifetimes #124531

Open
FeanorTheElf opened this issue Apr 29, 2024 · 0 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@FeanorTheElf
Copy link

FeanorTheElf commented Apr 29, 2024

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)) }
}

Playground

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.

@FeanorTheElf FeanorTheElf added the C-bug Category: This is a bug. label Apr 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 29, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants