Skip to content

Commit 8ecb35b

Browse files
Make ReScope values opaque when exporting to crate metadata.
1 parent 2b0120f commit 8ecb35b

File tree

14 files changed

+91
-12
lines changed

14 files changed

+91
-12
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ for ty::RegionKind {
6868
ty::ReScope(code_extent) => {
6969
code_extent.hash_stable(hcx, hasher);
7070
}
71+
ty::ReScopeAnon(ref fingerprint) => {
72+
fingerprint.hash_stable(hcx, hasher);
73+
}
7174
ty::ReFree(ref free_region) => {
7275
free_region.hash_stable(hcx, hasher);
7376
}

src/librustc/infer/combine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
453453
ty::ReEmpty |
454454
ty::ReStatic |
455455
ty::ReScope(..) |
456+
ty::ReScopeAnon(..) |
456457
ty::ReVar(..) |
457458
ty::ReEarlyBound(..) |
458459
ty::ReFree(..) => {

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
244244
ty::ReSkolemized(..) |
245245
ty::ReVar(_) |
246246
ty::ReLateBound(..) |
247+
ty::ReScopeAnon(..) |
247248
ty::ReErased => {
248249
(format!("lifetime {:?}", region), None)
249250
}

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
9494
ty::ReEarlyBound(..) |
9595
ty::ReFree(_) |
9696
ty::ReScope(_) |
97+
ty::ReScopeAnon(_) |
9798
ty::ReVar(_) |
9899
ty::ReSkolemized(..) |
99100
ty::ReEmpty |

src/librustc/infer/region_inference/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use middle::free_region::RegionRelations;
2626
use ty::{self, Ty, TyCtxt};
2727
use ty::{Region, RegionVid};
2828
use ty::{ReEmpty, ReStatic, ReFree, ReEarlyBound, ReErased};
29-
use ty::{ReLateBound, ReScope, ReVar, ReSkolemized, BrFresh};
29+
use ty::{ReLateBound, ReScope, ReScopeAnon, ReVar, ReSkolemized, BrFresh};
3030

3131
use std::cell::{Cell, RefCell};
3232
use std::fmt;
@@ -905,6 +905,8 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
905905
match (a, b) {
906906
(&ReLateBound(..), _) |
907907
(_, &ReLateBound(..)) |
908+
(&ReScopeAnon(..), _) |
909+
(_, &ReScopeAnon(..)) |
908910
(&ReErased, _) |
909911
(_, &ReErased) => {
910912
bug!("cannot relate region: LUB({:?}, {:?})", a, b);

src/librustc/ty/fold.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,49 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
654654
false
655655
}
656656
}
657+
658+
///////////////////////////////////////////////////////////////////////////
659+
// ReScope Anonymizer
660+
661+
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
662+
663+
/// Transforms RegionKind::ReScope values into equivalent
664+
/// RegionKind::ReScopeAnon values. This should be done before exporting to
665+
/// crate metadata (as long as we can't use erase_regions() on everything
666+
/// that is exported).
667+
pub fn anonymize_scope_regions<T>(self, value: &T) -> T
668+
where T : TypeFoldable<'tcx>
669+
{
670+
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
671+
use ich::{StableHashingContext, Fingerprint};
672+
673+
let hcx = StableHashingContext::new(self);
674+
675+
return value.fold_with(&mut AnonReScopes {
676+
tcx: self,
677+
hcx,
678+
});
679+
680+
struct AnonReScopes<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
681+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
682+
hcx: StableHashingContext<'a, 'gcx, 'tcx>,
683+
}
684+
685+
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AnonReScopes<'a, 'gcx, 'tcx> {
686+
687+
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
688+
689+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
690+
match *r {
691+
ty::ReScope(code_extent) => {
692+
let mut hasher = StableHasher::new();
693+
code_extent.hash_stable(&mut self.hcx, &mut hasher);
694+
let code_extent_fingerprint: Fingerprint = hasher.finish();
695+
self.tcx.mk_region(ty::ReScopeAnon(code_extent_fingerprint))
696+
},
697+
_ => r,
698+
}
699+
}
700+
}
701+
}
702+
}

src/librustc/ty/sty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use util::nodemap::FxHashMap;
2929
use serialize;
3030

3131
use hir;
32+
use ich;
3233

3334
use self::InferTy::*;
3435
use self::TypeVariants::*;
@@ -785,6 +786,13 @@ pub enum RegionKind {
785786
/// current function.
786787
ReScope(region::CodeExtent),
787788

789+
/// The same as ReScope but with the CodeExtent transformed into a opaque,
790+
/// stable representation. Any ReScope that gets exported to crate metadata
791+
/// should be transformed into such a ReScopeAnon in order to avoid mixing
792+
/// NodeIds from different crates. Note that ReScopeAnon values can still
793+
/// safely be hashed and compared for equality.
794+
ReScopeAnon(ich::Fingerprint),
795+
788796
/// Static data that has an "infinite" lifetime. Top in the region lattice.
789797
ReStatic,
790798

src/librustc/ty/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
753753
ty::ReLateBound(..) |
754754
ty::ReFree(..) |
755755
ty::ReScope(..) |
756+
ty::ReScopeAnon(..) |
756757
ty::ReVar(..) |
757758
ty::ReSkolemized(..) => {
758759
bug!("TypeIdHasher: unexpected region {:?}", r)

src/librustc/util/ppaux.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ impl fmt::Debug for ty::RegionKind {
481481
write!(f, "ReScope({:?})", id)
482482
}
483483

484+
ty::ReScopeAnon(fingerprint) => {
485+
write!(f, "ReScopeAnon({:?})", fingerprint)
486+
}
487+
484488
ty::ReStatic => write!(f, "ReStatic"),
485489

486490
ty::ReVar(ref vid) => {
@@ -543,6 +547,7 @@ impl fmt::Display for ty::RegionKind {
543547
write!(f, "'{}rv", region_vid.index)
544548
}
545549
ty::ReScope(_) |
550+
ty::ReScopeAnon(_) |
546551
ty::ReVar(_) |
547552
ty::ReErased => Ok(()),
548553
ty::ReStatic => write!(f, "'static"),

src/librustc_borrowck/borrowck/gather_loans/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
367367
ty::ReLateBound(..) |
368368
ty::ReVar(..) |
369369
ty::ReSkolemized(..) |
370+
ty::ReScopeAnon(..) |
370371
ty::ReErased => {
371372
span_bug!(
372373
cmt.span,

0 commit comments

Comments
 (0)