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

TAIT: allow using '_ to reference the current lifetime #138620

Open
erickt opened this issue Mar 17, 2025 · 0 comments
Open

TAIT: allow using '_ to reference the current lifetime #138620

erickt opened this issue Mar 17, 2025 · 0 comments
Labels
C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@erickt
Copy link
Contributor

erickt commented Mar 17, 2025

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)
@erickt erickt added the C-bug Category: This is a bug. label Mar 17, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 17, 2025
@tmandry tmandry added the F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` label Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
Development

No branches or pull requests

3 participants