Skip to content

Commit e9ed626

Browse files
Add support for variant fields in rustdoc::html::format::print_anchor
1 parent 3d461af commit e9ed626

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/librustdoc/html/format.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -840,16 +840,25 @@ pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl D
840840
if let Ok(HrefInfo { url, kind, rust_path }) = href(did, cx) {
841841
let tcx = cx.tcx();
842842
let def_kind = tcx.def_kind(did);
843-
let anchor = if matches!(
844-
def_kind,
845-
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant
846-
) {
847-
let parent_def_id = tcx.parent(did);
848-
let item_type =
849-
ItemType::from_def_kind(def_kind, Some(tcx.def_kind(parent_def_id)));
850-
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
851-
} else {
852-
String::new()
843+
let anchor = match def_kind {
844+
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
845+
let parent_def_id = tcx.parent(did);
846+
let item_type =
847+
ItemType::from_def_kind(def_kind, Some(tcx.def_kind(parent_def_id)));
848+
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
849+
}
850+
DefKind::Field => {
851+
let s;
852+
let parent_id = tcx.parent(did);
853+
let kind = if tcx.def_kind(parent_id) == DefKind::Variant {
854+
s = format!("variant.{}.field", tcx.item_name(parent_id).as_str());
855+
&s
856+
} else {
857+
"structfield"
858+
};
859+
format!("#{kind}.{}", tcx.item_name(did))
860+
}
861+
_ => String::new(),
853862
};
854863

855864
write!(

0 commit comments

Comments
 (0)