Skip to main content

rustc_dump_layout

Attribute rustc_dump_layout 

Source
Expand description

Dumps the layout of the annotated item.

This ends up calling the layout_of query to get the Layout of the annotated item. If used with the debug modifier, it will print the entirety of Layout. Other modifiers will print only parts of it.

See AttributeKind::RustcDumpLayout for the internal representation of this attribute.

§Example: debug

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no
//@ normalize-stderr: "randomization_seed: \d+" -> "randomization_seed: $$SEED"

#![feature(rustc_attrs)]

#[rustc_dump_layout(debug)]
pub union Union {
    Float: f32,
    Int: u32,
}

produces:

error: layout_of(Union) = Layout {
          size: Size(4 bytes),
          align: AbiAlign {
              abi: Align(4 bytes),
          },
          backend_repr: Memory {
              sized: true,
          },
          fields: Union(
              2,
          ),
          largest_niche: None,
          uninhabited: false,
          variants: Single {
              index: 0,
          },
          max_repr_align: None,
          unadjusted_abi_align: Align(4 bytes),
          randomization_seed: $SEED,
      }
--> $DIR/rustc_dump_layout_debug.rs:8:1
 |
8 | pub union Union {
 | ^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

§Example: largest_niche

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no

#![feature(rustc_attrs)]

#[rustc_dump_layout(largest_niche)]
type Alias = Option<char>;

produces:

error: largest_niche: Some(Niche { offset: Size(0 bytes), value: u32, valid_range: (..=1114111) | (4294967295..) })
--> $DIR/rustc_dump_layout_largest_niche.rs:7:1
 |
7 | type Alias = Option<char>;
 | ^^^^^^^^^^

error: aborting due to 1 previous error

§Example: size

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no

#![feature(rustc_attrs)]

#[rustc_dump_layout(size)]
type ID = std::any::TypeId;

produces:

error: size: Size(16 bytes)
--> $DIR/rustc_dump_layout_size.rs:7:1
 |
7 | type ID = std::any::TypeId;
 | ^^^^^^^

error: aborting due to 1 previous error

§Example: align

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no

#![feature(rustc_attrs)]

#[rustc_dump_layout(align)]
enum Enum {
    Bytes([u8; 4]),
    Int(u32),
}

produces:

error: align: Align(4 bytes)
--> $DIR/rustc_dump_layout_align.rs:7:1
 |
7 | enum Enum {
 | ^^^^^^^^^

error: aborting due to 1 previous error

§Example: backend_repr

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no

#![feature(rustc_attrs)]
#![feature(portable_simd)]

#[rustc_dump_layout(backend_repr)]
type Simd = std::simd::u32x4;

produces:

error: backend_repr: SimdVector { element: u32 is .., count: BackendLaneCount(4) }
--> $DIR/rustc_dump_layout_backend_repr.rs:8:1
 |
8 | type Simd = std::simd::u32x4;
 | ^^^^^^^^^

error: aborting due to 1 previous error

§Example: homogeneous_aggregate

//@ dont-require-annotations: ERROR
//@ compile-flags: --crate-type lib -Z ui-testing=no

#![feature(rustc_attrs)]

#[rustc_dump_layout(homogeneous_aggregate)]
#[repr(C)]
struct Struct {
    field: [u8; 32],
    unit: (),
}

produces:

error: homogeneous_aggregate: Ok(Homogeneous(Reg { kind: Integer, size: Size(1 bytes) }))
--> $DIR/rustc_dump_layout_homogeneous_aggregate.rs:8:1
 |
8 | struct Struct {
 | ^^^^^^^^^^^^^

error: aborting due to 1 previous error