TAIT: allow using '_ to reference the current lifetime
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
We were doing some experimentation with type alias impl trait to make a macro that can associate compile-time constants with inferred types, and I think we're running into either a bug or unimplemented feature. We're hoping to use this so we can take a println!(...) -ish format string and access associated constants at compile time for runtime values. Unfortunately it seems Rust wants us to provide the name of the current lifetime, but it doesn't accept '_. Here's a simplified version of what we're trying to do:
#![feature(type_alias_impl_trait)]
trait MyTrait {
const ARG: &'static str;
}
impl MyTrait for &str {
const ARG: &'static str = "%s";
}
fn main() {
let string = String::new();
type T<'a> = impl MyTrait;
let _: T<'_> = string.as_str();
const ARG: &str = T::ARG;
println!("{:?}", ARG);
}
This results in this error:
Compiling playground v0.0.1 (/playground)
error[E0792]: expected generic lifetime parameter, found `'_`
--> src/main.rs:15:12
|
14 | type T<'a> = impl MyTrait;
| -- this generic parameter must be used with a generic lifetime parameter
15 | let _: T<'_> = string.as_str();
| ^^^^^
error[E0597]: `string` does not live long enough
--> src/main.rs:15:20
|
12 | let string = String::new();
| ------ binding `string` declared here
...
15 | let _: T<'_> = string.as_str();
| ----- ^^^^^^ borrowed value does not live long enough
| |
| type annotation requires that `string` is borrowed for `'static`
...
19 | }
| - `string` dropped here while still borrowed
However it does work if we can use a named lifetime:
#![feature(type_alias_impl_trait)]
trait MyTrait {
const ARG: &'static str;
}
impl MyTrait for &str {
const ARG: &'static str = "%s";
}
fn func<'b>(s: &'a str) {
type T<'a> = impl MyTrait;
let _: T<'b> = s;
const ARG: &str = T::ARG;
println!("{:?}", ARG);
}
fn main() {
let string = String::new();
func(string.as_str());
}
Meta
rustc --version --verbose:
1.87.0-nightly (2025-03-16 227690a258492c84ae99)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reduced type_alias_impl_trait example and the reported E0792 and E0597 diagnostics on nightly rustc 1.87.0. Trace how lifetime arguments for type alias impl Trait are handled, then add coverage showing whether T<'_> can refer to the current lifetime and verify the associated constant use without requiring a named lifetime.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100