Skip to content

Commit a4d3692

Browse files
committed
Make it work for traits
1 parent 5a89f40 commit a4d3692

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,29 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
911911
&path_str, is_val)
912912
});
913913

914-
if result.is_ok() {
915-
return result.map(|path| (path.def, None));
914+
if let Ok(result) = result {
915+
// In case this is a trait item, skip the
916+
// early return and try looking for the trait
917+
let value = match result.def {
918+
Def::Method(_) | Def::AssociatedConst(_) => true,
919+
Def::AssociatedTy(_) => false,
920+
// not a trait item, just return what we found
921+
_ => return Ok((result.def, None))
922+
};
923+
924+
if value != is_val {
925+
return Err(())
926+
}
927+
} else {
928+
// If resolution failed, it may still be a method
929+
// because methods are not handled by the resolver
930+
// If so, bail when we're not looking for a value
931+
if !is_val {
932+
return Err(())
933+
}
916934
}
917935

918-
// Try looking for methods and other associated items
936+
// Try looking for methods and associated items
919937
let mut split = path_str.rsplitn(2, "::");
920938
let mut item_name = if let Some(first) = split.next() {
921939
first
@@ -935,8 +953,6 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
935953
resolver.resolve_str_path_error(DUMMY_SP,
936954
&path, false)
937955
})?;
938-
939-
940956
match ty.def {
941957
Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
942958
let item = cx.tcx.inherent_impls(did).iter()
@@ -952,9 +968,22 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
952968
Err(())
953969
}
954970
}
955-
Def::Trait(_) => {
956-
// XXXManishearth todo
957-
Err(())
971+
Def::Trait(did) => {
972+
let item = cx.tcx.associated_item_def_ids(did).iter()
973+
.map(|item| cx.tcx.associated_item(*item))
974+
.find(|item| item.name == item_name);
975+
if let Some(item) = item {
976+
let kind = match item.kind {
977+
ty::AssociatedKind::Const if is_val => "associatedconstant",
978+
ty::AssociatedKind::Type if !is_val => "associatedtype",
979+
ty::AssociatedKind::Method if is_val => "tymethod",
980+
_ => return Err(())
981+
};
982+
983+
Ok((ty.def, Some(format!("{}.{}", kind, item_name))))
984+
} else {
985+
Err(())
986+
}
958987
}
959988
_ => Err(())
960989
}

0 commit comments

Comments
 (0)