1+ //! Id handling for rustdoc-json.
2+ //!
3+ //! Manages the creation of [`rustdoc_json_types::Id`] and the
4+ //! fact that these don't corespond exactly to [`DefId`], because
5+ //! [`rustdoc_json_types::Item`] doesn't corespond exactly to what
6+ //! other phasises thing of as an "item".
7+
18use rustc_data_structures:: fx:: FxHashMap ;
29use rustc_hir:: def:: DefKind ;
310use rustc_hir:: def_id:: DefId ;
@@ -26,34 +33,36 @@ pub(super) struct FullItemId {
2633 /// used for edge-cases.
2734 def_id : DefId ,
2835
29- /// An extra DefId for auto-trait-impls or blanket-impls. These don't have DefId's
30- /// as they're synthesized by rustdoc.
36+ /// An extra [`DefId`], which we need for:
37+ ///
38+ /// 1. Auto-trait impls synthesized by rustdoc.
39+ /// 2. Blanket impls synthesized by rustdoc.
40+ /// 3. Splitting of reexports of multiple items.
41+ ///
42+ /// Eg:
43+ ///
44+ /// ```rust
45+ /// mod module {
46+ /// pub struct Foo {}; // Exists in type namespace
47+ /// pub fn Foo(){} // Exists in value namespace
48+ /// }
49+ ///
50+ /// pub use module::Foo; // Imports both items
51+ /// ```
52+ ///
53+ /// In HIR, the `pub use` is just 1 item, but in rustdoc-json it's 2, so
54+ /// we need to disambiguate.
3155 extra_id : Option < DefId > ,
3256
3357 /// Needed for `rustc_doc_primitive` modules.
3458 ///
35- /// For these, 1 DefId is used for both the primitive and the fake-module
59+ /// For these, 1 [` DefId`] is used for both the primitive and the fake-module
3660 /// that holds it's docs.
3761 ///
3862 /// N.B. This only matters when documenting the standard library with
3963 /// `--document-private-items`. Maybe we should delete that module, and
4064 /// remove this.
4165 name : Option < Symbol > ,
42-
43- /// Used to distinguish imports of different items with the same name.
44- ///
45- /// ```rust
46- /// mod module {
47- /// pub struct Foo {}; // Exists in type namespace
48- /// pub fn Foo(){} // Exists in value namespace
49- /// }
50- ///
51- /// pub use module::Foo; // Imports both items
52- /// ```
53- ///
54- /// In HIR, the `pub use` is just 1 item, but in rustdoc-json it's 2, so
55- /// we need to disambiguate.
56- imported_id : Option < types:: Id > ,
5766}
5867
5968impl JsonRenderer < ' _ > {
@@ -65,9 +74,9 @@ impl JsonRenderer<'_> {
6574 & self ,
6675 item_id : ItemId ,
6776 name : Option < Symbol > ,
68- imported_id : Option < Id > ,
77+ imported_id : Option < DefId > ,
6978 ) -> Id {
70- let ( def_id, extra_id) = match item_id {
79+ let ( def_id, mut extra_id) = match item_id {
7180 ItemId :: DefId ( did) => ( did, None ) ,
7281 ItemId :: Blanket { for_, impl_id } => ( for_, Some ( impl_id) ) ,
7382 ItemId :: Auto { for_, trait_ } => ( for_, Some ( trait_) ) ,
@@ -92,7 +101,12 @@ impl JsonRenderer<'_> {
92101 }
93102 } ;
94103
95- let key = FullItemId { def_id, extra_id, name, imported_id } ;
104+ if let Some ( imported_id) = imported_id {
105+ assert_eq ! ( extra_id, None , "On an import item, so won't have extra from impl" ) ;
106+ extra_id = Some ( imported_id) ;
107+ }
108+
109+ let key = FullItemId { def_id, extra_id, name } ;
96110
97111 let mut interner = self . id_interner . borrow_mut ( ) ;
98112 let len = interner. len ( ) ;
@@ -104,9 +118,8 @@ impl JsonRenderer<'_> {
104118 pub ( crate ) fn id_from_item ( & self , item : & clean:: Item ) -> Id {
105119 match item. kind {
106120 clean:: ItemKind :: ImportItem ( ref import) => {
107- let extra =
108- import. source . did . map ( ItemId :: from) . map ( |i| self . id_from_item_default ( i) ) ;
109- self . id_from_item_inner ( item. item_id , item. name , extra)
121+ let imported_id = import. source . did ;
122+ self . id_from_item_inner ( item. item_id , item. name , imported_id)
110123 }
111124 _ => self . id_from_item_inner ( item. item_id , item. name , None ) ,
112125 }
0 commit comments