@@ -3,7 +3,6 @@ use std::collections::BTreeMap;
33
44use rustc_data_structures:: fx:: FxHashMap ;
55use rustc_middle:: ty:: TyCtxt ;
6- use rustc_span:: def_id:: LOCAL_CRATE ;
76use rustc_span:: symbol:: Symbol ;
87use serde:: ser:: { Serialize , SerializeStruct , Serializer } ;
98
@@ -24,6 +23,7 @@ pub(crate) fn build_index<'tcx>(
2423 tcx : TyCtxt < ' tcx > ,
2524) -> String {
2625 let mut itemid_to_pathid = FxHashMap :: default ( ) ;
26+ let mut primitives = FxHashMap :: default ( ) ;
2727 let mut crate_paths = vec ! [ ] ;
2828
2929 // Attach all orphan items to the type's definition if the type
@@ -78,50 +78,83 @@ pub(crate) fn build_index<'tcx>(
7878 // First, on function signatures
7979 let mut search_index = std:: mem:: replace ( & mut cache. search_index , Vec :: new ( ) ) ;
8080 for item in search_index. iter_mut ( ) {
81+ fn insert_into_map < F : std:: hash:: Hash + Eq > (
82+ ty : & mut RenderType ,
83+ map : & mut FxHashMap < F , usize > ,
84+ itemid : F ,
85+ lastpathid : & mut usize ,
86+ crate_paths : & mut Vec < ( ItemType , Symbol ) > ,
87+ item_type : ItemType ,
88+ path : Symbol ,
89+ ) {
90+ match map. entry ( itemid) {
91+ Entry :: Occupied ( entry) => ty. id = Some ( RenderTypeId :: Index ( * entry. get ( ) ) ) ,
92+ Entry :: Vacant ( entry) => {
93+ let pathid = * lastpathid;
94+ entry. insert ( pathid) ;
95+ * lastpathid += 1 ;
96+ crate_paths. push ( ( item_type, path) ) ;
97+ ty. id = Some ( RenderTypeId :: Index ( pathid) ) ;
98+ }
99+ }
100+ }
101+
81102 fn convert_render_type (
82103 ty : & mut RenderType ,
83104 cache : & mut Cache ,
84105 itemid_to_pathid : & mut FxHashMap < ItemId , usize > ,
106+ primitives : & mut FxHashMap < Symbol , usize > ,
85107 lastpathid : & mut usize ,
86108 crate_paths : & mut Vec < ( ItemType , Symbol ) > ,
87109 ) {
88110 if let Some ( generics) = & mut ty. generics {
89111 for item in generics {
90- convert_render_type ( item, cache, itemid_to_pathid, lastpathid, crate_paths) ;
112+ convert_render_type (
113+ item,
114+ cache,
115+ itemid_to_pathid,
116+ primitives,
117+ lastpathid,
118+ crate_paths,
119+ ) ;
91120 }
92121 }
93122 let Cache { ref paths, ref external_paths, .. } = * cache;
94123 let Some ( id) = ty. id . clone ( ) else {
95124 assert ! ( ty. generics. is_some( ) ) ;
96125 return ;
97126 } ;
98- let ( itemid , path , item_type ) = match id {
127+ match id {
99128 RenderTypeId :: DefId ( defid) => {
100129 if let Some ( & ( ref fqp, item_type) ) =
101130 paths. get ( & defid) . or_else ( || external_paths. get ( & defid) )
102131 {
103- ( ItemId :: DefId ( defid) , * fqp. last ( ) . unwrap ( ) , item_type)
132+ insert_into_map (
133+ ty,
134+ itemid_to_pathid,
135+ ItemId :: DefId ( defid) ,
136+ lastpathid,
137+ crate_paths,
138+ item_type,
139+ * fqp. last ( ) . unwrap ( ) ,
140+ ) ;
104141 } else {
105142 ty. id = None ;
106- return ;
107143 }
108144 }
109- RenderTypeId :: Primitive ( primitive) => (
110- ItemId :: Primitive ( primitive, LOCAL_CRATE ) ,
111- primitive. as_sym ( ) ,
112- ItemType :: Primitive ,
113- ) ,
114- RenderTypeId :: Index ( _) => return ,
115- } ;
116- match itemid_to_pathid. entry ( itemid) {
117- Entry :: Occupied ( entry) => ty. id = Some ( RenderTypeId :: Index ( * entry. get ( ) ) ) ,
118- Entry :: Vacant ( entry) => {
119- let pathid = * lastpathid;
120- entry. insert ( pathid) ;
121- * lastpathid += 1 ;
122- crate_paths. push ( ( item_type, path) ) ;
123- ty. id = Some ( RenderTypeId :: Index ( pathid) ) ;
145+ RenderTypeId :: Primitive ( primitive) => {
146+ let sym = primitive. as_sym ( ) ;
147+ insert_into_map (
148+ ty,
149+ primitives,
150+ sym,
151+ lastpathid,
152+ crate_paths,
153+ ItemType :: Primitive ,
154+ sym,
155+ ) ;
124156 }
157+ RenderTypeId :: Index ( _) => { }
125158 }
126159 }
127160 if let Some ( search_type) = & mut item. search_type {
@@ -130,6 +163,7 @@ pub(crate) fn build_index<'tcx>(
130163 item,
131164 cache,
132165 & mut itemid_to_pathid,
166+ & mut primitives,
133167 & mut lastpathid,
134168 & mut crate_paths,
135169 ) ;
@@ -139,6 +173,7 @@ pub(crate) fn build_index<'tcx>(
139173 item,
140174 cache,
141175 & mut itemid_to_pathid,
176+ & mut primitives,
142177 & mut lastpathid,
143178 & mut crate_paths,
144179 ) ;
0 commit comments