# PSA: Debugging rustc type layouts

**URL:** <https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082>\
**Category:** Uncategorized\
**Created:** [April 4, 2020, 5:17pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082 "2020-04-04T17:17:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [April 4, 2020, 5:17pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/1 "2020-04-04T17:17:56Z")

</div>

This post is a “public service announcement” for people working on the guts of rustc. I wish I had known about this a year ago, so I hope this post can make this feature more widely known.

Debugging layouts recently became a lot easier:

```rust
#![feature(rustc_attrs)]

#[rustc_layout(debug)]
type T = (u8, u16);

```

[https://www.ralfj.de/blog/2020/04/04/layout-debugging.html](https://www.ralfj.de/blog/2020/04/04/layout-debugging.html)

---

<div class="post-metadata">

**Author:** ![jschievink](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jschievink/32/3063_2.png) [@jschievink](https://internals.rust-lang.org/u/jschievink)\
**Post date:** [April 4, 2020, 9:00pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/2 "2020-04-04T21:00:10Z")

</div>

Nice to see this!

Unfortunately it doesn't seem to work with `impl Trait` in the type alias, so I don't think it helps with debugging generator layout.

```rust
#![feature(rustc_attrs, generators, generator_trait, type_alias_impl_trait)]

use std::ops::Generator;

#[rustc_layout(debug)]
type T = impl Generator<u8>;

fn f() -> T {
    |mut r: u8| {
        r = yield "hi";
        123
    }
}

```

```rust
warning: unused attribute
 --> src/lib.rs:5:1
  |
5 | #[rustc_layout(debug)]
  | ^^^^^^^^^^^^^^^^^^^^^^

```

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [April 5, 2020, 7:19am UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/3 "2020-04-05T07:19:14Z")

</div>

> [@jschievink](#):
>
> Unfortunately it doesn't seem to work with `impl Trait` in the type alias, so I don't think it helps with debugging generator layout.

Interesting. My first suspicion would be that [this call fails](https://github.com/rust-lang/rust/blob/e129923b7e4d58a92c8a0530cd6eaa5be548342d/src/librustc_passes/layout_test.rs#L50), i.e., that the type does not yet have a layout when the attribute gets processed. @eddyb might be able to confirm or reject that hypothesis.

---

<div class="post-metadata">

**Author:** ![jschievink](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jschievink/32/3063_2.png) [@jschievink](https://internals.rust-lang.org/u/jschievink)\
**Post date:** [April 5, 2020, 11:19am UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/4 "2020-04-05T11:19:43Z")

</div>

Looks like [this match](https://github.com/rust-lang/rust/blob/e129923b7e4d58a92c8a0530cd6eaa5be548342d/src/librustc_passes/layout_test.rs#L26-L38) needs to handle `OpaqueTy`

---

<div class="post-metadata">

**Author:** ![eddyb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eddyb/32/8411_2.png) [@eddyb](https://internals.rust-lang.org/u/eddyb)\
**Post date:** [April 5, 2020, 11:27am UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/5 "2020-04-05T11:27:02Z")

</div>

> [@RalfJung](#):
>
> i.e., that the type does not yet have a layout when the attribute gets processed

That's not really a thing nowadays, you can't observe something like that without it being unsound for the query system and incremental - what would happen is that the necessary queries would be triggered and the type computed on-demand (this is similar to CTFE triggering type-checking, borrow-checking and only then running the MIR through miri).

It works with this tweak, which means @jschievink is right:

```rust
#[rustc_layout(debug)]
type X = T;
type T = impl Generator<u8>;

```

This is somewhat surprising to me, as I expected `impl Trait` to be allowed _anywhere_ inside the RHS of a `type` alias, but apparently the feature is limited to `type Foo = impl Bar;`.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [April 5, 2020, 11:32am UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/6 "2020-04-05T11:32:06Z")

</div>

So `type T = impl X` is not a `TyAlias`? It syntactically looks like one.^^ Interesting.

EDIT: I am working on a PR for this, but LLVM takes 1h to rebuild, so it'll be a bit until I can push.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [April 5, 2020, 1:02pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/7 "2020-04-05T13:02:36Z")

</div>

It's the new syntax for existential types.

This used to be spelled as

```rust
existential type Foo: Trait;

```

But was changed to

```rust
type Foo = impl Trait;

```

To leverage the existing `impl Trait` syntax instead of a completely new syntax.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [April 5, 2020, 2:22pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/8 "2020-04-05T14:22:21Z")

</div>

> [@RustyYato](#):
>
> It's the new syntax for existential types.

I find it somewhat confusing to get entirely different AST nodes for very similar surface syntax -- naively I would have expected that `TyAlias` then has two kind of "child nodes", one for normal aliases and one for `impl`. But well, I probably should just have read the docs. 😉

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [April 5, 2020, 3:14pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/9 "2020-04-05T15:14:57Z")

</div>

The PR is up:

> <https://github.com/rust-lang/rust/pull/70815>

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [April 5, 2020, 9:56pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/10 "2020-04-05T21:56:20Z")

</div>

> [@eddyb](#):
>
> This is somewhat surprising to me, as I expected `impl Trait` to be allowed _anywhere_ inside the RHS of a `type` alias, but apparently the feature is limited to `type Foo = impl Bar;` .

It should be but it's not fully implemented yet (see [https://github.com/rust-lang/rust/pull/66197](https://github.com/rust-lang/rust/pull/66197), we still have those hacks in lowering, sadly).

> [@RalfJung](#):
>
> So `type T = impl X` is not a `TyAlias` ? It syntactically looks like one.^^ Interesting.

It is in the AST (see PR above) but not in HIR.

> [@RalfJung](#):
>
> I find it somewhat confusing to get entirely different AST nodes for very similar surface syntax -- naively I would have expected that `TyAlias` then has two kind of "child nodes", one for normal aliases and one for `impl` . But well, I probably should just have read the docs. 😉

Yeah that's how it should work, but... TODO 😛

---

<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 4, 2020, 9:56pm UTC](https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082/11 "2020-07-04T21:56:31Z")

</div>

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