` for<'a> &'a T ` seems to require ` T: 'static `
⚓ Rust 📅 2025-12-26 👤 surdeus 👁️ 1Hi,
On my project, I'm facing some weird error, with a type required to outlive 'static somehow.
Here's a reduced version of my code:
// RUN THIS CODE WITH THE `-Znext-solver` RUST FLAG
use std::ops::Deref;
trait GenerateName {
fn generate_name(self) -> String;
}
// This allows to implement GenerateName for `&&&&T` (and so on) where `&T: GenerateName`
impl<'a, T, S> GenerateName for &'a T
where
S: 'a,
&'a S: GenerateName,
T: Deref<Target = S>,
{
fn generate_name(self) -> String {
(**self).generate_name()
}
}
struct NameGenerator<'a> {
example_field: &'a (),
}
impl GenerateName for &NameGenerator<'_> {
fn generate_name(self) -> String {
"Rick Astley".into()
}
}
fn func<Generator>(generator: Generator)
where
for<'a> &'a Generator: GenerateName, // I use this to be able to call `generate_name` multiple times
{
generator.generate_name();
generator.generate_name();
}
fn main() {
let counter = ();
let context = NameGenerator {
example_field: &counter,
};
func(&context);
}
This code only produces my error with the new trait solver, for some reason (with the current trait solver, it gives an overflow error). I've tried it on nightly (rustc 1.94.0-nightly (2ca7bcd03 2025-12-23)), but maybe it "works" on stable/beta.
When I run this code, it says that counter must be borrowed for 'static, because func requires for<'a> &'a Generator: GenerateName. As a hint, it says due to a current limitation of the type system, this implies a `'static` lifetime.
~~~ a current limitation of the type system ~~~
Also, when I call func(context) without the reference, it works, so maybe it has to do with my generic implementation on Derefs.
Can someone explain why there is this error, and what it means?
6 posts - 4 participants
🏷️ Rust_feed