rust-lang / rust-lang/rust

Incorrect `'static` lifetime bound error with associated type defaults

Open
#150,081 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reproducer in src/lib.rs with rustc 1.93.0-nightly and compare the current diagnostics with the desired output. The completed fix should attribute the suggested 'static bound to A rather than F, or produce the correct format for requiring the new bound.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.