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

+3
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

+1
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

+1
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

+1
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

+3-1
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

+46
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

+8
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

+1
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

+5
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

+1
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,

src/librustc_metadata/encoder.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
482482

483483
fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
484484
let tcx = self.tcx;
485-
let ty = tcx.type_of(def_id);
485+
let ty = tcx.anonymize_scope_regions(&tcx.type_of(def_id));
486486
debug!("IsolatedEncoder::encode_item_type({:?}) => {:?}", def_id, ty);
487487
self.lazy(&ty)
488488
}
@@ -506,7 +506,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
506506
discr: variant.discr,
507507
struct_ctor: None,
508508
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
509-
Some(self.lazy(&tcx.fn_sig(def_id)))
509+
Some(self.encode_fn_sig(def_id))
510510
} else {
511511
None
512512
}
@@ -633,7 +633,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
633633
discr: variant.discr,
634634
struct_ctor: Some(def_id.index),
635635
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
636-
Some(self.lazy(&tcx.fn_sig(def_id)))
636+
Some(self.encode_fn_sig(def_id))
637637
} else {
638638
None
639639
}
@@ -683,7 +683,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
683683
fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
684684
debug!("IsolatedEncoder::encode_predicates({:?})", def_id);
685685
let tcx = self.tcx;
686-
self.lazy(&tcx.predicates_of(def_id))
686+
self.lazy(&tcx.anonymize_scope_regions(&tcx.predicates_of(def_id)))
687+
}
688+
689+
fn encode_fn_sig(&mut self, def_id: DefId) -> Lazy<ty::PolyFnSig<'tcx>> {
690+
debug!("IsolatedEncoder::encode_fn_sig({:?})", def_id);
691+
let tcx = self.tcx;
692+
self.lazy(&tcx.anonymize_scope_regions(&tcx.fn_sig(def_id)))
687693
}
688694

689695
fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
@@ -720,7 +726,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
720726
FnData {
721727
constness: hir::Constness::NotConst,
722728
arg_names,
723-
sig: self.lazy(&tcx.fn_sig(def_id)),
729+
sig: self.encode_fn_sig(def_id),
724730
}
725731
} else {
726732
bug!()
@@ -776,7 +782,6 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
776782

777783
fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'tcx> {
778784
debug!("IsolatedEncoder::encode_info_for_impl_item({:?})", def_id);
779-
let tcx = self.tcx;
780785

781786
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
782787
let ast_item = self.tcx.hir.expect_impl_item(node_id);
@@ -799,7 +804,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
799804
FnData {
800805
constness: sig.constness,
801806
arg_names: self.encode_fn_arg_names_for_body(body),
802-
sig: self.lazy(&tcx.fn_sig(def_id)),
807+
sig: self.encode_fn_sig(def_id),
803808
}
804809
} else {
805810
bug!()
@@ -917,7 +922,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
917922
let data = FnData {
918923
constness,
919924
arg_names: self.encode_fn_arg_names_for_body(body),
920-
sig: self.lazy(&tcx.fn_sig(def_id)),
925+
sig: self.encode_fn_sig(def_id),
921926
};
922927

923928
EntryKind::Fn(self.lazy(&data))
@@ -1013,7 +1018,9 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
10131018
unsafety: trait_def.unsafety,
10141019
paren_sugar: trait_def.paren_sugar,
10151020
has_default_impl: tcx.trait_has_default_impl(def_id),
1016-
super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
1021+
super_predicates: self.lazy(
1022+
&tcx.anonymize_scope_regions(&tcx.super_predicates_of(def_id))
1023+
),
10171024
};
10181025

10191026
EntryKind::Trait(self.lazy(&data))
@@ -1215,7 +1222,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12151222

12161223
let data = ClosureData {
12171224
kind: tcx.closure_kind(def_id),
1218-
sig: self.lazy(&tcx.fn_sig(def_id)),
1225+
sig: self.encode_fn_sig(def_id),
12191226
};
12201227

12211228
Entry {
@@ -1403,7 +1410,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
14031410
let data = FnData {
14041411
constness: hir::Constness::NotConst,
14051412
arg_names: self.encode_fn_arg_names(names),
1406-
sig: self.lazy(&tcx.fn_sig(def_id)),
1413+
sig: self.encode_fn_sig(def_id),
14071414
};
14081415
EntryKind::ForeignFn(self.lazy(&data))
14091416
}

src/librustc_typeck/check/writeback.rs

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
289289
ty::ReFree(_) |
290290
ty::ReLateBound(..) |
291291
ty::ReScope(_) |
292+
ty::ReScopeAnon(_) |
292293
ty::ReSkolemized(..) => {
293294
let span = node_id.to_span(&self.fcx.tcx);
294295
span_err!(self.tcx().sess, span, E0564,

src/librustc_typeck/variance/constraints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
455455

456456
ty::ReFree(..) |
457457
ty::ReScope(..) |
458+
ty::ReScopeAnon(..) |
458459
ty::ReVar(..) |
459460
ty::ReSkolemized(..) |
460461
ty::ReEmpty |

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ impl Clean<Option<Lifetime>> for ty::RegionKind {
873873
ty::ReLateBound(..) |
874874
ty::ReFree(..) |
875875
ty::ReScope(..) |
876+
ty::ReScopeAnon(..) |
876877
ty::ReVar(..) |
877878
ty::ReSkolemized(..) |
878879
ty::ReEmpty |

0 commit comments

Comments
 (0)