@@ -12,7 +12,6 @@ use rustc_hashes::Hash128;
12
12
use rustc_hir:: ItemId ;
13
13
use rustc_hir:: attrs:: InlineAttr ;
14
14
use rustc_hir:: def_id:: { CrateNum , DefId , DefIdSet , LOCAL_CRATE } ;
15
- use rustc_index:: Idx ;
16
15
use rustc_macros:: { HashStable , TyDecodable , TyEncodable } ;
17
16
use rustc_query_system:: ich:: StableHashingContext ;
18
17
use rustc_session:: config:: OptLevel ;
@@ -526,44 +525,50 @@ impl<'tcx> CodegenUnit<'tcx> {
526
525
tcx : TyCtxt < ' tcx > ,
527
526
) -> Vec < ( MonoItem < ' tcx > , MonoItemData ) > {
528
527
// The codegen tests rely on items being process in the same order as
529
- // they appear in the file, so for local items, we sort by node_id first
528
+ // they appear in the file, so for local items, we sort by span first
530
529
#[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
531
- struct ItemSortKey < ' tcx > ( Option < usize > , SymbolName < ' tcx > ) ;
532
-
530
+ struct ItemSortKey < ' tcx > ( Option < Span > , SymbolName < ' tcx > ) ;
531
+
532
+ // We only want to take HirIds of user-defines instances into account.
533
+ // The others don't matter for the codegen tests and can even make item
534
+ // order unstable.
535
+ fn local_item_id < ' tcx > ( item : MonoItem < ' tcx > ) -> Option < DefId > {
536
+ match item {
537
+ MonoItem :: Fn ( ref instance) => match instance. def {
538
+ InstanceKind :: Item ( def) => def. as_local ( ) . map ( |_| def) ,
539
+ InstanceKind :: VTableShim ( ..)
540
+ | InstanceKind :: ReifyShim ( ..)
541
+ | InstanceKind :: Intrinsic ( ..)
542
+ | InstanceKind :: FnPtrShim ( ..)
543
+ | InstanceKind :: Virtual ( ..)
544
+ | InstanceKind :: ClosureOnceShim { .. }
545
+ | InstanceKind :: ConstructCoroutineInClosureShim { .. }
546
+ | InstanceKind :: DropGlue ( ..)
547
+ | InstanceKind :: CloneShim ( ..)
548
+ | InstanceKind :: ThreadLocalShim ( ..)
549
+ | InstanceKind :: FnPtrAddrShim ( ..)
550
+ | InstanceKind :: AsyncDropGlue ( ..)
551
+ | InstanceKind :: FutureDropPollShim ( ..)
552
+ | InstanceKind :: AsyncDropGlueCtorShim ( ..) => None ,
553
+ } ,
554
+ MonoItem :: Static ( def_id) => def_id. as_local ( ) . map ( |_| def_id) ,
555
+ MonoItem :: GlobalAsm ( item_id) => Some ( item_id. owner_id . def_id . to_def_id ( ) ) ,
556
+ }
557
+ }
533
558
fn item_sort_key < ' tcx > ( tcx : TyCtxt < ' tcx > , item : MonoItem < ' tcx > ) -> ItemSortKey < ' tcx > {
534
559
ItemSortKey (
535
- match item {
536
- MonoItem :: Fn ( ref instance) => {
537
- match instance. def {
538
- // We only want to take HirIds of user-defined
539
- // instances into account. The others don't matter for
540
- // the codegen tests and can even make item order
541
- // unstable.
542
- InstanceKind :: Item ( def) => def. as_local ( ) . map ( Idx :: index) ,
543
- InstanceKind :: VTableShim ( ..)
544
- | InstanceKind :: ReifyShim ( ..)
545
- | InstanceKind :: Intrinsic ( ..)
546
- | InstanceKind :: FnPtrShim ( ..)
547
- | InstanceKind :: Virtual ( ..)
548
- | InstanceKind :: ClosureOnceShim { .. }
549
- | InstanceKind :: ConstructCoroutineInClosureShim { .. }
550
- | InstanceKind :: DropGlue ( ..)
551
- | InstanceKind :: CloneShim ( ..)
552
- | InstanceKind :: ThreadLocalShim ( ..)
553
- | InstanceKind :: FnPtrAddrShim ( ..)
554
- | InstanceKind :: AsyncDropGlue ( ..)
555
- | InstanceKind :: FutureDropPollShim ( ..)
556
- | InstanceKind :: AsyncDropGlueCtorShim ( ..) => None ,
557
- }
558
- }
559
- MonoItem :: Static ( def_id) => def_id. as_local ( ) . map ( Idx :: index) ,
560
- MonoItem :: GlobalAsm ( item_id) => Some ( item_id. owner_id . def_id . index ( ) ) ,
561
- } ,
560
+ local_item_id ( item)
561
+ . map ( |def_id| tcx. def_span ( def_id) . find_ancestor_not_from_macro ( ) )
562
+ . flatten ( ) ,
562
563
item. symbol_name ( tcx) ,
563
564
)
564
565
}
565
566
566
567
let mut items: Vec < _ > = self . items ( ) . iter ( ) . map ( |( & i, & data) | ( i, data) ) . collect ( ) ;
568
+ if !tcx. sess . opts . unstable_opts . codegen_source_order {
569
+ // It's already deterministic, so we can just use it.
570
+ return items;
571
+ }
567
572
items. sort_by_cached_key ( |& ( i, _) | item_sort_key ( tcx, i) ) ;
568
573
items
569
574
}
0 commit comments