# Infinite types as unsized?

**URL:** <https://internals.rust-lang.org/t/infinite-types-as-unsized/14594>\
**Category:** Uncategorized\
**Created:** [April 29, 2021, 2:37am UTC](https://internals.rust-lang.org/t/infinite-types-as-unsized/14594 "2021-04-29T02:37:48Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![pcpthm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pcpthm/32/4952_2.png) [@pcpthm](https://internals.rust-lang.org/u/pcpthm)\
**Post date:** [April 30, 2021, 2:15am UTC](https://internals.rust-lang.org/t/infinite-types-as-unsized/14594/13 "2021-04-30T02:15:01Z")

</div>

As others pointed out, currently, an enum type always has a fixed size, and if a fixed size is assumed the recursive enum type cannot exist.

To allow such a recursive enum type, two features are necessary.

### Allow dynamically sized enum type

Like dynamically sized structs, an enum type can automatically make dynamically sized if one of the variants has a dynamically sized field at the end.

I found an old RFC issue [https://github.com/rust-lang/rfcs/issues/1151](https://github.com/rust-lang/rfcs/issues/1151). There are some subtleties regarding where to put enum tag, as the tag is necessary as the metadata.

### Automatically make otherwise infinitely sized enum types to dynamically sized

If DST enum is implemented, it is possible to make recursive enum "just work" by making it dynamically sized automatically.

However, I think automatic dynamically sizing is problematic when multiple types are mutually recursive, as there are multiple ways to "solve" the infinite-ness. Consider these mutually recursive enum types:

```rust
enum E1 {
    X,
    Y(E2),
}

enum E2 {
    X,
    Y(E1),
}

```

Currently, there are two minimal ways to make it work by boxing: either modifying `E1` to have `X(Box<E2>)`, or modifying `E2` to have `Y(Box<E1>)`. Respectively, either `E1` or `E2`, or both can be made dynamically sized, but the compiler has to determine which way is preferred. I _guess_ there are more issues when generics are involved.

Thus, I think, requiring an explicit keyword or something is a sensible way to support the recursive dynamically sized types.

---

_[View the full topic](https://internals.rust-lang.org/t/infinite-types-as-unsized/14594)._
