# Pre-RFC: returning automatically generating impl Trait

**URL:** <https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090>\
**Category:** language design\
**Created:** [September 17, 2020, 6:28pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090 "2020-09-17T18:28:52Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [September 18, 2020, 11:39am UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/21 "2020-09-18T11:39:24Z")

</div>

> [@dhm](#):
>
> ```rust
> match condition {
> | true => create_iterator(),
> | false => generate_iterator(),
> } as AutoEnum<impl Iterator, impl Iterator>
> 
> ```

I would expect this to instead be

```rust
match condition {
    | true => create_iterator() as AutoEnum<impl Iterator, impl Iterator>,
    | false => generate_iterator(),
}

```

`as` after the match does not influence `Box` coercions currently, so I don't think it would influence any stack box. Then this is getting very close to something [that can be implemented by user code](https://github.com/Nemo157/impl_sum/blob/master/examples/first.rs) (other than needing variadic generics to be able to define it for an arbitrary arity).

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [September 18, 2020, 1:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/22 "2020-09-18T13:05:24Z")

</div>

@Nemo157 oh sure, I was hinting at what the most aesthetic (because symmetrical) situation would be; but if "back-tracking" type inference can only go so far (IIRC, it does work for things such as `&mut dyn Iterator...`), then so be it.

> **Aside**
>
> > [@Nemo157](#):
> >
> > something [that can be implemented by user code](https://github.com/Nemo157/impl_sum/blob/master/examples/first.rs)
> 
> Oh indeed, that's kind of what I had in mind, but we do hit the issue for both an automated way to opt into delegation for custom traits, and the lack of variadic generics, although the latter is not a real problem, provided we are given enough impls, such as your `Z Y X ...` (nit: I would rename those to `_25 ... _2 _1 _0`, since I find `sum_types::_2` to be more readable than `sum_types::C` 😉)
> 
> But maybe you can publish that crate, "now" that proc-macros are stable?

---

<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:** [September 18, 2020, 1:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/23 "2020-09-18T13:16:16Z")

</div>

> [@dhm](#):
>
> Also for retro-compatibility reasons (in this case, adding unexpected implementors of some traits can cause logic bugs or even unsoundness ( _c.f._ , these layout-based `unsafe` traits). So it needs to be something `opt-in` , for _each_ and every trait.

It's always\* sound to have an incorrect implementation of a trait which is not `unsafe`. Thus it makes sense that "auto enum `dyn`" be limited to safe traits, since the generated implementation may be incorrect w.r.t. `unsafe` invariants on the trait.

\* modulo traits (ab)using privacy barriers to require a closed set of implementations. So, add another requirement: it must be possible to implement the trait at the point where the "auto enum `dyn`" is constructed. This is _even more complicated_ a property than the "constructability" constraint for the Safer Transmute RFC.

---

<div class="post-metadata">

**Author:** ![robinm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/robinm/32/6255_2.png) [@robinm](https://internals.rust-lang.org/u/robinm)\
**Post date:** [September 18, 2020, 1:19pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/24 "2020-09-18T13:19:53Z")

</div>

> [@dhm](#):
>
> But maybe you can publish that crate, "now" that proc-macros are stable?

Given that:

- anyway an explicit marker seems to be needed
- it's already possible to create this construction using user code

Then I also think that experimenting with a crate is the right way to advance forward (a bit like `try!` that evolved into `?`).

---

<div class="post-metadata">

**Author:** ![taiki-e](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taiki-e/32/5017_2.png) [@taiki-e](https://internals.rust-lang.org/u/taiki-e)\
**Post date:** [September 18, 2020, 1:45pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/25 "2020-09-18T13:45:18Z")

</div>

> [@robinm](#):
>
> - anyway an explicit marker seems to be needed

Even if proc-macro implementations, [can make it work without explicit markers in many cases by analyzing ast](https://github.com/rust-lang/rfcs/issues/2414#issuecomment-457155492).

```rust
#[auto_enum]
fn foo(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => 1..10,
        _ => vec![5, 10].into_iter(),
    }
}

```

(The linked example uses the `Iterator` argument, but [it can be detected automatically by analyzing the return type.](https://github.com/taiki-e/auto_enums/blob/master/tests/type_analysis.rs))

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [September 18, 2020, 5:46pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/26 "2020-09-18T17:46:34Z")

</div>

> [@CAD97](#):
>
> It's always\* sound to have an incorrect implementation of a trait which is not `unsafe` ... modulo traits (ab)using privacy barriers to require a closed set of implementations. So, add another requirement

I followed the same train of thought. And since non`-unsafe` traits that rely on the closed set of implementors do exist, we generally cannot go and implement traits for these things.

> [@CAD97](#):
>
> So, add another requirement: it must be possible to implement the trait at the point where the "auto enum `dyn` " is constructed.

Well, the two "hypothetical" suggestions I made, either the attribute on the trait definition site, or the impl for that `AutoEnum` wrapper type, require both that such things be written in the crate that defines the trait, so we wouldn't have to tackle that issue.

* * *

> [@taiki-e](#):
>
> > [@robinm](#):
> >
> > - anyway an explicit marker seems to be needed
> 
> Even if proc-macro implementations, [can make it work without explicit markers in many cases by analyzing ast](https://github.com/rust-lang/rfcs/issues/2414#issuecomment-457155492).

In that case, the `#[auto_enum]` proc-macro _is the marker_.

* * *

I think we all agree that provided some marker, either built-in or through a (proc-)macro, that auto-generates an _ad-hoc_ instance of (a potentially _ad-hoc_) enum definition, this can "easily" be achieved, provided we target a trait we already know how to make it delegate to each variant.

* * *

But I am still concerned about other traits; we should focus on a way to generalize delegations, otherwise this "feature" will be of limited usability.

- That being said, I like your approach, @taiki-e, of relying on defining custom-derives for a at least some form of extensionability 👌

And besides `unsafe`-ty of the trait, visibility / implementability of the trait, there is also the question of associated items that wouldn't be methods.

For instance, given

```rust
trait IntoConstLenIterator : IntoIterator {
    const LEN: usize;
}
impl<T, const N: usize> Into... for [T; N] { const LEN: usize = N; }

```

Then what do we do with the following?

```rust
fn foo (cond: bool) -> impl IntoConstLenIterator<IntoIter = ...>
{
    enum match cond { true => [42], false => [42, 27] }
}

```

There is no sensible delegation for this trait.

More generally, we should restrict ourselves to traits:

- not having associated functions without a `self` receiver (otherwise we cannot read the `enum` discriminant to dispatch),

- whose non-function associated items (`type`s and `const`s) would have to be fixed, similar to `dyn Trait`.

---

<div class="post-metadata">

**Author:** ![taiki-e](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taiki-e/32/5017_2.png) [@taiki-e](https://internals.rust-lang.org/u/taiki-e)\
**Post date:** [September 19, 2020, 7:01am UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/27 "2020-09-19T07:01:04Z")

</div>

> [@dhm](#):
>
> > [@taiki-e](#):
> >
> > Even if proc-macro implementations, [can make it work without explicit markers in many cases by analyzing ast](https://github.com/rust-lang/rfcs/issues/2414#issuecomment-457155492).
> 
> In that case, the `#[auto_enum]` proc-macro _is the marker_ .

Ah, I thought "explicit makers" mean ["ｍarkers to indicate which branch needs to be wrapped by an enum"](https://github.com/Nemo157/impl_sum/blob/2ba4973f2183cddde6f0636036c60b3b38e98bb1/examples/first.rs#L27). But, if "explicit makers" mean "any annotation to the code", it's certainly a marker.

---

<div class="post-metadata">

**Author:** ![taiki-e](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/taiki-e/32/5017_2.png) [@taiki-e](https://internals.rust-lang.org/u/taiki-e)\
**Post date:** [September 19, 2020, 5:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/28 "2020-09-19T17:26:04Z")

</div>

> [@dhm](#):
>
> But I am still concerned about other traits; we should focus on a way to generalize delegations, otherwise this "feature" will be of limited usability.

It may work well [in combination with the crate does delegation.](https://github.com/taiki-e/auto_enums/issues/110#issuecomment-695332217)

---

<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:** [December 18, 2020, 5:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090/29 "2020-12-18T17:26:09Z")

</div>

Closed on December 18, 2020, 5:26pm UTC

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

[Previous page](https://internals.rust-lang.org/t/pre-rfc-returning-automatically-generating-impl-trait/13090.md?page=1)
