about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamelid <camelidcamel@gmail.com>2021-04-12 19:42:26 -0700
committerCamelid <camelidcamel@gmail.com>2021-05-11 09:55:31 -0700
commit12ee920a7c3aa237eb7e207e128120f5a639743a (patch)
tree86261e367548e095d74a6f573844480bfa5101bf
parent48da66f28f078c0dc31068a30977e9079c876d90 (diff)
downloadrust-12ee920a7c3aa237eb7e207e128120f5a639743a.tar.gz
rust-12ee920a7c3aa237eb7e207e128120f5a639743a.zip
Only show type layout info if `--show-type-layout` is passed
-rw-r--r--src/librustdoc/config.rs4
-rw-r--r--src/librustdoc/html/render/context.rs4
-rw-r--r--src/librustdoc/html/render/print_item.rs4
-rw-r--r--src/librustdoc/lib.rs3
-rw-r--r--src/test/rustdoc/type-layout-flag-required.rs4
-rw-r--r--src/test/rustdoc/type-layout.rs2
6 files changed, 21 insertions, 0 deletions
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 045b42d0dca..b75e98ae16c 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -267,6 +267,8 @@ crate struct RenderOptions {
     crate document_hidden: bool,
     /// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
     crate generate_redirect_map: bool,
+    /// Show the memory layout of types in the docs.
+    crate show_type_layout: bool,
     crate unstable_features: rustc_feature::UnstableFeatures,
     crate emit: Vec<EmitType>,
 }
@@ -636,6 +638,7 @@ impl Options {
         let document_hidden = matches.opt_present("document-hidden-items");
         let run_check = matches.opt_present("check");
         let generate_redirect_map = matches.opt_present("generate-redirect-map");
+        let show_type_layout = matches.opt_present("show-type-layout");
 
         let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
 
@@ -695,6 +698,7 @@ impl Options {
                 document_private,
                 document_hidden,
                 generate_redirect_map,
+                show_type_layout,
                 unstable_features: rustc_feature::UnstableFeatures::from_environment(
                     crate_name.as_deref(),
                 ),
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs
index 4e17dc8d3a7..666d9dfc3e9 100644
--- a/src/librustdoc/html/render/context.rs
+++ b/src/librustdoc/html/render/context.rs
@@ -91,6 +91,8 @@ crate struct SharedContext<'tcx> {
     crate include_sources: bool,
     /// The local file sources we've emitted and their respective url-paths.
     crate local_sources: FxHashMap<PathBuf, String>,
+    /// Show the memory layout of types in the docs.
+    pub(super) show_type_layout: bool,
     /// Whether the collapsed pass ran
     collapsed: bool,
     /// The base-URL of the issue tracker for when an item has been tagged with
@@ -373,6 +375,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
             generate_search_filter,
             unstable_features,
             generate_redirect_map,
+            show_type_layout,
             ..
         } = options;
 
@@ -446,6 +449,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
             all: RefCell::new(AllTypes::new()),
             errors: receiver,
             redirections: if generate_redirect_map { Some(Default::default()) } else { None },
+            show_type_layout,
         };
 
         // Add the default themes to the `Vec` of stylepaths
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 9ac9ee66f6a..2de1a2226f5 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -1536,6 +1536,10 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
 }
 
 fn document_ty_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
+    if !cx.shared.show_type_layout {
+        return;
+    }
+
     let param_env = cx.tcx().param_env(ty_def_id);
     let ty = cx.tcx().type_of(ty_def_id);
     match cx.tcx().layout_of(param_env.and(ty)) {
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 169ef015fa8..5ede3780e87 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -594,6 +594,9 @@ fn opts() -> Vec<RustcOptGroup> {
             )
         }),
         unstable("no-run", |o| o.optflag("", "no-run", "Compile doctests without running them")),
+        unstable("show-type-layout", |o| {
+            o.optflag("", "show-type-layout", "Include the memory layout of types in the docs")
+        }),
     ]
 }
 
diff --git a/src/test/rustdoc/type-layout-flag-required.rs b/src/test/rustdoc/type-layout-flag-required.rs
new file mode 100644
index 00000000000..a01fbd22950
--- /dev/null
+++ b/src/test/rustdoc/type-layout-flag-required.rs
@@ -0,0 +1,4 @@
+// Tests that `--show-type-layout` is required in order to show layout info.
+
+// @!has type_layout_flag_required/struct.Foo.html 'Size: '
+pub struct Foo(usize);
diff --git a/src/test/rustdoc/type-layout.rs b/src/test/rustdoc/type-layout.rs
index e18bdeba913..9fd8f92aeb9 100644
--- a/src/test/rustdoc/type-layout.rs
+++ b/src/test/rustdoc/type-layout.rs
@@ -1,3 +1,5 @@
+// compile-flags: --show-type-layout -Z unstable-options
+
 // @has type_layout/struct.Foo.html 'Size: '
 // @has - ' bytes'
 pub struct Foo {