From e70db80076766b7020df4e247cb59e38e466d666 Mon Sep 17 00:00:00 2001 From: ywxt Date: Fri, 25 Jul 2025 17:24:14 +0800 Subject: [PATCH 1/4] Fix async closures not reproducible --- compiler/rustc_middle/src/mir/mono.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index 105736b9e243e..ae256e74c0441 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -12,7 +12,6 @@ use rustc_data_structures::unord::UnordMap; use rustc_hashes::Hash128; use rustc_hir::ItemId; use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE}; -use rustc_index::Idx; use rustc_macros::{HashStable, TyDecodable, TyEncodable}; use rustc_query_system::ich::StableHashingContext; use rustc_session::config::OptLevel; @@ -527,8 +526,8 @@ impl<'tcx> CodegenUnit<'tcx> { ) -> Vec<(MonoItem<'tcx>, MonoItemData)> { // The codegen tests rely on items being process in the same order as // they appear in the file, so for local items, we sort by node_id first - #[derive(PartialEq, Eq, PartialOrd, Ord)] - struct ItemSortKey<'tcx>(Option, SymbolName<'tcx>); + #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] + struct ItemSortKey<'tcx>(Option, SymbolName<'tcx>); fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> { ItemSortKey( @@ -539,7 +538,9 @@ impl<'tcx> CodegenUnit<'tcx> { // instances into account. The others don't matter for // the codegen tests and can even make item order // unstable. - InstanceKind::Item(def) => def.as_local().map(Idx::index), + InstanceKind::Item(def) => { + def.as_local().map(|def_id|tcx.def_span(def_id)) + }, InstanceKind::VTableShim(..) | InstanceKind::ReifyShim(..) | InstanceKind::Intrinsic(..) @@ -556,8 +557,8 @@ impl<'tcx> CodegenUnit<'tcx> { | InstanceKind::AsyncDropGlueCtorShim(..) => None, } } - MonoItem::Static(def_id) => def_id.as_local().map(Idx::index), - MonoItem::GlobalAsm(item_id) => Some(item_id.owner_id.def_id.index()), + MonoItem::Static(def_id) => def_id.as_local().map(|def_id|tcx.def_span(def_id)), + MonoItem::GlobalAsm(item_id) => Some(tcx.def_span(item_id.owner_id.def_id)), }, item.symbol_name(tcx), ) From e96bd14aec37326cb4a13ac8bc866b4c28228c7a Mon Sep 17 00:00:00 2001 From: ywxt Date: Fri, 25 Jul 2025 17:27:15 +0800 Subject: [PATCH 2/4] Fix fmt --- compiler/rustc_middle/src/mir/mono.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index ae256e74c0441..18a8a7d1d3350 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -539,8 +539,8 @@ impl<'tcx> CodegenUnit<'tcx> { // the codegen tests and can even make item order // unstable. InstanceKind::Item(def) => { - def.as_local().map(|def_id|tcx.def_span(def_id)) - }, + def.as_local().map(|def_id| tcx.def_span(def_id)) + } InstanceKind::VTableShim(..) | InstanceKind::ReifyShim(..) | InstanceKind::Intrinsic(..) @@ -557,7 +557,9 @@ impl<'tcx> CodegenUnit<'tcx> { | InstanceKind::AsyncDropGlueCtorShim(..) => None, } } - MonoItem::Static(def_id) => def_id.as_local().map(|def_id|tcx.def_span(def_id)), + MonoItem::Static(def_id) => { + def_id.as_local().map(|def_id| tcx.def_span(def_id)) + } MonoItem::GlobalAsm(item_id) => Some(tcx.def_span(item_id.owner_id.def_id)), }, item.symbol_name(tcx), From 0ac0b304a5e1c4be60ee8218162512d422042b32 Mon Sep 17 00:00:00 2001 From: ywxt Date: Sat, 26 Jul 2025 14:21:29 +0800 Subject: [PATCH 3/4] Use the local_span method. --- compiler/rustc_middle/src/mir/mono.rs | 37 ++------------------------- 1 file changed, 2 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index 18a8a7d1d3350..f60d1cba34c73 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -525,45 +525,12 @@ impl<'tcx> CodegenUnit<'tcx> { tcx: TyCtxt<'tcx>, ) -> Vec<(MonoItem<'tcx>, MonoItemData)> { // The codegen tests rely on items being process in the same order as - // they appear in the file, so for local items, we sort by node_id first + // they appear in the file, so for local items, we sort by span first #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] struct ItemSortKey<'tcx>(Option, SymbolName<'tcx>); fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> { - ItemSortKey( - match item { - MonoItem::Fn(ref instance) => { - match instance.def { - // We only want to take HirIds of user-defined - // instances into account. The others don't matter for - // the codegen tests and can even make item order - // unstable. - InstanceKind::Item(def) => { - def.as_local().map(|def_id| tcx.def_span(def_id)) - } - InstanceKind::VTableShim(..) - | InstanceKind::ReifyShim(..) - | InstanceKind::Intrinsic(..) - | InstanceKind::FnPtrShim(..) - | InstanceKind::Virtual(..) - | InstanceKind::ClosureOnceShim { .. } - | InstanceKind::ConstructCoroutineInClosureShim { .. } - | InstanceKind::DropGlue(..) - | InstanceKind::CloneShim(..) - | InstanceKind::ThreadLocalShim(..) - | InstanceKind::FnPtrAddrShim(..) - | InstanceKind::AsyncDropGlue(..) - | InstanceKind::FutureDropPollShim(..) - | InstanceKind::AsyncDropGlueCtorShim(..) => None, - } - } - MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.def_span(def_id)) - } - MonoItem::GlobalAsm(item_id) => Some(tcx.def_span(item_id.owner_id.def_id)), - }, - item.symbol_name(tcx), - ) + ItemSortKey(item.local_span(tcx), item.symbol_name(tcx)) } let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect(); From 0ea30f5450c0a2fc34e5010195c6762995bfd413 Mon Sep 17 00:00:00 2001 From: ywxt Date: Sat, 26 Jul 2025 15:10:59 +0800 Subject: [PATCH 4/4] Remove Debug trait for ItemSortKey. --- compiler/rustc_middle/src/mir/mono.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index f60d1cba34c73..b62a3fb39f0bb 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -526,7 +526,7 @@ impl<'tcx> CodegenUnit<'tcx> { ) -> Vec<(MonoItem<'tcx>, MonoItemData)> { // The codegen tests rely on items being process in the same order as // they appear in the file, so for local items, we sort by span first - #[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] + #[derive(PartialEq, Eq, PartialOrd, Ord)] struct ItemSortKey<'tcx>(Option, SymbolName<'tcx>); fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {