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
+
1
8
use rustc_data_structures:: fx:: FxHashMap ;
2
9
use rustc_hir:: def:: DefKind ;
3
10
use rustc_hir:: def_id:: DefId ;
@@ -26,34 +33,36 @@ pub(super) struct FullItemId {
26
33
/// used for edge-cases.
27
34
def_id : DefId ,
28
35
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.
31
55
extra_id : Option < DefId > ,
32
56
33
57
/// Needed for `rustc_doc_primitive` modules.
34
58
///
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
36
60
/// that holds it's docs.
37
61
///
38
62
/// N.B. This only matters when documenting the standard library with
39
63
/// `--document-private-items`. Maybe we should delete that module, and
40
64
/// remove this.
41
65
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 > ,
57
66
}
58
67
59
68
impl JsonRenderer < ' _ > {
@@ -65,9 +74,9 @@ impl JsonRenderer<'_> {
65
74
& self ,
66
75
item_id : ItemId ,
67
76
name : Option < Symbol > ,
68
- imported_id : Option < Id > ,
77
+ imported_id : Option < DefId > ,
69
78
) -> Id {
70
- let ( def_id, extra_id) = match item_id {
79
+ let ( def_id, mut extra_id) = match item_id {
71
80
ItemId :: DefId ( did) => ( did, None ) ,
72
81
ItemId :: Blanket { for_, impl_id } => ( for_, Some ( impl_id) ) ,
73
82
ItemId :: Auto { for_, trait_ } => ( for_, Some ( trait_) ) ,
@@ -92,7 +101,12 @@ impl JsonRenderer<'_> {
92
101
}
93
102
} ;
94
103
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 } ;
96
110
97
111
let mut interner = self . id_interner . borrow_mut ( ) ;
98
112
let len = interner. len ( ) ;
@@ -104,9 +118,8 @@ impl JsonRenderer<'_> {
104
118
pub ( crate ) fn id_from_item ( & self , item : & clean:: Item ) -> Id {
105
119
match item. kind {
106
120
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)
110
123
}
111
124
_ => self . id_from_item_inner ( item. item_id , item. name , None ) ,
112
125
}
0 commit comments