From c0451f73b2094d7edccb77d7ea8be48f64887e67 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 6 Sep 2021 07:09:32 +0000 Subject: [PATCH] Correctly handle niche of enum --- src/librustdoc/html/render/print_item.rs | 12 ++++++++---- src/test/rustdoc/type-layout.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 6054073bf79..aac631a1cfc 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -13,7 +13,7 @@ use rustc_middle::ty::layout::LayoutError; use rustc_middle::ty::{Adt, TyCtxt}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; -use rustc_target::abi::{Layout, Primitive, Variants}; +use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants}; use super::{ collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl, @@ -1639,7 +1639,9 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) { w.write_str("

Size: "); write_size_of_layout(w, ty_layout.layout, 0); writeln!(w, "

"); - if let Variants::Multiple { variants, tag, .. } = &ty_layout.layout.variants { + if let Variants::Multiple { variants, tag, tag_encoding, .. } = + &ty_layout.layout.variants + { if !variants.is_empty() { w.write_str( "

Size for each variant:

\ @@ -1652,10 +1654,12 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) { span_bug!(tcx.def_span(ty_def_id), "not an adt") }; - let tag_size = if let Primitive::Int(i, _) = tag.value { + let tag_size = if let TagEncoding::Niche { .. } = tag_encoding { + 0 + } else if let Primitive::Int(i, _) = tag.value { i.size().bytes() } else { - span_bug!(tcx.def_span(ty_def_id), "tag is not int") + span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int") }; for (index, layout) in variants.iter_enumerated() { diff --git a/src/test/rustdoc/type-layout.rs b/src/test/rustdoc/type-layout.rs index 8c4f4f8fc98..0868486fa59 100644 --- a/src/test/rustdoc/type-layout.rs +++ b/src/test/rustdoc/type-layout.rs @@ -61,3 +61,12 @@ pub enum Variants { A, B(u8), } + +// @has type_layout/enum.WithNiche.html 'Size: ' +// @has - //p '4 bytes' +// @has - 'None: 0 bytes' +// @has - 'Some: 4 bytes' +pub enum WithNiche { + None, + Some(std::num::NonZeroU32), +}