Skip to content

Commit

Permalink
Return the actual DefId for assoc. items in register_res
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Nov 24, 2021
1 parent 8a48b37 commit 47266ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/librustdoc/clean/utils.rs
Expand Up @@ -393,20 +393,12 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
debug!("register_res({:?})", res);

let (did, kind) = match res {
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
// associated items are documented, but on the page of their parent
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
}
Res::Def(DefKind::Variant, i) => {
// variant items are documented, but on the page of their parent
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
}
// Each of these have their own page.
Res::Def(
kind
@
(Fn | TyAlias | Enum | Trait | Struct | Union | Mod | ForeignTy | Const | Static
| Macro(..) | TraitAlias),
(AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
| Union | Mod | ForeignTy | Const | Static | Macro(..) | TraitAlias),
i,
) => (i, kind.into()),
// This is part of a trait definition; document the trait.
Expand Down
13 changes: 12 additions & 1 deletion src/librustdoc/html/format.rs
Expand Up @@ -13,8 +13,10 @@ use rustc_attr::{ConstStability, StabilityLevel};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_middle::ty;
use rustc_middle::ty::DefIdTree;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::CRATE_DEF_INDEX;
use rustc_target::spec::abi::Abi;
Expand Down Expand Up @@ -502,7 +504,16 @@ crate fn href_with_root_path(
cx: &Context<'_>,
root_path: Option<&str>,
) -> Result<(String, ItemType, Vec<String>), HrefError> {
let cache = &cx.cache();
let tcx = cx.tcx();
let def_kind = tcx.def_kind(did);
let did = match def_kind {
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
// documented on their parent's page
tcx.parent(did).unwrap()
}
_ => did,
};
let cache = cx.cache();
let relative_to = &cx.current;
fn to_module_fqp(shortty: ItemType, fqp: &[String]) -> &[String] {
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
Expand Down

0 comments on commit 47266ba

Please sign in to comment.