# Idea: aliasing the \`'static\` lifetime for lifetime parameters / trait bounds, e.g., \`'auto\`

**URL:** <https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117>\
**Category:** language design\
**Created:** [July 9, 2023, 6:17pm UTC](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117 "2023-07-09T18:17:10Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 11, 2023, 5:39am UTC](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117/8 "2023-07-11T05:39:08Z")

</div>

> [@Sunshine](#):
>
> `pub trait Text = Borrow<str>;`

Trait aliases are unstable. FYI, because of this, you almost certainly will run into edge cases where the compiler isn't as helpful as we'd like it to be. If you're a beginner and not familiar with Rust, you absolutely should not be enabling any unstable features.

If the problematic middle step had been written without a trait alias:

```rust
pub enum Intermediate<T: Borrow<str> + Into<Cow<'a, str>>> {
    SingleField(T),
    DoubleField { first: T, second: T },
}

pub trait ToIntermediate<T> {
    type Text: Borrow<str> + Into<Cow<'a, str>>;
    fn to_intermediate(&self) -> Intermediate<Self::Text>;
}

```

you'd get the error of

```plaintext
error[E0261]: use of undeclared lifetime name `'a`
 --> src/lib.rs:4:49
  |
4 | pub enum Intermediate<T: Borrow<str> + Into<Cow<'a, str>>> {
  | ^^ undeclared lifetime
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
  |
4 | pub enum Intermediate<T: Borrow<str> + for<'a> Into<Cow<'a, str>>> {
  | +++++++
help: consider introducing lifetime `'a` here
  |
4 | pub enum Intermediate<'a, T: Borrow<str> + Into<Cow<'a, str>>> {
  | +++

error[E0261]: use of undeclared lifetime name `'a`
  --> src/lib.rs:13:39
   |
13 | type Text: Borrow<str> + Into<Cow<'a, str>>;
   | ^^ undeclared lifetime
   |
help: consider making the bound lifetime-generic with a new `'a` lifetime
   |
13 | type Text: Borrow<str> + for<'a> Into<Cow<'a, str>>;
   | +++++++
help: consider introducing lifetime `'a` here
   |
13 | type Text<'a>: Borrow<str> + Into<Cow<'a, str>>;
   | ++++
help: consider introducing lifetime `'a` here
   |
12 | pub trait ToIntermediate<'a, T> {
   | +++

```

which I think might lead to a bit more productive of an error fix cycle.

* * *

If there's a "root" issue here, it's that std provides

```rust
impl<'a> From<String> for Cow<'a, str>
// and
impl<'a> From<&'a str> for Cow<'a, str>
// but not
impl<'a> From<&'static str> for Cow<'a, str>

// without overlapping impls, it'd be spelled
impl<'short, 'long: 'short> From<&'long str> for Cow<'short, str>
// but this would cause a lot of inference breakage
// as Cow::from("str") would have multiple candidates

```

A general fix might be for the compiler to synthesize more general impls when variant types are involved, or at least to note this in the "lifetime not general enough" error.

> [@Sunshine](#):
>
> the compiler mentioned before:
> 
> ```rust
> = note: `From<&'static str>` would have to be implemented for the type `Cow<'0, str>`, for any lifetime `'0`...
> = note: ...but `From<&'1 str>` is actually implemented for the type `Cow<'1, str>`, for some specific lifetime `'1`
> 
> ```
> 
> One would naturally think that for `Into<Cow<'static, str>>` to be implemented, a `String` with `'static` lifetime would be needed which, surprisingly, is not the case.

I believe the miscommunication with the compiler happened somewhere around here — the error message is about the _`&str`_ implementation, and _not_ the `String` implementation. `String` satisfies the `for<'a> Into<Cow<'a, str>>` bound you asked for, but `&str` doesn't.

> [@Sunshine](#):
>
> In other words, `From<String>` is implemented for `Cow<'static, str>` even if the `String` itself might not have a `'static` lifetime, and the documentation stated:
> 
> ```rust
> impl<'a> From<String> for Cow<'a, str>
> 
> ```
> 
> instead of:
> 
> ```rust
> impl From<String> for Cow<'static, str>
> 
> ```
> 
> although `Cow<'static, str>` is a [subtype](https://doc.rust-lang.org/reference/subtyping.html) of `Cow<'a, str>` and could be [coerced](https://doc.rust-lang.org/reference/type-coercions.html#coercion-types) to it.

The reason that `Cow<'static, str>: From<String>` holds is that the implementation is provided for `impl<'a>`. This doesn't mean anything w.r.t. the `String`, because `String` doesn't have any lifetime parameters.

I find that subtyping is a horrible way of thinking about generic applicability. It's necessary for coercions, but what's useful for generics is the "outlives" relation and the "forall" binder. The impl is provided with `impl<'a>`, and no bounds are placed on `'a`; this means that when you use the implementation, you're allowed to choose any lifetime you fancy, including `'static`.

Nowadays the impl might be written as `impl From<String> for Cow<'_, str>` instead, which based on the explanation of your logic chain, I expect you would have intuited better. Many existing Rust devs are actually the other way, and prefer the named but unbound lifetime.

> [@Sunshine](#):
>
> Since I've spent so much time to figure out how to set such a simple matter straight. I couldn't help but ask myself: is that effort something necessary for a Rust beginner who just wants to treat `&'static str`s and `String`s in a generic way?

Generalizing like this is unfortunately _not_ such a simple matter. It means requiring running headfirst into lifetime problems, and any time you're considering or the compiler starts suggesting "higher-ranked" lifetimes (`for<'a>`) you're venturing off anything remotely close to the "simple" path. "Lifetime not general enough" is a contender for the worst and most difficult to improve error that the compiler will emit.

The general advice is two bits:

- Any time you're expressing a function signature of the shape `fn() -> for<'a> &'a T`, you want to say `fn() -> &'static T` instead. This holds for any "covariant" lifetime (most of them).
- To accept either `&'static str` or `String`, don't use generics. Just use `Cow<'static, str>` instead. (For `&'a str`, `Cow<'a, str>`.)

---

_[View the full topic](https://internals.rust-lang.org/t/idea-aliasing-the-static-lifetime-for-lifetime-parameters-trait-bounds-e-g-auto/19117)._
