# Semantics of nested \`impl Trait\` + HRTBs

**URL:** <https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492>\
**Category:** language design\
**Created:** [April 18, 2022, 8:05am UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492 "2022-04-18T08:05:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliemjay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aliemjay/32/9301_2.png) [@aliemjay](https://internals.rust-lang.org/u/aliemjay)\
**Post date:** [April 18, 2022, 8:05am UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/1 "2022-04-18T08:05:17Z")

</div>

I noticed that that [Lazy type-alias-impl-trait take two by oli-obk · Pull Request #94081 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/94081) makes the following code pass:

```rust
trait Tr<'a> {
	type Assoc;
}

impl<'a> Tr<'a> for () {
	type Assoc = ();
}

fn f() -> impl for<'a> Tr<'a, Assoc = impl Copy + 'a> {}

```

But I can't find a documentation of the intended semantics in this case.

From the following test it seems that it allows the existential `impl Copy + 'a` to be paramterized by `'a`, and that's quite surprising behavior since adding a lifetime makes the test fails, which would pass otherwise.

```rust
fn check<Assoc>(_: impl for<'a> Tr<'a, Assoc = Assoc>) {}
fn main() { check(f()); }

```

[playground link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f913122ccd585c1b8e5a363a1641e5f4)

see also [yet another ICE with HRTB and RPIT · Issue #95647 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/95647)

cc @oli-obk

---

<div class="post-metadata">

**Author:** ![aliemjay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aliemjay/32/9301_2.png) [@aliemjay](https://internals.rust-lang.org/u/aliemjay)\
**Post date:** [April 18, 2022, 9:03am UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/2 "2022-04-18T09:03:23Z")

</div>

In other words, I'm asking if

```rust
fn f() -> impl for<'a> Tr<'a, Assoc = impl Copy + 'a> {}

```

should be equivalent to:

```rust
type Assoc = impl for<'a> Copy + 'a;
fn f() -> impl for<'a> Tr<'a, Assoc = Assoc> {}

```

or:

```rust
type Assoc<'a> = impl Copy + 'a;
fn f() -> impl for<'a> Tr<'a, Assoc = Assoc<'a>> {}

```

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [April 18, 2022, 12:43pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/3 "2022-04-18T12:43:22Z")

</div>

I would think it should be the second one

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [April 18, 2022, 12:52pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/4 "2022-04-18T12:52:28Z")

</div>

If the lifetime is used in the assoc type in your example

```rust

impl<'a> Tr<'a> for () {
	type Assoc = &'a ();
}

```

the compiler ICEs. Though I do expect it to be a breaking change to add a lifetime to an `impl Trait`

---

<div class="post-metadata">

**Author:** ![aliemjay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aliemjay/32/9301_2.png) [@aliemjay](https://internals.rust-lang.org/u/aliemjay)\
**Post date:** [April 18, 2022, 2:13pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/5 "2022-04-18T14:13:49Z")

</div>

> [@oli-obk](#):
>
> I would think it should be the second one

I think we really need a decision here. For me it would be an inconsistent and and odd behavior.

For example, if `for<'a> Tr<'a, Assoc = impl Copy + 'a>` allows `Assoc` to be paramterized by `'a`, I would expect the same behavior from `for<'a> Tr<'a, Assoc = impl Copy>` as well! Adding a lifetime bound shouldn't change semantics in this regard.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [April 18, 2022, 7:09pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/6 "2022-04-18T19:09:56Z")

</div>

~~Agreed. I would expect that `impl Copy` to have access to `'a`, just like the `impl` outside the `for<'a>` has access to generics of the surrounding function~~

EDIT: I wasn't paying attention when I wrote this. We do need the `+ 'a` just like we need it for non-nested impls.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [April 18, 2022, 7:11pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/7 "2022-04-18T19:11:37Z")

</div>

Can you summarize this in an issue so we can inform the lang team about the unintended stabilization and let them make a decision on it?

Otherwise i'll do that next week, I'm on vacation right now without a computer

---

<div class="post-metadata">

**Author:** ![aliemjay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aliemjay/32/9301_2.png) [@aliemjay](https://internals.rust-lang.org/u/aliemjay)\
**Post date:** [April 18, 2022, 11:12pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/8 "2022-04-18T23:12:36Z")

</div>

opened an issue [nested RPIT and HRTB: unclear semantics and future incompatibility · Issue #96194 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/96194)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 17, 2022, 11:13pm UTC](https://internals.rust-lang.org/t/semantics-of-nested-impl-trait-hrtbs/16492/9 "2022-07-17T23:13:16Z")

</div>

Closed on July 17, 2022, 11:13pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
