@@ -3,7 +3,7 @@ use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt
3
3
use rustc_attr:: InlineAttr ;
4
4
use rustc_data_structures:: base_n;
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
- use rustc_data_structures:: fx:: FxHashMap ;
6
+ use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
7
7
use rustc_data_structures:: stable_hasher:: { Hash128 , HashStable , StableHasher } ;
8
8
use rustc_hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
9
9
use rustc_hir:: ItemId ;
@@ -230,7 +230,7 @@ pub struct CodegenUnit<'tcx> {
230
230
/// contain something unique to this crate (e.g., a module path)
231
231
/// as well as the crate name and disambiguator.
232
232
name : Symbol ,
233
- items : FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > ,
233
+ items : FxIndexMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > ,
234
234
size_estimate : Option < usize > ,
235
235
primary : bool ,
236
236
/// True if this is CGU is used to hold code coverage information for dead code,
@@ -291,11 +291,11 @@ impl<'tcx> CodegenUnit<'tcx> {
291
291
self . primary = true ;
292
292
}
293
293
294
- pub fn items ( & self ) -> & FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
294
+ pub fn items ( & self ) -> & FxIndexMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
295
295
& self . items
296
296
}
297
297
298
- pub fn items_mut ( & mut self ) -> & mut FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
298
+ pub fn items_mut ( & mut self ) -> & mut FxIndexMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
299
299
& mut self . items
300
300
}
301
301
@@ -364,6 +364,47 @@ impl<'tcx> CodegenUnit<'tcx> {
364
364
. unwrap_or_else ( || panic ! ( "Could not find work-product for CGU `{}`" , self . name( ) ) )
365
365
}
366
366
367
+ // njn: dups code in items_in_deterministic_order
368
+ pub fn sort_items ( & mut self , tcx : TyCtxt < ' tcx > ) {
369
+ // The codegen tests rely on items being process in the same order as
370
+ // they appear in the file, so for local items, we sort by node_id first
371
+ #[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
372
+ pub struct ItemSortKey < ' tcx > ( Option < usize > , SymbolName < ' tcx > ) ;
373
+
374
+ fn item_sort_key < ' tcx > ( tcx : TyCtxt < ' tcx > , item : MonoItem < ' tcx > ) -> ItemSortKey < ' tcx > {
375
+ ItemSortKey (
376
+ match item {
377
+ MonoItem :: Fn ( ref instance) => {
378
+ match instance. def {
379
+ // We only want to take HirIds of user-defined
380
+ // instances into account. The others don't matter for
381
+ // the codegen tests and can even make item order
382
+ // unstable.
383
+ InstanceDef :: Item ( def) => def. as_local ( ) . map ( Idx :: index) ,
384
+ InstanceDef :: VTableShim ( ..)
385
+ | InstanceDef :: ReifyShim ( ..)
386
+ | InstanceDef :: Intrinsic ( ..)
387
+ | InstanceDef :: FnPtrShim ( ..)
388
+ | InstanceDef :: Virtual ( ..)
389
+ | InstanceDef :: ClosureOnceShim { .. }
390
+ | InstanceDef :: DropGlue ( ..)
391
+ | InstanceDef :: CloneShim ( ..)
392
+ | InstanceDef :: ThreadLocalShim ( ..)
393
+ | InstanceDef :: FnPtrAddrShim ( ..) => None ,
394
+ }
395
+ }
396
+ MonoItem :: Static ( def_id) => def_id. as_local ( ) . map ( Idx :: index) ,
397
+ MonoItem :: GlobalAsm ( item_id) => Some ( item_id. owner_id . def_id . index ( ) ) ,
398
+ } ,
399
+ item. symbol_name ( tcx) ,
400
+ )
401
+ }
402
+
403
+ self . items_mut ( ) . sort_by ( |& i1, _, & i2, _| {
404
+ std:: cmp:: Ord :: cmp ( & item_sort_key ( tcx, i1) , & item_sort_key ( tcx, i2) )
405
+ } ) ;
406
+ }
407
+
367
408
pub fn items_in_deterministic_order (
368
409
& self ,
369
410
tcx : TyCtxt < ' tcx > ,
0 commit comments