Add support for variant and types fields for intra links

This commit is contained in:
Guillaume Gomez 2018-03-30 14:20:16 +02:00
parent 696076144d
commit d0eeb291dd
2 changed files with 55 additions and 3 deletions

View file

@ -1013,8 +1013,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
let ty = cx.resolver.borrow_mut()
.with_scope(*id,
|resolver| {
resolver.resolve_str_path_error(DUMMY_SP,
&path, false)
resolver.resolve_str_path_error(DUMMY_SP, &path, false)
})?;
match ty.def {
Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
@ -1029,7 +1028,27 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
};
Ok((ty.def, Some(format!("{}.{}", out, item_name))))
} else {
Err(())
let is_enum = match ty.def {
Def::Enum(_) => true,
_ => false,
};
let elem = if is_enum {
cx.tcx.adt_def(did).all_fields().find(|item| item.name == item_name)
} else {
cx.tcx.adt_def(did)
.non_enum_variant()
.fields
.iter()
.find(|item| item.name == item_name)
};
if let Some(item) = elem {
Ok((ty.def,
Some(format!("{}.{}",
if is_enum { "variant" } else { "structfield" },
item.name))))
} else {
Err(())
}
}
}
Def::Trait(did) => {