diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 1e3cd2668506f..a699d0b38a70c 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -32,6 +32,7 @@ mod context; mod print_item; mod sidebar; mod span_map; +mod type_layout; mod write_shared; pub(crate) use self::context::*; diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 6bce57340040b..b71109f15609e 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -6,16 +6,14 @@ use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; use rustc_middle::middle::stability; -use rustc_middle::span_bug; -use rustc_middle::ty::layout::LayoutError; -use rustc_middle::ty::{self, Adt, TyCtxt}; +use rustc_middle::ty::{self, TyCtxt}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; -use rustc_target::abi::{LayoutS, Primitive, TagEncoding, Variants}; use std::cmp::Ordering; use std::fmt; use std::rc::Rc; +use super::type_layout::document_type_layout; use super::{ collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference, item_ty_to_section, notable_traits_button, notable_traits_json, render_all_impls, @@ -1932,118 +1930,6 @@ fn document_non_exhaustive<'a>(item: &'a clean::Item) -> impl fmt::Display + 'a }) } -fn document_type_layout<'a, 'cx: 'a>( - cx: &'a Context<'cx>, - ty_def_id: DefId, -) -> impl fmt::Display + 'a + Captures<'cx> { - fn write_size_of_layout(mut w: impl fmt::Write, layout: &LayoutS, tag_size: u64) { - if layout.abi.is_unsized() { - write!(w, "(unsized)").unwrap(); - } else { - let size = layout.size.bytes() - tag_size; - write!(w, "{size} byte{pl}", pl = if size == 1 { "" } else { "s" }).unwrap(); - if layout.abi.is_uninhabited() { - write!( - w, - " (uninhabited)" - ).unwrap(); - } - } - } - - display_fn(move |mut f| { - if !cx.shared.show_type_layout { - return Ok(()); - } - - writeln!( - f, - "
Note: Most layout information is \
- completely unstable and may even differ between compilations. \
- The only exception is types with certain repr(...)
attributes. \
- Please see the Rust Reference’s \
- “Type Layout” \
- chapter for details on type layout guarantees.
Size: ")?; - write_size_of_layout(&mut f, &ty_layout.layout.0, 0); - writeln!(f, "
")?; - if let Variants::Multiple { variants, tag, tag_encoding, .. } = - &ty_layout.layout.variants() - { - if !variants.is_empty() { - f.write_str( - "Size for each variant:
\ -{name}
: ")?;
- write_size_of_layout(&mut f, layout, tag_size);
- writeln!(&mut f, "Note: Unable to compute type layout, \ - possibly due to this type having generic parameters. \ - Layout can only be computed for concrete, fully-instantiated types.
" - )?; - } - // This kind of error probably can't happen with valid code, but we don't - // want to panic and prevent the docs from building, so we just let the - // user know that we couldn't compute the layout. - Err(LayoutError::SizeOverflow(_)) => { - writeln!( - f, - "Note: Encountered an error during type layout; \ - the type was too big.
" - )?; - } - Err(LayoutError::NormalizationFailure(_, _)) => { - writeln!( - f, - "Note: Encountered an error during type layout; \ - the type failed to be normalized.
" - )?; - } - } - - writeln!(f, " {# #}
+ Note: Most layout information is completely {#+ #}
+ unstable and may even differ between compilations. {#+ #}
+ The only exception is types with certain repr(...)
{#+ #}
+ attributes. Please see the Rust Reference’s {#+ #}
+ “Type Layout” {#+ #}
+ chapter for details on type layout guarantees. {# #}
+
Size: {{ type_layout_size|safe }}
{# #} + {% if !variants.is_empty() %} +{# #} + Size for each variant: {# #} +
{# #} +{{ name }}
: {#+ #}
+ {{ layout_size|safe }}
+ {# #} + Note: Unable to compute type layout, {#+ #} + possibly due to this type having generic parameters. {#+ #} + Layout can only be computed for concrete, fully-instantiated types. {# #} +
{# #} + {# This kind of error probably can't happen with valid code, but we don't + want to panic and prevent the docs from building, so we just let the + user know that we couldn't compute the layout. #} + {% when Err(LayoutError::SizeOverflow(_)) %} +{# #} + Note: Encountered an error during type layout; {#+ #} + the type was too big. {# #} +
{# #} + {% when Err(LayoutError::NormalizationFailure(_, _)) %} +{# #} + Note: Encountered an error during type layout; {#+ #} + the type failed to be normalized. {# #} +
{# #} + {% endmatch %} +