Skip to content

Commit 73c3f55

Browse files
committed
Auto merge of #43743 - michaelwoerister:gcx-tcx-switcheroo, r=eddyb
Some assorted region hashing fixes. This PR contains three changes. 1. It changes what we implement `HashStable` for. Previously, the trait was implemented for things in the local `TyCtxt`. That was OK, since we only invoked hashing with a `TyCtxt<'_, 'tcx, 'tcx>` where there is no difference. With query result hashing this becomes a problem though. So we now implement `HashStable` for things in `'gcx`. 2. The PR makes the regular `HashStable` implementation *not* anonymize late-bound regions anymore. It's a waste of computing resources and it's not clear that it would always be correct to do so. 3. The PR adds an option for stable hashing to treat all regions as erased and uses this new option when computing the `TypeId`. This should help with #41875. I did not add a test case for (3) since that's not possible yet. But it looks like @zackmdavis has something in the pipeline there `:)`. r? @eddyb
2 parents 6c5212f + 6dbd846 commit 73c3f55

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

src/librustc/ich/impls_mir.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref });
3333
impl_stable_hash_for!(struct mir::BasicBlockData<'tcx> { statements, terminator, is_cleanup });
3434

3535
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
36-
for mir::Terminator<'tcx> {
36+
for mir::Terminator<'gcx> {
3737
#[inline]
3838
fn hash_stable<W: StableHasherResult>(&self,
3939
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
@@ -125,7 +125,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::P
125125
}
126126

127127
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
128-
for mir::TerminatorKind<'tcx> {
128+
for mir::TerminatorKind<'gcx> {
129129
fn hash_stable<W: StableHasherResult>(&self,
130130
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
131131
hasher: &mut StableHasher<W>) {
@@ -186,7 +186,7 @@ for mir::TerminatorKind<'tcx> {
186186
}
187187

188188
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
189-
for mir::AssertMessage<'tcx> {
189+
for mir::AssertMessage<'gcx> {
190190
fn hash_stable<W: StableHasherResult>(&self,
191191
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
192192
hasher: &mut StableHasher<W>) {
@@ -207,7 +207,7 @@ for mir::AssertMessage<'tcx> {
207207
impl_stable_hash_for!(struct mir::Statement<'tcx> { source_info, kind });
208208

209209
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
210-
for mir::StatementKind<'tcx> {
210+
for mir::StatementKind<'gcx> {
211211
fn hash_stable<W: StableHasherResult>(&self,
212212
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
213213
hasher: &mut StableHasher<W>) {
@@ -244,7 +244,7 @@ for mir::StatementKind<'tcx> {
244244
}
245245

246246
impl<'a, 'gcx, 'tcx, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
247-
for mir::ValidationOperand<'tcx, T>
247+
for mir::ValidationOperand<'gcx, T>
248248
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
249249
{
250250
fn hash_stable<W: StableHasherResult>(&self,
@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
260260

261261
impl_stable_hash_for!(enum mir::ValidationOp { Acquire, Release, Suspend(extent) });
262262

263-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Lvalue<'tcx> {
263+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Lvalue<'gcx> {
264264
fn hash_stable<W: StableHasherResult>(&self,
265265
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
266266
hasher: &mut StableHasher<W>) {
@@ -280,7 +280,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::L
280280
}
281281

282282
impl<'a, 'gcx, 'tcx, B, V, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
283-
for mir::Projection<'tcx, B, V, T>
283+
for mir::Projection<'gcx, B, V, T>
284284
where B: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
285285
V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
286286
T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
@@ -299,7 +299,7 @@ for mir::Projection<'tcx, B, V, T>
299299
}
300300

301301
impl<'a, 'gcx, 'tcx, V, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
302-
for mir::ProjectionElem<'tcx, V, T>
302+
for mir::ProjectionElem<'gcx, V, T>
303303
where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
304304
T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
305305
{
@@ -335,7 +335,7 @@ for mir::ProjectionElem<'tcx, V, T>
335335

336336
impl_stable_hash_for!(struct mir::VisibilityScopeData { span, parent_scope });
337337

338-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Operand<'tcx> {
338+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Operand<'gcx> {
339339
fn hash_stable<W: StableHasherResult>(&self,
340340
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
341341
hasher: &mut StableHasher<W>) {
@@ -352,7 +352,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::O
352352
}
353353
}
354354

355-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Rvalue<'tcx> {
355+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Rvalue<'gcx> {
356356
fn hash_stable<W: StableHasherResult>(&self,
357357
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
358358
hasher: &mut StableHasher<W>) {
@@ -413,7 +413,7 @@ impl_stable_hash_for!(enum mir::CastKind {
413413
});
414414

415415
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
416-
for mir::AggregateKind<'tcx> {
416+
for mir::AggregateKind<'gcx> {
417417
fn hash_stable<W: StableHasherResult>(&self,
418418
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
419419
hasher: &mut StableHasher<W>) {
@@ -469,7 +469,7 @@ impl_stable_hash_for!(enum mir::NullOp {
469469

470470
impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, literal });
471471

472-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Literal<'tcx> {
472+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for mir::Literal<'gcx> {
473473
fn hash_stable<W: StableHasherResult>(&self,
474474
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
475475
hasher: &mut StableHasher<W>) {

src/librustc/ich/impls_ty.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax_pos::symbol::InternedString;
2020
use ty;
2121

2222
impl<'a, 'gcx, 'tcx, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
23-
for &'tcx ty::Slice<T>
23+
for &'gcx ty::Slice<T>
2424
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> {
2525
fn hash_stable<W: StableHasherResult>(&self,
2626
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
@@ -30,7 +30,7 @@ for &'tcx ty::Slice<T>
3030
}
3131

3232
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
33-
for ty::subst::Kind<'tcx> {
33+
for ty::subst::Kind<'gcx> {
3434
fn hash_stable<W: StableHasherResult>(&self,
3535
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
3636
hasher: &mut StableHasher<W>) {
@@ -55,6 +55,11 @@ for ty::RegionKind {
5555
db.depth.hash_stable(hcx, hasher);
5656
i.hash_stable(hcx, hasher);
5757
}
58+
ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
59+
db.depth.hash_stable(hcx, hasher);
60+
def_id.hash_stable(hcx, hasher);
61+
name.hash_stable(hcx, hasher);
62+
}
5863
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
5964
def_id.hash_stable(hcx, hasher);
6065
index.hash_stable(hcx, hasher);
@@ -76,7 +81,7 @@ for ty::RegionKind {
7681
}
7782

7883
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
79-
for ty::adjustment::AutoBorrow<'tcx> {
84+
for ty::adjustment::AutoBorrow<'gcx> {
8085
fn hash_stable<W: StableHasherResult>(&self,
8186
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
8287
hasher: &mut StableHasher<W>) {
@@ -94,7 +99,7 @@ for ty::adjustment::AutoBorrow<'tcx> {
9499
}
95100

96101
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
97-
for ty::adjustment::Adjust<'tcx> {
102+
for ty::adjustment::Adjust<'gcx> {
98103
fn hash_stable<W: StableHasherResult>(&self,
99104
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
100105
hasher: &mut StableHasher<W>) {
@@ -128,7 +133,7 @@ impl_stable_hash_for!(enum ty::BorrowKind {
128133
});
129134

130135
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
131-
for ty::UpvarCapture<'tcx> {
136+
for ty::UpvarCapture<'gcx> {
132137
fn hash_stable<W: StableHasherResult>(&self,
133138
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
134139
hasher: &mut StableHasher<W>) {
@@ -150,12 +155,13 @@ impl_stable_hash_for!(struct ty::FnSig<'tcx> {
150155
});
151156

152157
impl<'a, 'gcx, 'tcx, T> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::Binder<T>
153-
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + ty::fold::TypeFoldable<'tcx>
158+
where T: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
154159
{
155160
fn hash_stable<W: StableHasherResult>(&self,
156161
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
157162
hasher: &mut StableHasher<W>) {
158-
hcx.tcx().anonymize_late_bound_regions(self).0.hash_stable(hcx, hasher);
163+
let ty::Binder(ref inner) = *self;
164+
inner.hash_stable(hcx, hasher);
159165
}
160166
}
161167

@@ -190,7 +196,7 @@ impl_stable_hash_for!(struct ty::ProjectionPredicate<'tcx> { projection_ty, ty }
190196
impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { substs, item_def_id });
191197

192198

193-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::Predicate<'tcx> {
199+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::Predicate<'gcx> {
194200
fn hash_stable<W: StableHasherResult>(&self,
195201
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
196202
hasher: &mut StableHasher<W>) {
@@ -256,7 +262,7 @@ impl_stable_hash_for!(struct ty::FieldDef {
256262
});
257263

258264
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
259-
for ::middle::const_val::ConstVal<'tcx> {
265+
for ::middle::const_val::ConstVal<'gcx> {
260266
fn hash_stable<W: StableHasherResult>(&self,
261267
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
262268
hasher: &mut StableHasher<W>) {
@@ -483,7 +489,7 @@ impl_stable_hash_for!(enum ty::BoundRegion {
483489
});
484490

485491
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
486-
for ty::TypeVariants<'tcx>
492+
for ty::TypeVariants<'gcx>
487493
{
488494
fn hash_stable<W: StableHasherResult>(&self,
489495
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
@@ -574,7 +580,7 @@ impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {
574580
});
575581

576582
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
577-
for ty::ExistentialPredicate<'tcx>
583+
for ty::ExistentialPredicate<'gcx>
578584
{
579585
fn hash_stable<W: StableHasherResult>(&self,
580586
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
@@ -607,7 +613,7 @@ impl_stable_hash_for!(struct ty::ExistentialProjection<'tcx> {
607613

608614

609615
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
610-
for ty::TypeckTables<'tcx> {
616+
for ty::TypeckTables<'gcx> {
611617
fn hash_stable<W: StableHasherResult>(&self,
612618
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
613619
hasher: &mut StableHasher<W>) {
@@ -687,7 +693,7 @@ impl_stable_hash_for!(struct ty::Instance<'tcx> {
687693
substs
688694
});
689695

690-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::InstanceDef<'tcx> {
696+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::InstanceDef<'gcx> {
691697
fn hash_stable<W: StableHasherResult>(&self,
692698
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
693699
hasher: &mut StableHasher<W>) {

src/librustc/macros.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ macro_rules! __impl_stable_hash_field {
7373
#[macro_export]
7474
macro_rules! impl_stable_hash_for {
7575
(enum $enum_name:path { $( $variant:ident $( ( $($arg:ident),* ) )* ),* }) => {
76-
impl<'a, 'gcx, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'gcx, 'tcx>> for $enum_name {
76+
impl<'a, 'tcx, 'lcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx, 'lcx>> for $enum_name {
7777
#[inline]
7878
fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
79-
__ctx: &mut $crate::ich::StableHashingContext<'a, 'gcx, 'tcx>,
79+
__ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx, 'lcx>,
8080
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
8181
use $enum_name::*;
8282
::std::mem::discriminant(self).hash_stable(__ctx, __hasher);
@@ -92,10 +92,10 @@ macro_rules! impl_stable_hash_for {
9292
}
9393
};
9494
(struct $struct_name:path { $($field:ident),* }) => {
95-
impl<'a, 'gcx, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'gcx, 'tcx>> for $struct_name {
95+
impl<'a, 'tcx, 'lcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx, 'lcx>> for $struct_name {
9696
#[inline]
9797
fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
98-
__ctx: &mut $crate::ich::StableHashingContext<'a, 'gcx, 'tcx>,
98+
__ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx, 'lcx>,
9999
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
100100
let $struct_name {
101101
$(ref $field),*
@@ -106,10 +106,10 @@ macro_rules! impl_stable_hash_for {
106106
}
107107
};
108108
(tuple_struct $struct_name:path { $($field:ident),* }) => {
109-
impl<'a, 'gcx, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'gcx, 'tcx>> for $struct_name {
109+
impl<'a, 'tcx, 'lcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a, 'tcx, 'lcx>> for $struct_name {
110110
#[inline]
111111
fn hash_stable<W: ::rustc_data_structures::stable_hasher::StableHasherResult>(&self,
112-
__ctx: &mut $crate::ich::StableHashingContext<'a, 'gcx, 'tcx>,
112+
__ctx: &mut $crate::ich::StableHashingContext<'a, 'tcx, 'lcx>,
113113
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<W>) {
114114
let $struct_name (
115115
$(ref $field),*
@@ -125,11 +125,11 @@ macro_rules! impl_stable_hash_for {
125125
macro_rules! impl_stable_hash_for_spanned {
126126
($T:path) => (
127127

128-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ::syntax::codemap::Spanned<$T>
128+
impl<'a, 'tcx, 'lcx> HashStable<StableHashingContext<'a, 'tcx, 'lcx>> for ::syntax::codemap::Spanned<$T>
129129
{
130130
#[inline]
131131
fn hash_stable<W: StableHasherResult>(&self,
132-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
132+
hcx: &mut StableHashingContext<'a, 'tcx, 'lcx>,
133133
hasher: &mut StableHasher<W>) {
134134
self.node.hash_stable(hcx, hasher);
135135
self.span.hash_stable(hcx, hasher);

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl<'tcx> TyS<'tcx> {
498498
}
499499
}
500500

501-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::TyS<'tcx> {
501+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::TyS<'gcx> {
502502
fn hash_stable<W: StableHasherResult>(&self,
503503
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
504504
hasher: &mut StableHasher<W>) {

src/librustc/ty/util.rs

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
214214
let mut hasher = StableHasher::new();
215215
let mut hcx = StableHashingContext::new(self);
216216

217+
// We want the type_id be independent of the types free regions, so we
218+
// erase them. The erase_regions() call will also anonymize bound
219+
// regions, which is desirable too.
220+
let ty = self.erase_regions(&ty);
221+
217222
hcx.while_hashing_spans(false, |hcx| {
218223
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
219224
ty.hash_stable(hcx, &mut hasher);

src/test/run-pass/type-id-higher-rank.rs

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ fn main() {
4545
assert!(g != h);
4646
assert!(g != i);
4747
assert!(h != i);
48+
49+
// Make sure lifetime anonymization handles nesting correctly
50+
let j = TypeId::of::<fn(for<'a> fn(&'a isize) -> &'a usize)>();
51+
let k = TypeId::of::<fn(for<'b> fn(&'b isize) -> &'b usize)>();
52+
assert_eq!(j, k);
4853
}
4954
// Boxed unboxed closures
5055
{

0 commit comments

Comments
 (0)