@@ -38,7 +38,7 @@ impl JsonRenderer<'_> {
38
38
Some ( UrlFragment :: UserWritten ( _) ) | None => * page_id,
39
39
} ;
40
40
41
- ( link. clone ( ) , id_from_item_inner ( id. into ( ) , self . tcx , None ) )
41
+ ( link. clone ( ) , id_from_item_inner ( id. into ( ) , self . tcx , None , None ) )
42
42
} )
43
43
. collect ( ) ;
44
44
let docs = item. attrs . collapsed_doc_value ( ) ;
@@ -108,7 +108,7 @@ impl JsonRenderer<'_> {
108
108
Some ( ty:: Visibility :: Public ) => Visibility :: Public ,
109
109
Some ( ty:: Visibility :: Restricted ( did) ) if did. is_crate_root ( ) => Visibility :: Crate ,
110
110
Some ( ty:: Visibility :: Restricted ( did) ) => Visibility :: Restricted {
111
- parent : id_from_item_inner ( did. into ( ) , self . tcx , None ) ,
111
+ parent : id_from_item_inner ( did. into ( ) , self . tcx , None , None ) ,
112
112
path : self . tcx . def_path ( did) . to_string_no_crate_verbose ( ) ,
113
113
} ,
114
114
}
@@ -208,12 +208,17 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
208
208
/// It generates an ID as follows:
209
209
///
210
210
/// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
211
- pub ( crate ) fn id_from_item_inner ( item_id : ItemId , tcx : TyCtxt < ' _ > , extra : Option < & Id > ) -> Id {
212
- struct DisplayDefId < ' a , ' b > ( DefId , TyCtxt < ' a > , Option < & ' b Id > ) ;
211
+ pub ( crate ) fn id_from_item_inner (
212
+ item_id : ItemId ,
213
+ tcx : TyCtxt < ' _ > ,
214
+ extra : Option < & Id > ,
215
+ name : Option < Symbol > ,
216
+ ) -> Id {
217
+ struct DisplayDefId < ' a , ' b > ( DefId , TyCtxt < ' a > , Option < & ' b Id > , Option < Symbol > ) ;
213
218
214
219
impl < ' a , ' b > fmt:: Display for DisplayDefId < ' a , ' b > {
215
220
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
216
- let DisplayDefId ( def_id, tcx, extra) = self ;
221
+ let DisplayDefId ( def_id, tcx, extra, name ) = self ;
217
222
// We need this workaround because primitive types' DefId actually refers to
218
223
// their parent module, which isn't present in the output JSON items. So
219
224
// instead, we directly get the primitive symbol and convert it to u32 to
@@ -225,41 +230,54 @@ pub(crate) fn id_from_item_inner(item_id: ItemId, tcx: TyCtxt<'_>, extra: Option
225
230
} else {
226
231
""
227
232
} ;
228
- let name = if matches ! ( tcx. def_kind( def_id) , DefKind :: Mod ) &&
229
- let Some ( prim) = tcx. get_attrs ( * def_id, sym:: doc)
230
- . flat_map ( |attr| attr. meta_item_list ( ) . unwrap_or_default ( ) )
231
- . filter ( |attr| attr. has_name ( sym:: primitive) )
232
- . find_map ( |attr| attr. value_str ( ) ) {
233
- format ! ( ":{}" , prim. as_u32( ) )
234
- } else {
235
- tcx
236
- . opt_item_name ( * def_id)
237
- . map ( |n| format ! ( ":{}" , n. as_u32( ) ) )
238
- . unwrap_or_default ( )
233
+ let name = match name {
234
+ Some ( name) => format ! ( ":{}" , name. as_u32( ) ) ,
235
+ None => {
236
+ // We need this workaround because primitive types' DefId actually refers to
237
+ // their parent module, which isn't present in the output JSON items. So
238
+ // instead, we directly get the primitive symbol and convert it to u32 to
239
+ // generate the ID.
240
+ if matches ! ( tcx. def_kind( def_id) , DefKind :: Mod ) &&
241
+ let Some ( prim) = tcx. get_attrs ( * def_id, sym:: doc)
242
+ . flat_map ( |attr| attr. meta_item_list ( ) . unwrap_or_default ( ) )
243
+ . filter ( |attr| attr. has_name ( sym:: primitive) )
244
+ . find_map ( |attr| attr. value_str ( ) ) {
245
+ format ! ( ":{}" , prim. as_u32( ) )
246
+ } else {
247
+ tcx
248
+ . opt_item_name ( * def_id)
249
+ . map ( |n| format ! ( ":{}" , n. as_u32( ) ) )
250
+ . unwrap_or_default ( )
251
+ }
252
+ }
239
253
} ;
240
- write ! ( f, "{}:{}{name}{extra}" , self . 0 . krate. as_u32( ) , u32 :: from( self . 0 . index) )
254
+ write ! ( f, "{}:{}{name}{extra}" , def_id . krate. as_u32( ) , u32 :: from( def_id . index) )
241
255
}
242
256
}
243
257
244
258
match item_id {
245
- ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did, tcx, extra) ) ) ,
246
- ItemId :: Blanket { for_, impl_id } => {
247
- Id ( format ! ( "b:{}-{}" , DisplayDefId ( impl_id, tcx, None ) , DisplayDefId ( for_, tcx, extra) ) )
248
- }
249
- ItemId :: Auto { for_, trait_ } => {
250
- Id ( format ! ( "a:{}-{}" , DisplayDefId ( trait_, tcx, None ) , DisplayDefId ( for_, tcx, extra) ) )
251
- }
259
+ ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did, tcx, extra, name) ) ) ,
260
+ ItemId :: Blanket { for_, impl_id } => Id ( format ! (
261
+ "b:{}-{}" ,
262
+ DisplayDefId ( impl_id, tcx, None , None ) ,
263
+ DisplayDefId ( for_, tcx, extra, name)
264
+ ) ) ,
265
+ ItemId :: Auto { for_, trait_ } => Id ( format ! (
266
+ "a:{}-{}" ,
267
+ DisplayDefId ( trait_, tcx, None , None ) ,
268
+ DisplayDefId ( for_, tcx, extra, name)
269
+ ) ) ,
252
270
}
253
271
}
254
272
255
273
pub ( crate ) fn id_from_item ( item : & clean:: Item , tcx : TyCtxt < ' _ > ) -> Id {
256
274
match * item. kind {
257
275
clean:: ItemKind :: ImportItem ( ref import) => {
258
276
let extra =
259
- import. source . did . map ( ItemId :: from) . map ( |i| id_from_item_inner ( i, tcx, None ) ) ;
260
- id_from_item_inner ( item. item_id , tcx, extra. as_ref ( ) )
277
+ import. source . did . map ( ItemId :: from) . map ( |i| id_from_item_inner ( i, tcx, None , None ) ) ;
278
+ id_from_item_inner ( item. item_id , tcx, extra. as_ref ( ) , item . name )
261
279
}
262
- _ => id_from_item_inner ( item. item_id , tcx, None ) ,
280
+ _ => id_from_item_inner ( item. item_id , tcx, None , item . name ) ,
263
281
}
264
282
}
265
283
@@ -533,7 +551,7 @@ impl FromWithTcx<clean::Path> for Path {
533
551
fn from_tcx ( path : clean:: Path , tcx : TyCtxt < ' _ > ) -> Path {
534
552
Path {
535
553
name : path. whole_name ( ) ,
536
- id : id_from_item_inner ( path. def_id ( ) . into ( ) , tcx, None ) ,
554
+ id : id_from_item_inner ( path. def_id ( ) . into ( ) , tcx, None , None ) ,
537
555
args : path. segments . last ( ) . map ( |args| Box :: new ( args. clone ( ) . args . into_tcx ( tcx) ) ) ,
538
556
}
539
557
}
@@ -710,7 +728,7 @@ impl FromWithTcx<clean::Import> for Import {
710
728
Import {
711
729
source : import. source . path . whole_name ( ) ,
712
730
name,
713
- id : import. source . did . map ( ItemId :: from) . map ( |i| id_from_item_inner ( i, tcx, None ) ) ,
731
+ id : import. source . did . map ( ItemId :: from) . map ( |i| id_from_item_inner ( i, tcx, None , None ) ) ,
714
732
glob,
715
733
}
716
734
}
0 commit comments