From 655c2af6fc60786d890ac7ff911cad467408fa5a Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Wed, 27 May 2020 10:31:58 +0200 Subject: [PATCH] Add section for debugging rustc type layouts --- src/compiler-debugging.md | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/compiler-debugging.md b/src/compiler-debugging.md index 780f5d871..a499224ae 100644 --- a/src/compiler-debugging.md +++ b/src/compiler-debugging.md @@ -274,3 +274,62 @@ try`. This is helpful when you want to examine the resulting build of a PR without doing the build yourself. [rtim]: https://github.com/kennytm/rustup-toolchain-install-master + +## Debugging type layouts + +The (permanently) unstable `#[rustc_layout]` attribute can be used to dump +the [`Layout`] of the type it is attached to. For example: + +```rust +#![feature(rustc_attrs)] + +#[rustc_layout(debug)] +type T<'a> = &'a u32; +``` + +Will emit the following: + +```text +error: layout_of(&'a u32) = Layout { + fields: Primitive, + variants: Single { + index: 0, + }, + abi: Scalar( + Scalar { + value: Pointer, + valid_range: 1..=18446744073709551615, + }, + ), + largest_niche: Some( + Niche { + offset: Size { + raw: 0, + }, + scalar: Scalar { + value: Pointer, + valid_range: 1..=18446744073709551615, + }, + }, + ), + align: AbiAndPrefAlign { + abi: Align { + pow2: 3, + }, + pref: Align { + pow2: 3, + }, + }, + size: Size { + raw: 8, + }, +} + --> src/lib.rs:4:1 + | +4 | type T<'a> = &'a u32; + | ^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error +``` + +[`Layout`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/abi/struct.Layout.html