@@ -38,7 +38,7 @@ impl JsonRenderer<'_> {
38
38
Some ( UrlFragment :: UserWritten ( _) ) | None => * page_id,
39
39
} ;
40
40
41
- ( link. clone ( ) , from_item_id ( id. into ( ) , self . tcx ) )
41
+ ( link. clone ( ) , id_from_item_inner ( id. into ( ) , self . tcx , None ) )
42
42
} )
43
43
. collect ( ) ;
44
44
let docs = item. attrs . collapsed_doc_value ( ) ;
@@ -50,7 +50,8 @@ impl JsonRenderer<'_> {
50
50
. collect ( ) ;
51
51
let span = item. span ( self . tcx ) ;
52
52
let visibility = item. visibility ( self . tcx ) ;
53
- let clean:: Item { name, attrs : _, kind : _, item_id, cfg : _, .. } = item;
53
+ let clean:: Item { name, item_id, .. } = item;
54
+ let id = id_from_item ( & item, self . tcx ) ;
54
55
let inner = match * item. kind {
55
56
clean:: KeywordItem => return None ,
56
57
clean:: StrippedItem ( ref inner) => {
@@ -69,7 +70,7 @@ impl JsonRenderer<'_> {
69
70
_ => from_clean_item ( item, self . tcx ) ,
70
71
} ;
71
72
Some ( Item {
72
- id : from_item_id_with_name ( item_id , self . tcx , name ) ,
73
+ id,
73
74
crate_id : item_id. krate ( ) . as_u32 ( ) ,
74
75
name : name. map ( |sym| sym. to_string ( ) ) ,
75
76
span : span. and_then ( |span| self . convert_span ( span) ) ,
@@ -107,7 +108,7 @@ impl JsonRenderer<'_> {
107
108
Some ( ty:: Visibility :: Public ) => Visibility :: Public ,
108
109
Some ( ty:: Visibility :: Restricted ( did) ) if did. is_crate_root ( ) => Visibility :: Crate ,
109
110
Some ( ty:: Visibility :: Restricted ( did) ) => Visibility :: Restricted {
110
- parent : from_item_id ( did. into ( ) , self . tcx ) ,
111
+ parent : id_from_item_inner ( did. into ( ) , self . tcx , None ) ,
111
112
path : self . tcx . def_path ( did) . to_string_no_crate_verbose ( ) ,
112
113
} ,
113
114
}
@@ -207,51 +208,58 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
207
208
/// It generates an ID as follows:
208
209
///
209
210
/// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
210
- pub ( crate ) fn from_item_id ( item_id : ItemId , tcx : TyCtxt < ' _ > ) -> Id {
211
- from_item_id_with_name ( item_id, tcx, None )
212
- }
213
-
214
- // FIXME: this function (and appending the name at the end of the ID) should be removed when
215
- // reexports are not inlined anymore for json format. It should be done in #93518.
216
- pub ( crate ) fn from_item_id_with_name ( item_id : ItemId , tcx : TyCtxt < ' _ > , name : Option < Symbol > ) -> Id {
217
- struct DisplayDefId < ' a > ( DefId , TyCtxt < ' a > , Option < Symbol > ) ;
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 > ) ;
218
213
219
- impl < ' a > fmt:: Display for DisplayDefId < ' a > {
214
+ impl < ' a , ' b > fmt:: Display for DisplayDefId < ' a , ' b > {
220
215
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
221
- let DisplayDefId ( def_id, tcx, name) = self ;
222
- let name = match name {
223
- Some ( name) => format ! ( ":{}" , name. as_u32( ) ) ,
224
- None => {
225
- // We need this workaround because primitive types' DefId actually refers to
226
- // their parent module, which isn't present in the output JSON items. So
227
- // instead, we directly get the primitive symbol and convert it to u32 to
228
- // generate the ID.
229
- if matches ! ( tcx. def_kind( def_id) , DefKind :: Mod ) &&
230
- let Some ( prim) = tcx. get_attrs ( * def_id, sym:: doc)
231
- . flat_map ( |attr| attr. meta_item_list ( ) . unwrap_or_default ( ) )
232
- . filter ( |attr| attr. has_name ( sym:: primitive) )
233
- . find_map ( |attr| attr. value_str ( ) ) {
234
- format ! ( ":{}" , prim. as_u32( ) )
235
- } else {
236
- tcx
237
- . opt_item_name ( * def_id)
238
- . map ( |n| format ! ( ":{}" , n. as_u32( ) ) )
239
- . unwrap_or_default ( )
240
- }
241
- }
216
+ let DisplayDefId ( def_id, tcx, extra) = self ;
217
+ // We need this workaround because primitive types' DefId actually refers to
218
+ // their parent module, which isn't present in the output JSON items. So
219
+ // instead, we directly get the primitive symbol and convert it to u32 to
220
+ // generate the ID.
221
+ let s;
222
+ let extra = if let Some ( e) = extra {
223
+ s = format ! ( "-{}" , e. 0 ) ;
224
+ & s
225
+ } else {
226
+ ""
227
+ } ;
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 ( )
242
239
} ;
243
- write ! ( f, "{}:{}{} " , self . 0 . krate. as_u32( ) , u32 :: from( self . 0 . index) , name )
240
+ write ! ( f, "{}:{}{name}{extra} " , self . 0 . krate. as_u32( ) , u32 :: from( self . 0 . index) )
244
241
}
245
242
}
246
243
247
244
match item_id {
248
- ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did, tcx, name ) ) ) ,
245
+ ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did, tcx, extra ) ) ) ,
249
246
ItemId :: Blanket { for_, impl_id } => {
250
- Id ( format ! ( "b:{}-{}" , DisplayDefId ( impl_id, tcx, None ) , DisplayDefId ( for_, tcx, name ) ) )
247
+ Id ( format ! ( "b:{}-{}" , DisplayDefId ( impl_id, tcx, None ) , DisplayDefId ( for_, tcx, extra ) ) )
251
248
}
252
249
ItemId :: Auto { for_, trait_ } => {
253
- Id ( format ! ( "a:{}-{}" , DisplayDefId ( trait_, tcx, None ) , DisplayDefId ( for_, tcx, name) ) )
250
+ Id ( format ! ( "a:{}-{}" , DisplayDefId ( trait_, tcx, None ) , DisplayDefId ( for_, tcx, extra) ) )
251
+ }
252
+ }
253
+ }
254
+
255
+ pub ( crate ) fn id_from_item ( item : & clean:: Item , tcx : TyCtxt < ' _ > ) -> Id {
256
+ match * item. kind {
257
+ clean:: ItemKind :: ImportItem ( ref import) => {
258
+ 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 ( ) )
254
261
}
262
+ _ => id_from_item_inner ( item. item_id , tcx, None ) ,
255
263
}
256
264
}
257
265
@@ -525,7 +533,7 @@ impl FromWithTcx<clean::Path> for Path {
525
533
fn from_tcx ( path : clean:: Path , tcx : TyCtxt < ' _ > ) -> Path {
526
534
Path {
527
535
name : path. whole_name ( ) ,
528
- id : from_item_id ( path. def_id ( ) . into ( ) , tcx) ,
536
+ id : id_from_item_inner ( path. def_id ( ) . into ( ) , tcx, None ) ,
529
537
args : path. segments . last ( ) . map ( |args| Box :: new ( args. clone ( ) . args . into_tcx ( tcx) ) ) ,
530
538
}
531
539
}
@@ -702,7 +710,7 @@ impl FromWithTcx<clean::Import> for Import {
702
710
Import {
703
711
source : import. source . path . whole_name ( ) ,
704
712
name,
705
- id : import. source . did . map ( ItemId :: from) . map ( |i| from_item_id ( i, tcx) ) ,
713
+ id : import. source . did . map ( ItemId :: from) . map ( |i| id_from_item_inner ( i, tcx, None ) ) ,
706
714
glob,
707
715
}
708
716
}
@@ -791,7 +799,7 @@ fn ids(items: impl IntoIterator<Item = clean::Item>, tcx: TyCtxt<'_>) -> Vec<Id>
791
799
items
792
800
. into_iter ( )
793
801
. filter ( |x| !x. is_stripped ( ) && !x. is_keyword ( ) )
794
- . map ( |i| from_item_id_with_name ( i . item_id , tcx, i . name ) )
802
+ . map ( |i| id_from_item ( & i , tcx) )
795
803
. collect ( )
796
804
}
797
805
@@ -801,12 +809,10 @@ fn ids_keeping_stripped(
801
809
) -> Vec < Option < Id > > {
802
810
items
803
811
. into_iter ( )
804
- . map ( |i| {
805
- if !i. is_stripped ( ) && !i. is_keyword ( ) {
806
- Some ( from_item_id_with_name ( i. item_id , tcx, i. name ) )
807
- } else {
808
- None
809
- }
810
- } )
812
+ . map (
813
+ |i| {
814
+ if !i. is_stripped ( ) && !i. is_keyword ( ) { Some ( id_from_item ( & i, tcx) ) } else { None }
815
+ } ,
816
+ )
811
817
. collect ( )
812
818
}
0 commit comments