about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2021-11-11 15:05:25 -0800
committerNoah Lev <camelidcamel@gmail.com>2021-11-24 12:00:53 -0800
commit47266bacf1585ca290cd3baca214497adc200803 (patch)
tree1d2684aab3de61fe0291f78731b09c341aca90f9
parent8a48b376d559f26a9b8fc1f1d597acb0bc0a51f9 (diff)
downloadrust-47266bacf1585ca290cd3baca214497adc200803.tar.gz
rust-47266bacf1585ca290cd3baca214497adc200803.zip
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.
-rw-r--r--src/librustdoc/clean/utils.rs12
-rw-r--r--src/librustdoc/html/format.rs13
2 files changed, 14 insertions, 11 deletions
diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs
index 11032211236..78643841a0b 100644
--- a/src/librustdoc/clean/utils.rs
+++ b/src/librustdoc/clean/utils.rs
@@ -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.
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 4f204913204..d4c5dda19f9 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -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;
@@ -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] }