Skip to content

Commit 482b753

Browse files
committed
rustdoc: Consider enum variants when resolving assoc items in doc links
1 parent 1f4681a commit 482b753

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::def::{
1010
PerNS,
1111
};
1212
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
13-
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt};
13+
use rustc_middle::ty::{DefIdTree, Ty, TyCtxt, TyKind};
1414
use rustc_middle::{bug, span_bug, ty};
1515
use rustc_session::lint::Lint;
1616
use rustc_span::hygiene::MacroKind;
@@ -723,10 +723,27 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
723723
self.resolve_associated_item(res, item_name, ns, module_id)
724724
}
725725
Res::Def(
726-
DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::ForeignTy,
726+
def_kind @ (DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::ForeignTy),
727727
did,
728728
) => {
729729
debug!("looking for associated item named {} for item {:?}", item_name, did);
730+
// Checks if item_name is a variant of the `SomeItem` enum
731+
if ns == TypeNS && def_kind == DefKind::Enum {
732+
match tcx.type_of(did).kind() {
733+
TyKind::Adt(adt_def, _) => {
734+
for variant in &adt_def.variants {
735+
if variant.name == item_name {
736+
return Some((
737+
root_res,
738+
ItemFragment(FragmentKind::Variant, variant.def_id),
739+
));
740+
}
741+
}
742+
}
743+
_ => unreachable!(),
744+
}
745+
}
746+
730747
// Checks if item_name belongs to `impl SomeItem`
731748
let assoc_item = tcx
732749
.inherent_impls(did)

src/test/rustdoc/intra-doc/associated-items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ impl T2 for S {
5757
fn ambiguous_method() {}
5858
}
5959

60+
// @has associated_items/enum.MyEnum.html '//a/@href' 'enum.MyEnum.html#variant.MyVariant'
61+
/// Link to [MyEnumAlias::MyVariant]
62+
pub enum MyEnum {
63+
MyVariant,
64+
}
65+
66+
pub type MyEnumAlias = MyEnum;
67+
6068
fn main() {}

0 commit comments

Comments
 (0)