X-Git-Url: https://git.ralfj.de/web.git/blobdiff_plain/14fe6fd0bdbd3f006cc037da23d11bf734756c05..c00fca4e0107673ce66185625dadbb8c3c48ed09:/personal/_posts/2020-04-04-layout-debugging.md?ds=inline diff --git a/personal/_posts/2020-04-04-layout-debugging.md b/personal/_posts/2020-04-04-layout-debugging.md index 627a45f..66000e0 100644 --- a/personal/_posts/2020-04-04-layout-debugging.md +++ b/personal/_posts/2020-04-04-layout-debugging.md @@ -1,10 +1,11 @@ --- title: "Debugging rustc type layouts" categories: rust +forum: https://internals.rust-lang.org/t/psa-debugging-rustc-type-layouts/12082 --- This post is a "public service announcement" for people working on the guts of rustc. -I wish I had know about this a year ago, so I hope this post can make this feature more widely known. +I wish I had known about this a year ago, so I hope this post can make this feature more widely known. @@ -28,7 +29,7 @@ The (permanently) unstable `rustc_layout` attribute [can now be used](https://gi In this case, it prints: ``` -error: layout debugging: Layout { +error: layout_of((u8, u16)) = Layout { fields: Arbitrary { offsets: [ Size { @@ -82,6 +83,8 @@ the fields are at offsets 0 and 2, the type has alignment 2 (but preferred align We can also see that it uses the `ScalarPair` abi which is relevant for Miri and when passing data as arguments to another function. To learn more about what all this information means, see [the `Layout` type docs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/struct.Layout.html). +**Update:** After a suggestions by @jschievink, this can now also be used to print the [underlying type and layout](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1de2bed0c0d0f9171bfb41969f5028fb) of named opaque types, which is particularly useful [for generators](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=de99ab78a4d77bceee6760021b19de7d). **/Update** + So the next time you work with `Layout` and wonder how exactly the niche gets represented, or whether an `enum` can have `ScalarPair` abi (hint: yes it can), you can easily look at a few examples to see how rustc thinks about this type internally. This is basically the type-level equivalent of `--emit mir`. I have wanted this since forever, so much that some time ago I wrote an awful hack for this based on rustc debug tracing.