Skip to content

Commit de70a77

Browse files
Correctly handle cross-crate C-like variants
1 parent 1994d0b commit de70a77

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/librustdoc/html/render/print_item.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12611261
variants_count,
12621262
has_stripped_entries,
12631263
*is_non_exhaustive,
1264+
it.def_id().unwrap(),
12641265
)
12651266
});
12661267
item_variants(w, cx, it, &variants);
@@ -1425,6 +1426,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
14251426
count_variants,
14261427
e.has_stripped_entries(),
14271428
it.is_non_exhaustive(),
1429+
it.def_id().unwrap(),
14281430
);
14291431
});
14301432

@@ -1438,21 +1440,6 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
14381440
write!(w, "{}", document_type_layout(cx, def_id));
14391441
}
14401442

1441-
fn get_parent_enum_def_id(
1442-
cx: &mut Context<'_>,
1443-
variant_def_id: rustc_hir::def_id::LocalDefId,
1444-
) -> DefId {
1445-
use rustc_hir::{ItemKind, Node};
1446-
let variant_hir_id = cx.tcx().hir().local_def_id_to_hir_id(variant_def_id);
1447-
1448-
for (_, node) in cx.tcx().hir().parent_iter(variant_hir_id) {
1449-
if let Node::Item(item) = node && matches!(item.kind, ItemKind::Enum(..)) {
1450-
return item.owner_id.to_def_id();
1451-
}
1452-
}
1453-
panic!("No parent enum found for variant {variant_def_id:?}");
1454-
}
1455-
14561443
/// It'll return true if all variants are C-like variants and if at least one of them has a value
14571444
/// set.
14581445
fn should_show_c_like_variants_value(
@@ -1478,15 +1465,12 @@ fn display_c_like_variant(
14781465
variant: &clean::Variant,
14791466
index: rustc_target::abi::VariantIdx,
14801467
should_show_c_like_variants_value: bool,
1468+
enum_def_id: DefId,
14811469
) {
14821470
let name = item.name.unwrap();
14831471
if let Some(ref value) = variant.discriminant {
14841472
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
1485-
} else if should_show_c_like_variants_value &&
1486-
let Some(variant_def_id) = item.item_id.as_def_id() &&
1487-
let Some(variant_def_id) = variant_def_id.as_local()
1488-
{
1489-
let enum_def_id = get_parent_enum_def_id(cx, variant_def_id);
1473+
} else if should_show_c_like_variants_value {
14901474
let adt_def = cx.tcx().adt_def(enum_def_id);
14911475
let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
14921476
if discr.ty.is_signed() {
@@ -1507,6 +1491,7 @@ fn render_enum_fields(
15071491
count_variants: usize,
15081492
has_stripped_entries: bool,
15091493
is_non_exhaustive: bool,
1494+
enum_def_id: DefId,
15101495
) {
15111496
let should_show_c_like_variants_value = should_show_c_like_variants_value(variants);
15121497
if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) {
@@ -1538,6 +1523,7 @@ fn render_enum_fields(
15381523
var,
15391524
index,
15401525
should_show_c_like_variants_value,
1526+
enum_def_id,
15411527
),
15421528
clean::VariantKind::Tuple(ref s) => {
15431529
write!(w, "{}({})", v.name.unwrap(), print_tuple_struct_fields(cx, s));
@@ -1608,6 +1594,7 @@ fn item_variants(
16081594
var,
16091595
index,
16101596
should_show_c_like_variants_value,
1597+
it.def_id().unwrap(),
16111598
);
16121599
} else {
16131600
w.write_str(variant.name.unwrap().as_str());

0 commit comments

Comments
 (0)