]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/json/conversions.rs
Rollup merge of #86887 - jyn514:cleanup-clean, r=CraftSpider
[rust.git] / src / librustdoc / json / conversions.rs
index b89a266a695e99e752cca3bd0cfa123cd094fdbe..bf8db79416b342d92fb6e1108a7c06b0f456ab5c 100644 (file)
@@ -5,9 +5,10 @@
 #![allow(rustc::default_hash_types)]
 
 use std::convert::From;
+use std::fmt;
 
 use rustc_ast::ast;
-use rustc_hir::def::CtorKind;
+use rustc_hir::{def::CtorKind, def_id::DefId};
 use rustc_middle::ty::TyCtxt;
 use rustc_span::def_id::CRATE_DEF_INDEX;
 use rustc_span::Pos;
@@ -15,7 +16,7 @@
 use rustdoc_json_types::*;
 
 use crate::clean::utils::print_const_expr;
-use crate::clean::{self, FakeDefId};
+use crate::clean::{self, ItemId};
 use crate::formats::item_type::ItemType;
 use crate::json::JsonRenderer;
 use std::collections::HashSet;
@@ -30,7 +31,7 @@ pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
             .into_iter()
             .flatten()
             .filter_map(|clean::ItemLink { link, did, .. }| {
-                did.map(|did| (link.clone(), from_def_id(did.into())))
+                did.map(|did| (link.clone(), from_item_id(did.into())))
             })
             .collect();
         let docs = item.attrs.collapsed_doc_value();
@@ -47,7 +48,7 @@ pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
             _ => from_clean_item(item, self.tcx),
         };
         Some(Item {
-            id: from_def_id(def_id),
+            id: from_item_id(def_id),
             crate_id: def_id.krate().as_u32(),
             name: name.map(|sym| sym.to_string()),
             span: self.convert_span(span),
@@ -86,7 +87,7 @@ fn convert_visibility(&self, v: clean::Visibility) -> Visibility {
             Inherited => Visibility::Default,
             Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
             Restricted(did) => Visibility::Restricted {
-                parent: from_def_id(did.into()),
+                parent: from_item_id(did.into()),
                 path: self.tcx.def_path(did).to_string_no_crate_verbose(),
             },
         }
@@ -170,12 +171,24 @@ fn from_tcx(kind: clean::TypeBindingKind, tcx: TyCtxt<'_>) -> Self {
     }
 }
 
-crate fn from_def_id(did: FakeDefId) -> Id {
+crate fn from_item_id(did: ItemId) -> Id {
+    struct DisplayDefId(DefId);
+
+    impl fmt::Display for DisplayDefId {
+        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+            write!(f, "{}:{}", self.0.krate.as_u32(), u32::from(self.0.index))
+        }
+    }
+
     match did {
-        FakeDefId::Real(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))),
-        // We need to differentiate real and fake ids, because the indices might overlap for fake
-        // and real DefId's, which would cause two different Id's treated as they were the same.
-        FakeDefId::Fake(idx, krate) => Id(format!("F{}:{}", krate.as_u32(), u32::from(idx))),
+        ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did))),
+        ItemId::Blanket { for_, impl_id } => {
+            Id(format!("b:{}-{}", DisplayDefId(impl_id), DisplayDefId(for_)))
+        }
+        ItemId::Auto { for_, trait_ } => {
+            Id(format!("a:{}-{}", DisplayDefId(trait_), DisplayDefId(for_)))
+        }
+        ItemId::Primitive(ty, krate) => Id(format!("p:{}:{}", krate.as_u32(), ty.as_sym())),
     }
 }
 
@@ -375,7 +388,7 @@ fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
         match ty {
             ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath {
                 name: path.whole_name(),
-                id: from_def_id(did.into()),
+                id: from_item_id(did.into()),
                 args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
                 param_names: Vec::new(),
             },
@@ -387,7 +400,7 @@ fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
 
                 Type::ResolvedPath {
                     name: path.whole_name(),
-                    id: from_def_id(id.into()),
+                    id: from_item_id(id.into()),
                     args: path
                         .segments
                         .last()
@@ -568,13 +581,13 @@ fn from_tcx(import: clean::Import, _tcx: TyCtxt<'_>) -> Self {
             Simple(s) => Import {
                 source: import.source.path.whole_name(),
                 name: s.to_string(),
-                id: import.source.did.map(FakeDefId::from).map(from_def_id),
+                id: import.source.did.map(ItemId::from).map(from_item_id),
                 glob: false,
             },
             Glob => Import {
                 source: import.source.path.whole_name(),
                 name: import.source.path.last_name().to_string(),
-                id: import.source.did.map(FakeDefId::from).map(from_def_id),
+                id: import.source.did.map(ItemId::from).map(from_item_id),
                 glob: true,
             },
         }
@@ -668,5 +681,5 @@ fn from_tcx(kind: ItemType, _tcx: TyCtxt<'_>) -> Self {
 }
 
 fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
-    items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect()
+    items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect()
 }