@@ -10,7 +10,7 @@ use std::fmt;
10
10
use rustc_ast:: ast;
11
11
use rustc_hir:: { def:: CtorKind , def_id:: DefId } ;
12
12
use rustc_middle:: ty:: { self , TyCtxt } ;
13
- use rustc_span:: Pos ;
13
+ use rustc_span:: { Pos , Symbol } ;
14
14
use rustc_target:: spec:: abi:: Abi as RustcAbi ;
15
15
16
16
use rustdoc_json_types:: * ;
@@ -29,7 +29,9 @@ impl JsonRenderer<'_> {
29
29
. get ( & item. item_id )
30
30
. into_iter ( )
31
31
. flatten ( )
32
- . map ( |clean:: ItemLink { link, did, .. } | ( link. clone ( ) , from_item_id ( ( * did) . into ( ) ) ) )
32
+ . map ( |clean:: ItemLink { link, did, .. } | {
33
+ ( link. clone ( ) , from_item_id ( ( * did) . into ( ) , self . tcx ) )
34
+ } )
33
35
. collect ( ) ;
34
36
let docs = item. attrs . collapsed_doc_value ( ) ;
35
37
let attrs = item
@@ -45,7 +47,7 @@ impl JsonRenderer<'_> {
45
47
_ => from_clean_item ( item, self . tcx ) ,
46
48
} ;
47
49
Some ( Item {
48
- id : from_item_id ( item_id) ,
50
+ id : from_item_id_with_name ( item_id, self . tcx , name ) ,
49
51
crate_id : item_id. krate ( ) . as_u32 ( ) ,
50
52
name : name. map ( |sym| sym. to_string ( ) ) ,
51
53
span : self . convert_span ( span) ,
@@ -84,7 +86,7 @@ impl JsonRenderer<'_> {
84
86
Inherited => Visibility :: Default ,
85
87
Restricted ( did) if did. is_crate_root ( ) => Visibility :: Crate ,
86
88
Restricted ( did) => Visibility :: Restricted {
87
- parent : from_item_id ( did. into ( ) ) ,
89
+ parent : from_item_id ( did. into ( ) , self . tcx ) ,
88
90
path : self . tcx . def_path ( did) . to_string_no_crate_verbose ( ) ,
89
91
} ,
90
92
}
@@ -173,22 +175,39 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
173
175
}
174
176
}
175
177
176
- pub ( crate ) fn from_item_id ( item_id : ItemId ) -> Id {
177
- struct DisplayDefId ( DefId ) ;
178
+ /// It generates an ID as follows:
179
+ ///
180
+ /// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
181
+ pub ( crate ) fn from_item_id ( item_id : ItemId , tcx : TyCtxt < ' _ > ) -> Id {
182
+ from_item_id_with_name ( item_id, tcx, None )
183
+ }
184
+
185
+ // FIXME: this function (and appending the name at the end of the ID) should be removed when
186
+ // reexports are not inlined anymore for json format. It should be done in #93518.
187
+ pub ( crate ) fn from_item_id_with_name ( item_id : ItemId , tcx : TyCtxt < ' _ > , name : Option < Symbol > ) -> Id {
188
+ struct DisplayDefId < ' a > ( DefId , TyCtxt < ' a > , Option < Symbol > ) ;
178
189
179
- impl fmt:: Display for DisplayDefId {
190
+ impl < ' a > fmt:: Display for DisplayDefId < ' a > {
180
191
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
181
- write ! ( f, "{}:{}" , self . 0 . krate. as_u32( ) , u32 :: from( self . 0 . index) )
192
+ let name = match self . 2 {
193
+ Some ( name) => format ! ( ":{}" , name. as_u32( ) ) ,
194
+ None => self
195
+ . 1
196
+ . opt_item_name ( self . 0 )
197
+ . map ( |n| format ! ( ":{}" , n. as_u32( ) ) )
198
+ . unwrap_or_default ( ) ,
199
+ } ;
200
+ write ! ( f, "{}:{}{}" , self . 0 . krate. as_u32( ) , u32 :: from( self . 0 . index) , name)
182
201
}
183
202
}
184
203
185
204
match item_id {
186
- ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did) ) ) ,
205
+ ItemId :: DefId ( did) => Id ( format ! ( "{}" , DisplayDefId ( did, tcx , name ) ) ) ,
187
206
ItemId :: Blanket { for_, impl_id } => {
188
- Id ( format ! ( "b:{}-{}" , DisplayDefId ( impl_id) , DisplayDefId ( for_) ) )
207
+ Id ( format ! ( "b:{}-{}" , DisplayDefId ( impl_id, tcx , None ) , DisplayDefId ( for_, tcx , name ) ) )
189
208
}
190
209
ItemId :: Auto { for_, trait_ } => {
191
- Id ( format ! ( "a:{}-{}" , DisplayDefId ( trait_) , DisplayDefId ( for_) ) )
210
+ Id ( format ! ( "a:{}-{}" , DisplayDefId ( trait_, tcx , None ) , DisplayDefId ( for_, tcx , name ) ) )
192
211
}
193
212
ItemId :: Primitive ( ty, krate) => Id ( format ! ( "p:{}:{}" , krate. as_u32( ) , ty. as_sym( ) ) ) ,
194
213
}
@@ -201,7 +220,7 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
201
220
let header = item. fn_header ( tcx) ;
202
221
203
222
match * item. kind {
204
- ModuleItem ( m) => ItemEnum :: Module ( Module { is_crate, items : ids ( m. items ) } ) ,
223
+ ModuleItem ( m) => ItemEnum :: Module ( Module { is_crate, items : ids ( m. items , tcx ) } ) ,
205
224
ImportItem ( i) => ItemEnum :: Import ( i. into_tcx ( tcx) ) ,
206
225
StructItem ( s) => ItemEnum :: Struct ( s. into_tcx ( tcx) ) ,
207
226
UnionItem ( u) => ItemEnum :: Union ( u. into_tcx ( tcx) ) ,
@@ -255,7 +274,7 @@ impl FromWithTcx<clean::Struct> for Struct {
255
274
struct_type : from_ctor_kind ( struct_type) ,
256
275
generics : generics. into_tcx ( tcx) ,
257
276
fields_stripped,
258
- fields : ids ( fields) ,
277
+ fields : ids ( fields, tcx ) ,
259
278
impls : Vec :: new ( ) , // Added in JsonRenderer::item
260
279
}
261
280
}
@@ -268,7 +287,7 @@ impl FromWithTcx<clean::Union> for Union {
268
287
Union {
269
288
generics : generics. into_tcx ( tcx) ,
270
289
fields_stripped,
271
- fields : ids ( fields) ,
290
+ fields : ids ( fields, tcx ) ,
272
291
impls : Vec :: new ( ) , // Added in JsonRenderer::item
273
292
}
274
293
}
@@ -413,7 +432,7 @@ impl FromWithTcx<clean::Type> for Type {
413
432
match ty {
414
433
clean:: Type :: Path { path } => Type :: ResolvedPath {
415
434
name : path. whole_name ( ) ,
416
- id : from_item_id ( path. def_id ( ) . into ( ) ) ,
435
+ id : from_item_id ( path. def_id ( ) . into ( ) , tcx ) ,
417
436
args : path. segments . last ( ) . map ( |args| Box :: new ( args. clone ( ) . args . into_tcx ( tcx) ) ) ,
418
437
param_names : Vec :: new ( ) ,
419
438
} ,
@@ -422,7 +441,7 @@ impl FromWithTcx<clean::Type> for Type {
422
441
423
442
Type :: ResolvedPath {
424
443
name : first_trait. whole_name ( ) ,
425
- id : from_item_id ( first_trait. def_id ( ) . into ( ) ) ,
444
+ id : from_item_id ( first_trait. def_id ( ) . into ( ) , tcx ) ,
426
445
args : first_trait
427
446
. segments
428
447
. last ( )
@@ -517,7 +536,7 @@ impl FromWithTcx<clean::Trait> for Trait {
517
536
Trait {
518
537
is_auto,
519
538
is_unsafe : unsafety == rustc_hir:: Unsafety :: Unsafe ,
520
- items : ids ( items) ,
539
+ items : ids ( items, tcx ) ,
521
540
generics : generics. into_tcx ( tcx) ,
522
541
bounds : bounds. into_iter ( ) . map ( |x| x. into_tcx ( tcx) ) . collect ( ) ,
523
542
implementations : Vec :: new ( ) , // Added in JsonRenderer::item
@@ -550,7 +569,7 @@ impl FromWithTcx<clean::Impl> for Impl {
550
569
. collect ( ) ,
551
570
trait_,
552
571
for_ : for_. into_tcx ( tcx) ,
553
- items : ids ( items) ,
572
+ items : ids ( items, tcx ) ,
554
573
negative : negative_polarity,
555
574
synthetic,
556
575
blanket_impl : blanket_impl. map ( |x| x. into_tcx ( tcx) ) ,
@@ -593,21 +612,21 @@ impl FromWithTcx<clean::Enum> for Enum {
593
612
Enum {
594
613
generics : generics. into_tcx ( tcx) ,
595
614
variants_stripped,
596
- variants : ids ( variants) ,
615
+ variants : ids ( variants, tcx ) ,
597
616
impls : Vec :: new ( ) , // Added in JsonRenderer::item
598
617
}
599
618
}
600
619
}
601
620
602
621
impl FromWithTcx < clean:: VariantStruct > for Struct {
603
- fn from_tcx ( struct_ : clean:: VariantStruct , _tcx : TyCtxt < ' _ > ) -> Self {
622
+ fn from_tcx ( struct_ : clean:: VariantStruct , tcx : TyCtxt < ' _ > ) -> Self {
604
623
let fields_stripped = struct_. has_stripped_entries ( ) ;
605
624
let clean:: VariantStruct { struct_type, fields } = struct_;
606
625
Struct {
607
626
struct_type : from_ctor_kind ( struct_type) ,
608
627
generics : Default :: default ( ) ,
609
628
fields_stripped,
610
- fields : ids ( fields) ,
629
+ fields : ids ( fields, tcx ) ,
611
630
impls : Vec :: new ( ) ,
612
631
}
613
632
}
@@ -630,25 +649,25 @@ impl FromWithTcx<clean::Variant> for Variant {
630
649
} )
631
650
. collect ( ) ,
632
651
) ,
633
- Struct ( s) => Variant :: Struct ( ids ( s. fields ) ) ,
652
+ Struct ( s) => Variant :: Struct ( ids ( s. fields , tcx ) ) ,
634
653
}
635
654
}
636
655
}
637
656
638
657
impl FromWithTcx < clean:: Import > for Import {
639
- fn from_tcx ( import : clean:: Import , _tcx : TyCtxt < ' _ > ) -> Self {
658
+ fn from_tcx ( import : clean:: Import , tcx : TyCtxt < ' _ > ) -> Self {
640
659
use clean:: ImportKind :: * ;
641
660
match import. kind {
642
661
Simple ( s) => Import {
643
662
source : import. source . path . whole_name ( ) ,
644
663
name : s. to_string ( ) ,
645
- id : import. source . did . map ( ItemId :: from) . map ( from_item_id) ,
664
+ id : import. source . did . map ( ItemId :: from) . map ( |i| from_item_id ( i , tcx ) ) ,
646
665
glob : false ,
647
666
} ,
648
667
Glob => Import {
649
668
source : import. source . path . whole_name ( ) ,
650
669
name : import. source . path . last ( ) . to_string ( ) ,
651
- id : import. source . did . map ( ItemId :: from) . map ( from_item_id) ,
670
+ id : import. source . did . map ( ItemId :: from) . map ( |i| from_item_id ( i , tcx ) ) ,
652
671
glob : true ,
653
672
} ,
654
673
}
@@ -742,6 +761,10 @@ impl FromWithTcx<ItemType> for ItemKind {
742
761
}
743
762
}
744
763
745
- fn ids ( items : impl IntoIterator < Item = clean:: Item > ) -> Vec < Id > {
746
- items. into_iter ( ) . filter ( |x| !x. is_stripped ( ) ) . map ( |i| from_item_id ( i. item_id ) ) . collect ( )
764
+ fn ids ( items : impl IntoIterator < Item = clean:: Item > , tcx : TyCtxt < ' _ > ) -> Vec < Id > {
765
+ items
766
+ . into_iter ( )
767
+ . filter ( |x| !x. is_stripped ( ) )
768
+ . map ( |i| from_item_id_with_name ( i. item_id , tcx, i. name ) )
769
+ . collect ( )
747
770
}
0 commit comments