Skip to content

Commit

Permalink
Only special case struct fields for intra-doc links, not enum variants
Browse files Browse the repository at this point in the history
Variants are already handled by `resolve_str_path_error`, rustdoc doesn't need to consider them separately.
  • Loading branch information
jyn514 committed Dec 18, 2021
1 parent d3f3004 commit ae3ddf1
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,28 +643,22 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
if ns != Namespace::ValueNS {
return None;
}
debug!("looking for variants or fields named {} for {:?}", item_name, did);
debug!("looking for fields named {} for {:?}", item_name, did);
// FIXME: this doesn't really belong in `associated_item` (maybe `variant_field` is better?)
// NOTE: it's different from variant_field because it resolves fields and variants,
// NOTE: it's different from variant_field because it only resolves struct fields,
// not variant fields (2 path segments, not 3).
let def = match tcx.type_of(did).kind() {
ty::Adt(def, _) => def,
ty::Adt(def, _) if !def.is_enum() => def,
_ => return None,
};
let field = if def.is_enum() {
def.all_fields().find(|item| item.ident.name == item_name)
} else {
def.non_enum_variant().fields.iter().find(|item| item.ident.name == item_name)
}?;
let kind = if def.is_enum() { DefKind::Variant } else { DefKind::Field };
let field = def.non_enum_variant().fields.iter().find(|item| item.ident.name == item_name)?;
Some((
root_res,
format!(
"{}.{}",
if def.is_enum() { "variant" } else { "structfield" },
"structfield.{}",
field.ident
),
Some((kind, field.did)),
Some((DefKind::Field, field.did)),
))
}
Res::Def(DefKind::Trait, did) => tcx
Expand Down

0 comments on commit ae3ddf1

Please sign in to comment.