Skip to content

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

Open

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions