diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 38dbf8d428a20..1299e8d9f3326 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -10,7 +10,7 @@ use std::sync::{Arc, Mutex}; use rustc_attr_ir::{AttributeKind, EncodeCrossCrate, find_attr}; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; +use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet}; use rustc_data_structures::memmap::{Mmap, MmapMut}; use rustc_data_structures::owned_slice::slice_owned; use rustc_data_structures::stable_hash::{StableHash, StableHasher}; @@ -18,7 +18,7 @@ use rustc_data_structures::sync::{par_for_each_in, par_join}; use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_data_structures::thousands::usize_with_underscores; use rustc_hir as hir; -use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalDefIdSet}; +use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdSet}; use rustc_hir::definitions::DefPathData; use rustc_hir_pretty::id_to_string; use rustc_index::IndexVec; @@ -425,6 +425,18 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { index.encode(self); } + + fn encode_def_id_map>(&mut self, map: &FxHashMap) { + #[allow(rustc::potential_query_instability)] + let mut entries: Vec<(DefId, &V)> = map.iter().map(|(&k, v)| (k, v)).collect(); + entries.sort_unstable_by_key(|&(def_id, _)| self.tcx.def_path_hash(def_id)); + + self.emit_usize(entries.len()); + for (def_id, value) in entries { + def_id.encode(self); + value.encode(self); + } + } } // Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy($value))`, which would diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 7383ad23273be..7cf7ef619224b 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -937,6 +937,11 @@ impl<'tcx> TyEncoder<'tcx> for CacheEncoder<'tcx> { index.encode(self); } + + #[inline] + fn encode_def_id_map>(&mut self, map: &FxHashMap) { + map.encode(self); + } } macro_rules! encoder_methods { diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 81462e214b20f..6e121565c8c0b 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -12,6 +12,7 @@ use std::marker::DiscriminantKind; use rustc_data_structures::fx::FxHashMap; use rustc_serialize::{Decodable, Encodable}; +use rustc_span::def_id::DefId; use rustc_span::{SpanDecoder, SpanEncoder}; pub use self::ref_decodable::RefDecodable; @@ -37,6 +38,10 @@ pub trait TyEncoder<'tcx>: SpanEncoder { fn predicate_shorthands(&mut self) -> &mut FxHashMap, usize>; fn encode_alloc_id(&mut self, alloc_id: &AllocId); + + fn encode_def_id_map>(&mut self, map: &FxHashMap) + where + Self: Sized; } pub trait TyDecoder<'tcx>: diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 353ac74bd576f..fdef1c6450957 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -1,14 +1,16 @@ -use std::ops::ControlFlow; +use std::ops::{ControlFlow, Deref, DerefMut}; use rustc_ast as ast; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; use rustc_macros::{StableHash, TyDecodable, TyEncodable}; +use rustc_serialize::{Decodable, Encodable}; use rustc_span::{Span, Symbol, bug, kw}; use rustc_type_ir::{TypeSuperVisitable as _, TypeVisitable, TypeVisitor}; use tracing::instrument; use super::{Clause, InstantiatedClauses, ParamConst, ParamTy, Ty, TyCtxt, Unnormalized}; +use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::{self, ClauseKind, EarlyBinder, GenericArgsRef, Region, RegionKind, TyKind}; #[derive(Clone, Debug, TyEncodable, TyDecodable, StableHash)] @@ -124,7 +126,7 @@ pub struct Generics { /// Reverse map to the `index` field of each `GenericParamDef`. #[stable_hash(ignore)] - pub param_def_id_to_index: FxHashMap, + pub param_def_id_to_index: ParamDefIdToIndex, pub has_self: bool, pub has_late_bound_regions: Option, @@ -621,3 +623,37 @@ impl<'tcx> ConstConditions<'tcx> { ); } } + +#[derive(Clone, Default)] +pub struct ParamDefIdToIndex(FxHashMap); + +impl Deref for ParamDefIdToIndex { + type Target = FxHashMap; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ParamDefIdToIndex { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl FromIterator<(DefId, u32)> for ParamDefIdToIndex { + fn from_iter>(iter: I) -> Self { + Self(iter.into_iter().collect()) + } +} + +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ParamDefIdToIndex { + fn encode(&self, e: &mut E) { + e.encode_def_id_map(&self.0); + } +} + +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ParamDefIdToIndex { + fn decode(d: &mut D) -> Self { + Self(Decodable::decode(d)) + } +} diff --git a/compiler/rustc_monomorphize/src/offload/manifest.rs b/compiler/rustc_monomorphize/src/offload/manifest.rs index a6f5d1228082e..d4e1579e7d83e 100644 --- a/compiler/rustc_monomorphize/src/offload/manifest.rs +++ b/compiler/rustc_monomorphize/src/offload/manifest.rs @@ -149,6 +149,10 @@ impl<'a, 'tcx> TyEncoder<'tcx> for OffloadManifestEncoder<'a, 'tcx> { fn encode_alloc_id(&mut self, _alloc_id: &rustc_middle::mir::interpret::AllocId) { // AllocIds are not expected in the manifest. } + + fn encode_def_id_map>(&mut self, map: &FxHashMap) { + map.encode(self); + } } const UNRESOLVED_DEF_ID: DefId = DefId { diff --git a/tests/run-make/parallel-reproducible-build/generics-of-issue-163878.rs b/tests/run-make/parallel-reproducible-build/generics-of-issue-163878.rs new file mode 100644 index 0000000000000..df64b1b2b71f0 --- /dev/null +++ b/tests/run-make/parallel-reproducible-build/generics-of-issue-163878.rs @@ -0,0 +1,10 @@ +pub struct A {} + +impl A { + pub fn f1(&self, _b1: &()) -> impl Iterator + '_ { + std::iter::empty() + } + pub fn f2(&self, _b2: &()) -> impl Iterator + '_ { + std::iter::empty() + } +} diff --git a/tests/run-make/parallel-reproducible-build/rmake.rs b/tests/run-make/parallel-reproducible-build/rmake.rs index caaee1b47c583..b40d93a1a1c86 100644 --- a/tests/run-make/parallel-reproducible-build/rmake.rs +++ b/tests/run-make/parallel-reproducible-build/rmake.rs @@ -14,6 +14,17 @@ fn main() { const TESTS: &[(&str, &[&str])] = &[ ("static-muts-issue-140413", &["-Zthreads=50"]), ("derives-issue-129094", &["-Zthreads=16", "-Copt-level=3"]), + ( + "generics-of-issue-163878", + &[ + "-Zthreads=30", + "--crate-type=lib", + "--crate-name=test", + "-Ccodegen-units=1", + "-Zremap-cwd-prefix=reproducible_dir", + "--edition=2024", + ], + ), ]; for (file, args) in TESTS {