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

Unexpected static lifetime requirement for nested boxed closures with generic parameters #134835

Open
marmeladema opened this issue Dec 27, 2024 · 3 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@marmeladema
Copy link
Contributor

I tried this code:

type BoxedClosure<U> =
    Box<dyn for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static>;

pub struct CompiledExpr<U>(BoxedClosure<U>);

impl<U> CompiledExpr<U> {
    pub fn new(
        closure: impl for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static,
    ) -> Self {
        CompiledExpr(Box::new(closure))
    }
}

pub struct Context<'e, U> {
    data: &'e str,
    userdata: U,
}

pub fn compile1<U>() -> CompiledExpr<U> {
    CompiledExpr::new(|ctx| ctx.data == "rust")
}

pub fn compile2<U>() -> CompiledExpr<U> {
    let e = compile1::<U>();
    CompiledExpr::new(move |ctx| (e.0)(ctx))
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c96c451d36a64d657cdfecc7c5185f31

I expected to see this happen: successful compilation

Instead, this happened:

error[E0310]: the parameter type `U` may not live long enough
  --> src/lib.rs:25:5
   |
25 |     CompiledExpr::new(move |ctx| (e.0)(ctx))
   |     ^^^^^^^^^^^^^^^^^
   |     |
   |     the parameter type `U` must be valid for the static lifetime...
   |     ...so that the type `U` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
23 | pub fn compile2<U: 'static>() -> CompiledExpr<U> {
   |                  +++++++++

I am not exactly sure its a bug per say, but from my perspective, this is relatively surprising that this code fails to build.
The 'static bound is on the boxed dyn object itself and not on the parameters of the boxed closure.
Its not clear why U should be 'static when Context which holds U is not.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
@marmeladema marmeladema added the C-bug Category: This is a bug. label Dec 27, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 27, 2024
@compiler-errors
Copy link
Member

So a closure outlives 'static if all of its captures outlive 'static. In the second function compile2, the closure captures the type CompiledExpr<U>. CompiledExpr<U>: 'static only if U: 'static. So that's the source of the inconsistency here.

@marmeladema
Copy link
Contributor Author

That's really unfortunate because it was never my intention to capture U since it is supposed to only be accessible / usable when calling the closure, not "building" it.

Another inconsistency is that using an impl trait in the return type of compile1 just works! I would have expected the rules for a dyn trait vs impl trait to be similar:

type BoxedClosure<U> =
    Box<dyn for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static>;

pub struct CompiledExpr<U>(BoxedClosure<U>);

impl<U> CompiledExpr<U> {
    pub fn new(
        closure: impl for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static,
    ) -> Self {
        CompiledExpr(Box::new(closure))
    }
}

pub struct Context<'e, U> {
    data: &'e str,
    userdata: U,
}

pub fn compile1<U>() -> impl for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static  {
    |ctx| ctx.data == "rust"
}

pub fn compile2<U>() -> CompiledExpr<U> {
    let e = compile1::<U>();
    CompiledExpr::new(move |ctx| e(ctx))
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a109e8ee8ce903af1b0fc2cdafe8c21d

@marmeladema
Copy link
Contributor Author

Another more contrived example which exhibits the problem:

pub struct Context<'e, U> {
    data: &'e str,
    userdata: U,
}

pub fn to_opaque<U>(e: Box<dyn for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static>) -> impl for<'e> Fn(&'e Context<'e, U>) -> bool + Sync + Send + 'static {
    e
}

Which generates the following the compilation error:

error[E0310]: the parameter type `U` may not live long enough
 --> src/lib.rs:7:5
  |
7 |     e
  |     ^
  |     |
  |     the parameter type `U` must be valid for the static lifetime...
  |     ...so that the type `U` will meet its required lifetime bounds

@jieyouxu jieyouxu added T-types Relevant to the types team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2025
@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants