Skip to content

Commit c0451f7

Browse files
committed
Correctly handle niche of enum
1 parent 5f1505e commit c0451f7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/librustdoc/html/render/print_item.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::layout::LayoutError;
1313
use rustc_middle::ty::{Adt, TyCtxt};
1414
use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
16-
use rustc_target::abi::{Layout, Primitive, Variants};
16+
use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
1717

1818
use super::{
1919
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) {
16391639
w.write_str("<p><strong>Size:</strong> ");
16401640
write_size_of_layout(w, ty_layout.layout, 0);
16411641
writeln!(w, "</p>");
1642-
if let Variants::Multiple { variants, tag, .. } = &ty_layout.layout.variants {
1642+
if let Variants::Multiple { variants, tag, tag_encoding, .. } =
1643+
&ty_layout.layout.variants
1644+
{
16431645
if !variants.is_empty() {
16441646
w.write_str(
16451647
"<p><strong>Size for each variant:</strong></p>\
@@ -1652,10 +1654,12 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16521654
span_bug!(tcx.def_span(ty_def_id), "not an adt")
16531655
};
16541656

1655-
let tag_size = if let Primitive::Int(i, _) = tag.value {
1657+
let tag_size = if let TagEncoding::Niche { .. } = tag_encoding {
1658+
0
1659+
} else if let Primitive::Int(i, _) = tag.value {
16561660
i.size().bytes()
16571661
} else {
1658-
span_bug!(tcx.def_span(ty_def_id), "tag is not int")
1662+
span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int")
16591663
};
16601664

16611665
for (index, layout) in variants.iter_enumerated() {

src/test/rustdoc/type-layout.rs

+9
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,12 @@ pub enum Variants {
6161
A,
6262
B(u8),
6363
}
64+
65+
// @has type_layout/enum.WithNiche.html 'Size: '
66+
// @has - //p '4 bytes'
67+
// @has - '<code>None</code>: 0 bytes'
68+
// @has - '<code>Some</code>: 4 bytes'
69+
pub enum WithNiche {
70+
None,
71+
Some(std::num::NonZeroU32),
72+
}

0 commit comments

Comments
 (0)