Skip to content

Commit c26f887

Browse files
committed
Handle variants
1 parent a4d3692 commit c26f887

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,8 @@ impl AttributesExt for Attributes {
848848
/// they exist in both namespaces (structs and modules)
849849
fn value_ns_kind(def: Def, path_str: &str) -> Option<(&'static str, String)> {
850850
match def {
851-
// structs and mods exist in both namespaces. skip them
852-
Def::StructCtor(..) | Def::Mod(..) => None,
853-
Def::Variant(..) | Def::VariantCtor(..)
854-
=> Some(("variant", format!("{}()", path_str))),
851+
// structs, variants, and mods exist in both namespaces. skip them
852+
Def::StructCtor(..) | Def::Mod(..) | Def::Variant(..) | Def::VariantCtor(..) => None,
855853
Def::Fn(..)
856854
=> Some(("function", format!("{}()", path_str))),
857855
Def::Method(..)
@@ -897,6 +895,20 @@ fn ambiguity_error(cx: &DocContext, attrs: &Attributes,
897895
.emit();
898896
}
899897

898+
/// Given an enum variant's def, return the def of its enum and the associated fragment
899+
fn handle_variant(cx: &DocContext, def: Def) -> Result<(Def, Option<String>), ()> {
900+
use rustc::ty::DefIdTree;
901+
902+
let parent = if let Some(parent) = cx.tcx.parent(def.def_id()) {
903+
parent
904+
} else {
905+
return Err(())
906+
};
907+
let parent_def = Def::Enum(parent);
908+
let variant = cx.tcx.expect_variant_def(def);
909+
Ok((parent_def, Some(format!("{}.v", variant.name))))
910+
}
911+
900912
/// Resolve a given string as a path, along with whether or not it is
901913
/// in the value namespace. Also returns an optional URL fragment in the case
902914
/// of variants and methods
@@ -917,6 +929,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
917929
let value = match result.def {
918930
Def::Method(_) | Def::AssociatedConst(_) => true,
919931
Def::AssociatedTy(_) => false,
932+
Def::Variant(_) => return handle_variant(cx, result.def),
920933
// not a trait item, just return what we found
921934
_ => return Ok((result.def, None))
922935
};

0 commit comments

Comments
 (0)