Skip to content

Commit

Permalink
rustdoc: Consider enum variants when resolving assoc items in doc links
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Feb 13, 2022
1 parent 1f4681a commit 482b753
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::def::{
PerNS,
};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt};
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt, TyKind};
use rustc_middle::{bug, span_bug, ty};
use rustc_session::lint::Lint;
use rustc_span::hygiene::MacroKind;
Expand Down Expand Up @@ -723,10 +723,27 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
self.resolve_associated_item(res, item_name, ns, module_id)
}
Res::Def(
DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::ForeignTy,
def_kind @ (DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::ForeignTy),
did,
) => {
debug!("looking for associated item named {} for item {:?}", item_name, did);
// Checks if item_name is a variant of the `SomeItem` enum
if ns == TypeNS && def_kind == DefKind::Enum {
match tcx.type_of(did).kind() {
TyKind::Adt(adt_def, _) => {
for variant in &adt_def.variants {
if variant.name == item_name {
return Some((
root_res,
ItemFragment(FragmentKind::Variant, variant.def_id),
));
}
}
}
_ => unreachable!(),
}
}

// Checks if item_name belongs to `impl SomeItem`
let assoc_item = tcx
.inherent_impls(did)
Expand Down
8 changes: 8 additions & 0 deletions src/test/rustdoc/intra-doc/associated-items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ impl T2 for S {
fn ambiguous_method() {}
}

// @has associated_items/enum.MyEnum.html '//a/@href' 'enum.MyEnum.html#variant.MyVariant'
/// Link to [MyEnumAlias::MyVariant]
pub enum MyEnum {
MyVariant,
}

pub type MyEnumAlias = MyEnum;

fn main() {}

0 comments on commit 482b753

Please sign in to comment.