Skip to content

Commit 47266ba

Browse files
committed
Return the actual DefId for assoc. items in register_res
Before, if `register_res` were called on an associated item or enum variant, it would return the parent's `DefId`. Now, it returns the actual `DefId`. This change is a step toward removing `Type::ResolvedPath.did` and potentially removing `kind_side_channel` in rustdoc. It also just simplifies rustdoc's behavior.
1 parent 8a48b37 commit 47266ba

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/librustdoc/clean/utils.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -393,20 +393,12 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
393393
debug!("register_res({:?})", res);
394394

395395
let (did, kind) = match res {
396-
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
397-
// associated items are documented, but on the page of their parent
398-
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
399-
}
400-
Res::Def(DefKind::Variant, i) => {
401-
// variant items are documented, but on the page of their parent
402-
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
403-
}
404396
// Each of these have their own page.
405397
Res::Def(
406398
kind
407399
@
408-
(Fn | TyAlias | Enum | Trait | Struct | Union | Mod | ForeignTy | Const | Static
409-
| Macro(..) | TraitAlias),
400+
(AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
401+
| Union | Mod | ForeignTy | Const | Static | Macro(..) | TraitAlias),
410402
i,
411403
) => (i, kind.into()),
412404
// This is part of a trait definition; document the trait.

src/librustdoc/html/format.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use rustc_attr::{ConstStability, StabilityLevel};
1313
use rustc_data_structures::captures::Captures;
1414
use rustc_data_structures::fx::FxHashSet;
1515
use rustc_hir as hir;
16+
use rustc_hir::def::DefKind;
1617
use rustc_hir::def_id::DefId;
1718
use rustc_middle::ty;
19+
use rustc_middle::ty::DefIdTree;
1820
use rustc_middle::ty::TyCtxt;
1921
use rustc_span::def_id::CRATE_DEF_INDEX;
2022
use rustc_target::spec::abi::Abi;
@@ -502,7 +504,16 @@ crate fn href_with_root_path(
502504
cx: &Context<'_>,
503505
root_path: Option<&str>,
504506
) -> Result<(String, ItemType, Vec<String>), HrefError> {
505-
let cache = &cx.cache();
507+
let tcx = cx.tcx();
508+
let def_kind = tcx.def_kind(did);
509+
let did = match def_kind {
510+
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
511+
// documented on their parent's page
512+
tcx.parent(did).unwrap()
513+
}
514+
_ => did,
515+
};
516+
let cache = cx.cache();
506517
let relative_to = &cx.current;
507518
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
508519
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }

0 commit comments

Comments
 (0)