Skip to content

Incorrect 'static lifetime bound error with associated type defaults #150081

@DexterHill0

Description

@DexterHill0

Code

#![feature(associated_type_defaults)] struct Bar<A>(A); trait Foo<A> { type Inner<'a> = &'a mut Bar<A> where A: 'a; fn expects_inner(&self, inner: Self::Inner<'_>); } impl<A> Bar<A> { fn something<F>(&mut self, foo: F) where for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>>, { foo.expects_inner(self); let _ = Box::new(|| {}); } }

Current output

error: `A` does not live long enough --> src/lib.rs:19:17 | 19 | let _ = Box::new(|| {}); | ^^^^^^^^ | note: due to a current limitation of the type system, this implies a `'static` lifetime  --> src/lib.rs:14:20  | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>>,  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider restricting the type parameter to the `'static` lifetime  | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>> + 'static,  | +++++++++  error: `A` does not live long enough  --> src/lib.rs:19:17  | 19 | let _ = Box::new(|| {});  | ^^^^^^^^^^^^^^^  | note: due to a current limitation of the type system, this implies a `'static` lifetime  --> src/lib.rs:14:20  | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>>,  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider restricting the type parameter to the `'static` lifetime | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>> + 'static, | +++++++++ error: `A` does not live long enough --> src/lib.rs:19:26 | 19 | let _ = Box::new(|| {}); | ^^^^^ | note: due to a current limitation of the type system, this implies a `'static` lifetime  --> src/lib.rs:14:20  | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>>,  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider restricting the type parameter to the `'static` lifetime  | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>> + 'static,  | +++++++++

Desired output

error: `A` does not live long enough --> src/lib.rs:19:17 | 19 | let _ = Box::new(|| {}); | ^^^^^^^^ | note: due to a current limitation of the type system, this implies a `'static` lifetime help: consider restricting the type parameter to the `'static` lifetime | 14 | A: 'static,  | +++++++++++  error: `A` does not live long enough  --> src/lib.rs:19:17  | 19 | let _ = Box::new(|| {});  | ^^^^^^^^^^^^^^^  | note: due to a current limitation of the type system, this implies a `'static` lifetime help: consider restricting the type parameter to the `'static` lifetime  | 14 | A: 'static, | +++++++++++ error: `A` does not live long enough --> src/lib.rs:19:26 | 19 | let _ = Box::new(|| {}); | ^^^^^ | note: due to a current limitation of the type system, this implies a `'static` lifetime help: consider restricting the type parameter to the `'static` lifetime | 14 | A: 'static,  | +++++++++++  (or whatever is the correct error format for requiring a new bound)

Rationale and extra context

If I understand correctly, the limitation of the trait system is referring to the fact that closure will capture all the generics from the outer environment. In the example code, it will capture both A and F, but because of the Box, it requires that A must be 'static. This is stated in the error message:

error: `A` does not live long enough 

However, the diagnostic help text incorrectly says to apply the 'static bound to F, which is wrong. Even doing so, it will just continue to ask for 'static to be added to F:

 | 14 | for<'a> F: Foo<A, Inner<'a> = &'a mut Bar<A>> + 'static + 'static, | +++++++++ 

Instead, adding A: 'staticwill fix the error.

Other cases

Rust Version

rustc 1.93.0-nightly (b84478a1c 2025-11-30) binary: rustc commit-hash: b84478a1c477756cd3e1974eda867a6bb31e8902 commit-date: 2025-11-30 host: x86_64-pc-windows-msvc release: 1.93.0-nightly LLVM version: 21.1.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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