Skip to content

Commit 69a6d12

Browse files
committed
Auto merge of #96367 - Mark-Simulacrum:beta-next, r=Mark-Simulacrum
[beta] backports rollup * Remove NodeIdHashingMode. #95656 * Check that all hidden types are the same and then deduplicate them. #95731 r? `@Mark-Simulacrum`
2 parents 2431a97 + d76101d commit 69a6d12

File tree

23 files changed

+196
-454
lines changed

23 files changed

+196
-454
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+80-62
Original file line numberDiff line numberDiff line change
@@ -55,75 +55,93 @@ impl<'tcx> RegionInferenceContext<'tcx> {
5555
infcx: &InferCtxt<'_, 'tcx>,
5656
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
5757
) -> VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>> {
58-
opaque_ty_decls
59-
.into_iter()
60-
.map(|(opaque_type_key, (concrete_type, origin))| {
61-
let substs = opaque_type_key.substs;
62-
debug!(?concrete_type, ?substs);
58+
let mut result: VecMap<OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>> = VecMap::new();
59+
for (opaque_type_key, (concrete_type, origin)) in opaque_ty_decls {
60+
let substs = opaque_type_key.substs;
61+
debug!(?concrete_type, ?substs);
6362

64-
let mut subst_regions = vec![self.universal_regions.fr_static];
65-
let universal_substs = infcx.tcx.fold_regions(substs, &mut false, |region, _| {
66-
if let ty::RePlaceholder(..) = region.kind() {
67-
// Higher kinded regions don't need remapping, they don't refer to anything outside of this the substs.
68-
return region;
63+
let mut subst_regions = vec![self.universal_regions.fr_static];
64+
let universal_substs = infcx.tcx.fold_regions(substs, &mut false, |region, _| {
65+
if let ty::RePlaceholder(..) = region.kind() {
66+
// Higher kinded regions don't need remapping, they don't refer to anything outside of this the substs.
67+
return region;
68+
}
69+
let vid = self.to_region_vid(region);
70+
trace!(?vid);
71+
let scc = self.constraint_sccs.scc(vid);
72+
trace!(?scc);
73+
match self.scc_values.universal_regions_outlived_by(scc).find_map(|lb| {
74+
self.eval_equal(vid, lb).then_some(self.definitions[lb].external_name?)
75+
}) {
76+
Some(region) => {
77+
let vid = self.universal_regions.to_region_vid(region);
78+
subst_regions.push(vid);
79+
region
6980
}
70-
let vid = self.to_region_vid(region);
71-
trace!(?vid);
72-
let scc = self.constraint_sccs.scc(vid);
73-
trace!(?scc);
74-
match self.scc_values.universal_regions_outlived_by(scc).find_map(|lb| {
75-
self.eval_equal(vid, lb).then_some(self.definitions[lb].external_name?)
76-
}) {
77-
Some(region) => {
78-
let vid = self.universal_regions.to_region_vid(region);
79-
subst_regions.push(vid);
80-
region
81-
}
82-
None => {
83-
subst_regions.push(vid);
84-
infcx.tcx.sess.delay_span_bug(
85-
concrete_type.span,
86-
"opaque type with non-universal region substs",
87-
);
88-
infcx.tcx.lifetimes.re_static
89-
}
81+
None => {
82+
subst_regions.push(vid);
83+
infcx.tcx.sess.delay_span_bug(
84+
concrete_type.span,
85+
"opaque type with non-universal region substs",
86+
);
87+
infcx.tcx.lifetimes.re_static
9088
}
91-
});
89+
}
90+
});
9291

93-
subst_regions.sort();
94-
subst_regions.dedup();
92+
subst_regions.sort();
93+
subst_regions.dedup();
9594

96-
let universal_concrete_type =
97-
infcx.tcx.fold_regions(concrete_type, &mut false, |region, _| match *region {
98-
ty::ReVar(vid) => subst_regions
99-
.iter()
100-
.find(|ur_vid| self.eval_equal(vid, **ur_vid))
101-
.and_then(|ur_vid| self.definitions[*ur_vid].external_name)
102-
.unwrap_or(infcx.tcx.lifetimes.re_root_empty),
103-
_ => region,
104-
});
95+
let universal_concrete_type =
96+
infcx.tcx.fold_regions(concrete_type, &mut false, |region, _| match *region {
97+
ty::ReVar(vid) => subst_regions
98+
.iter()
99+
.find(|ur_vid| self.eval_equal(vid, **ur_vid))
100+
.and_then(|ur_vid| self.definitions[*ur_vid].external_name)
101+
.unwrap_or(infcx.tcx.lifetimes.re_root_empty),
102+
_ => region,
103+
});
105104

106-
debug!(?universal_concrete_type, ?universal_substs);
105+
debug!(?universal_concrete_type, ?universal_substs);
107106

108-
let opaque_type_key =
109-
OpaqueTypeKey { def_id: opaque_type_key.def_id, substs: universal_substs };
110-
let remapped_type = infcx.infer_opaque_definition_from_instantiation(
111-
opaque_type_key,
112-
universal_concrete_type,
113-
);
114-
let ty = if check_opaque_type_parameter_valid(
115-
infcx.tcx,
116-
opaque_type_key,
117-
origin,
118-
concrete_type.span,
119-
) {
120-
remapped_type
121-
} else {
122-
infcx.tcx.ty_error()
123-
};
124-
(opaque_type_key, OpaqueHiddenType { ty, span: concrete_type.span })
125-
})
126-
.collect()
107+
let opaque_type_key =
108+
OpaqueTypeKey { def_id: opaque_type_key.def_id, substs: universal_substs };
109+
let remapped_type = infcx.infer_opaque_definition_from_instantiation(
110+
opaque_type_key,
111+
universal_concrete_type,
112+
);
113+
let ty = if check_opaque_type_parameter_valid(
114+
infcx.tcx,
115+
opaque_type_key,
116+
origin,
117+
concrete_type.span,
118+
) {
119+
remapped_type
120+
} else {
121+
infcx.tcx.ty_error()
122+
};
123+
// Sometimes two opaque types are the same only after we remap the generic parameters
124+
// back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to `(X, Y)`
125+
// and `OpaqueType<Y, X>` mapped to `(Y, X)`, and those are the same, but we only know that
126+
// once we convert the generic parameters to those of the opaque type.
127+
if let Some(prev) = result.get_mut(&opaque_type_key) {
128+
if prev.ty != ty {
129+
let mut err = infcx.tcx.sess.struct_span_err(
130+
concrete_type.span,
131+
&format!("hidden type `{}` differed from previous `{}`", ty, prev.ty),
132+
);
133+
err.span_note(prev.span, "previous hidden type bound here");
134+
err.emit();
135+
prev.ty = infcx.tcx.ty_error();
136+
}
137+
// Pick a better span if there is one.
138+
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
139+
prev.span = prev.span.substitute_dummy(concrete_type.span);
140+
} else {
141+
result.insert(opaque_type_key, OpaqueHiddenType { ty, span: concrete_type.span });
142+
}
143+
}
144+
result
127145
}
128146

129147
/// Map the regions in the type to named regions. This is similar to what

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cell::RefCell;
33
use rustc_data_structures::{
44
fingerprint::Fingerprint,
55
fx::FxHashMap,
6-
stable_hasher::{HashStable, NodeIdHashingMode, StableHasher},
6+
stable_hasher::{HashStable, StableHasher},
77
};
88
use rustc_middle::{
99
bug,
@@ -94,11 +94,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
9494
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
9595
let mut hasher = StableHasher::new();
9696
let mut hcx = tcx.create_stable_hashing_context();
97-
hcx.while_hashing_spans(false, |hcx| {
98-
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
99-
self.hash_stable(hcx, &mut hasher);
100-
});
101-
});
97+
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher));
10298
hasher.finish::<Fingerprint>().to_hex()
10399
}
104100

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Mutability};
1919
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
2020
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
2121
use rustc_middle::ty::{self, ExistentialProjection, GeneratorSubsts, ParamEnv, Ty, TyCtxt};
22-
use rustc_query_system::ich::NodeIdHashingMode;
2322
use rustc_target::abi::{Integer, TagEncoding, Variants};
2423
use smallvec::SmallVec;
2524

@@ -704,11 +703,7 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
704703
// but we get a deterministic, virtually unique value for the constant.
705704
let hcx = &mut tcx.create_stable_hashing_context();
706705
let mut hasher = StableHasher::new();
707-
hcx.while_hashing_spans(false, |hcx| {
708-
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
709-
ct.val().hash_stable(hcx, &mut hasher);
710-
});
711-
});
706+
hcx.while_hashing_spans(false, |hcx| ct.val().hash_stable(hcx, &mut hasher));
712707
// Let's only emit 64 bits of the hash value. That should be plenty for
713708
// avoiding collisions and will make the emitted type names shorter.
714709
let hash: u64 = hasher.finish();

compiler/rustc_data_structures/src/stable_hasher.rs

-7
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,6 @@ fn stable_hash_reduce<HCX, I, C, F>(
612612
}
613613
}
614614

615-
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
616-
pub enum NodeIdHashingMode {
617-
Ignore,
618-
HashDefPath,
619-
}
620-
621615
/// Controls what data we do or not not hash.
622616
/// Whenever a `HashStable` implementation caches its
623617
/// result, it needs to include `HashingControls` as part
@@ -628,5 +622,4 @@ pub enum NodeIdHashingMode {
628622
#[derive(Clone, Hash, Eq, PartialEq, Debug)]
629623
pub struct HashingControls {
630624
pub hash_spans: bool,
631-
pub node_id_hashing_mode: NodeIdHashingMode,
632625
}

compiler/rustc_hir/src/hir.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ pub struct BodyId {
12641264
///
12651265
/// All bodies have an **owner**, which can be accessed via the HIR
12661266
/// map using `body_owner_def_id()`.
1267-
#[derive(Debug)]
1267+
#[derive(Debug, HashStable_Generic)]
12681268
pub struct Body<'hir> {
12691269
pub params: &'hir [Param<'hir>],
12701270
pub value: Expr<'hir>,
@@ -2024,7 +2024,7 @@ pub struct FnSig<'hir> {
20242024
// The bodies for items are stored "out of line", in a separate
20252025
// hashmap in the `Crate`. Here we just record the hir-id of the item
20262026
// so it can fetched later.
2027-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
2027+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
20282028
pub struct TraitItemId {
20292029
pub def_id: LocalDefId,
20302030
}
@@ -2041,7 +2041,7 @@ impl TraitItemId {
20412041
/// possibly including a default implementation. A trait item is
20422042
/// either required (meaning it doesn't have an implementation, just a
20432043
/// signature) or provided (meaning it has a default implementation).
2044-
#[derive(Debug)]
2044+
#[derive(Debug, HashStable_Generic)]
20452045
pub struct TraitItem<'hir> {
20462046
pub ident: Ident,
20472047
pub def_id: LocalDefId,
@@ -2087,7 +2087,7 @@ pub enum TraitItemKind<'hir> {
20872087
// The bodies for items are stored "out of line", in a separate
20882088
// hashmap in the `Crate`. Here we just record the hir-id of the item
20892089
// so it can fetched later.
2090-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
2090+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
20912091
pub struct ImplItemId {
20922092
pub def_id: LocalDefId,
20932093
}
@@ -2101,7 +2101,7 @@ impl ImplItemId {
21012101
}
21022102

21032103
/// Represents anything within an `impl` block.
2104-
#[derive(Debug)]
2104+
#[derive(Debug, HashStable_Generic)]
21052105
pub struct ImplItem<'hir> {
21062106
pub ident: Ident,
21072107
pub def_id: LocalDefId,
@@ -2602,7 +2602,7 @@ pub struct PolyTraitRef<'hir> {
26022602

26032603
pub type Visibility<'hir> = Spanned<VisibilityKind<'hir>>;
26042604

2605-
#[derive(Copy, Clone, Debug)]
2605+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
26062606
pub enum VisibilityKind<'hir> {
26072607
Public,
26082608
Crate(CrateSugar),
@@ -2678,7 +2678,7 @@ impl<'hir> VariantData<'hir> {
26782678
// The bodies for items are stored "out of line", in a separate
26792679
// hashmap in the `Crate`. Here we just record the hir-id of the item
26802680
// so it can fetched later.
2681-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, Hash)]
2681+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, Hash, HashStable_Generic)]
26822682
pub struct ItemId {
26832683
pub def_id: LocalDefId,
26842684
}
@@ -2694,7 +2694,7 @@ impl ItemId {
26942694
/// An item
26952695
///
26962696
/// The name might be a dummy name in case of anonymous items
2697-
#[derive(Debug)]
2697+
#[derive(Debug, HashStable_Generic)]
26982698
pub struct Item<'hir> {
26992699
pub ident: Ident,
27002700
pub def_id: LocalDefId,
@@ -2925,7 +2925,7 @@ pub enum AssocItemKind {
29252925
// The bodies for items are stored "out of line", in a separate
29262926
// hashmap in the `Crate`. Here we just record the hir-id of the item
29272927
// so it can fetched later.
2928-
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
2928+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
29292929
pub struct ForeignItemId {
29302930
pub def_id: LocalDefId,
29312931
}
@@ -2951,7 +2951,7 @@ pub struct ForeignItemRef {
29512951
pub span: Span,
29522952
}
29532953

2954-
#[derive(Debug)]
2954+
#[derive(Debug, HashStable_Generic)]
29552955
pub struct ForeignItem<'hir> {
29562956
pub ident: Ident,
29572957
pub kind: ForeignItemKind<'hir>,
@@ -2993,7 +2993,7 @@ pub struct Upvar {
29932993
// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
29942994
// has length > 0 if the trait is found through an chain of imports, starting with the
29952995
// import/use statement in the scope where the trait is used.
2996-
#[derive(Encodable, Decodable, Clone, Debug)]
2996+
#[derive(Encodable, Decodable, Clone, Debug, HashStable_Generic)]
29972997
pub struct TraitCandidate {
29982998
pub def_id: DefId,
29992999
pub import_ids: SmallVec<[LocalDefId; 1]>,

compiler/rustc_hir/src/hir_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt;
1212
/// incremental compilation where we have to persist things through changes to
1313
/// the code base.
1414
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
15-
#[derive(Encodable, Decodable)]
15+
#[derive(Encodable, Decodable, HashStable_Generic)]
1616
#[rustc_pass_by_value]
1717
pub struct HirId {
1818
pub owner: LocalDefId,

0 commit comments

Comments
 (0)