Skip to content

Commit

Permalink
Make ReScope values opaque when exporting to crate metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 16, 2017
1 parent 2b0120f commit 8ecb35b
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/librustc/ich/impls_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ for ty::RegionKind {
ty::ReScope(code_extent) => {
code_extent.hash_stable(hcx, hasher);
}
ty::ReScopeAnon(ref fingerprint) => {
fingerprint.hash_stable(hcx, hasher);
}
ty::ReFree(ref free_region) => {
free_region.hash_stable(hcx, hasher);
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
ty::ReEmpty |
ty::ReStatic |
ty::ReScope(..) |
ty::ReScopeAnon(..) |
ty::ReVar(..) |
ty::ReEarlyBound(..) |
ty::ReFree(..) => {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
ty::ReSkolemized(..) |
ty::ReVar(_) |
ty::ReLateBound(..) |
ty::ReScopeAnon(..) |
ty::ReErased => {
(format!("lifetime {:?}", region), None)
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::ReEarlyBound(..) |
ty::ReFree(_) |
ty::ReScope(_) |
ty::ReScopeAnon(_) |
ty::ReVar(_) |
ty::ReSkolemized(..) |
ty::ReEmpty |
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/infer/region_inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use middle::free_region::RegionRelations;
use ty::{self, Ty, TyCtxt};
use ty::{Region, RegionVid};
use ty::{ReEmpty, ReStatic, ReFree, ReEarlyBound, ReErased};
use ty::{ReLateBound, ReScope, ReVar, ReSkolemized, BrFresh};
use ty::{ReLateBound, ReScope, ReScopeAnon, ReVar, ReSkolemized, BrFresh};

use std::cell::{Cell, RefCell};
use std::fmt;
Expand Down Expand Up @@ -905,6 +905,8 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
match (a, b) {
(&ReLateBound(..), _) |
(_, &ReLateBound(..)) |
(&ReScopeAnon(..), _) |
(_, &ReScopeAnon(..)) |
(&ReErased, _) |
(_, &ReErased) => {
bug!("cannot relate region: LUB({:?}, {:?})", a, b);
Expand Down
46 changes: 46 additions & 0 deletions src/librustc/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,49 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
false
}
}

///////////////////////////////////////////////////////////////////////////
// ReScope Anonymizer

impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

/// Transforms RegionKind::ReScope values into equivalent
/// RegionKind::ReScopeAnon values. This should be done before exporting to
/// crate metadata (as long as we can't use erase_regions() on everything
/// that is exported).
pub fn anonymize_scope_regions<T>(self, value: &T) -> T
where T : TypeFoldable<'tcx>
{
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
use ich::{StableHashingContext, Fingerprint};

let hcx = StableHashingContext::new(self);

return value.fold_with(&mut AnonReScopes {
tcx: self,
hcx,
});

struct AnonReScopes<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
hcx: StableHashingContext<'a, 'gcx, 'tcx>,
}

impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AnonReScopes<'a, 'gcx, 'tcx> {

fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }

fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
match *r {
ty::ReScope(code_extent) => {
let mut hasher = StableHasher::new();
code_extent.hash_stable(&mut self.hcx, &mut hasher);
let code_extent_fingerprint: Fingerprint = hasher.finish();
self.tcx.mk_region(ty::ReScopeAnon(code_extent_fingerprint))
},
_ => r,
}
}
}
}
}
8 changes: 8 additions & 0 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use util::nodemap::FxHashMap;
use serialize;

use hir;
use ich;

use self::InferTy::*;
use self::TypeVariants::*;
Expand Down Expand Up @@ -785,6 +786,13 @@ pub enum RegionKind {
/// current function.
ReScope(region::CodeExtent),

/// The same as ReScope but with the CodeExtent transformed into a opaque,
/// stable representation. Any ReScope that gets exported to crate metadata
/// should be transformed into such a ReScopeAnon in order to avoid mixing
/// NodeIds from different crates. Note that ReScopeAnon values can still
/// safely be hashed and compared for equality.
ReScopeAnon(ich::Fingerprint),

/// Static data that has an "infinite" lifetime. Top in the region lattice.
ReStatic,

Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
ty::ReLateBound(..) |
ty::ReFree(..) |
ty::ReScope(..) |
ty::ReScopeAnon(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) => {
bug!("TypeIdHasher: unexpected region {:?}", r)
Expand Down
5 changes: 5 additions & 0 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ impl fmt::Debug for ty::RegionKind {
write!(f, "ReScope({:?})", id)
}

ty::ReScopeAnon(fingerprint) => {
write!(f, "ReScopeAnon({:?})", fingerprint)
}

ty::ReStatic => write!(f, "ReStatic"),

ty::ReVar(ref vid) => {
Expand Down Expand Up @@ -543,6 +547,7 @@ impl fmt::Display for ty::RegionKind {
write!(f, "'{}rv", region_vid.index)
}
ty::ReScope(_) |
ty::ReScopeAnon(_) |
ty::ReVar(_) |
ty::ReErased => Ok(()),
ty::ReStatic => write!(f, "'static"),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
ty::ReLateBound(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) |
ty::ReScopeAnon(..) |
ty::ReErased => {
span_bug!(
cmt.span,
Expand Down
29 changes: 18 additions & 11 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {

fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
let tcx = self.tcx;
let ty = tcx.type_of(def_id);
let ty = tcx.anonymize_scope_regions(&tcx.type_of(def_id));
debug!("IsolatedEncoder::encode_item_type({:?}) => {:?}", def_id, ty);
self.lazy(&ty)
}
Expand All @@ -506,7 +506,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
discr: variant.discr,
struct_ctor: None,
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
Some(self.lazy(&tcx.fn_sig(def_id)))
Some(self.encode_fn_sig(def_id))
} else {
None
}
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
discr: variant.discr,
struct_ctor: Some(def_id.index),
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
Some(self.lazy(&tcx.fn_sig(def_id)))
Some(self.encode_fn_sig(def_id))
} else {
None
}
Expand Down Expand Up @@ -683,7 +683,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
debug!("IsolatedEncoder::encode_predicates({:?})", def_id);
let tcx = self.tcx;
self.lazy(&tcx.predicates_of(def_id))
self.lazy(&tcx.anonymize_scope_regions(&tcx.predicates_of(def_id)))
}

fn encode_fn_sig(&mut self, def_id: DefId) -> Lazy<ty::PolyFnSig<'tcx>> {
debug!("IsolatedEncoder::encode_fn_sig({:?})", def_id);
let tcx = self.tcx;
self.lazy(&tcx.anonymize_scope_regions(&tcx.fn_sig(def_id)))
}

fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
Expand Down Expand Up @@ -720,7 +726,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
FnData {
constness: hir::Constness::NotConst,
arg_names,
sig: self.lazy(&tcx.fn_sig(def_id)),
sig: self.encode_fn_sig(def_id),
}
} else {
bug!()
Expand Down Expand Up @@ -776,7 +782,6 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {

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

let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
let ast_item = self.tcx.hir.expect_impl_item(node_id);
Expand All @@ -799,7 +804,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
FnData {
constness: sig.constness,
arg_names: self.encode_fn_arg_names_for_body(body),
sig: self.lazy(&tcx.fn_sig(def_id)),
sig: self.encode_fn_sig(def_id),
}
} else {
bug!()
Expand Down Expand Up @@ -917,7 +922,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
let data = FnData {
constness,
arg_names: self.encode_fn_arg_names_for_body(body),
sig: self.lazy(&tcx.fn_sig(def_id)),
sig: self.encode_fn_sig(def_id),
};

EntryKind::Fn(self.lazy(&data))
Expand Down Expand Up @@ -1013,7 +1018,9 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
unsafety: trait_def.unsafety,
paren_sugar: trait_def.paren_sugar,
has_default_impl: tcx.trait_has_default_impl(def_id),
super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
super_predicates: self.lazy(
&tcx.anonymize_scope_regions(&tcx.super_predicates_of(def_id))
),
};

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

let data = ClosureData {
kind: tcx.closure_kind(def_id),
sig: self.lazy(&tcx.fn_sig(def_id)),
sig: self.encode_fn_sig(def_id),
};

Entry {
Expand Down Expand Up @@ -1403,7 +1410,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
let data = FnData {
constness: hir::Constness::NotConst,
arg_names: self.encode_fn_arg_names(names),
sig: self.lazy(&tcx.fn_sig(def_id)),
sig: self.encode_fn_sig(def_id),
};
EntryKind::ForeignFn(self.lazy(&data))
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
ty::ReFree(_) |
ty::ReLateBound(..) |
ty::ReScope(_) |
ty::ReScopeAnon(_) |
ty::ReSkolemized(..) => {
let span = node_id.to_span(&self.fcx.tcx);
span_err!(self.tcx().sess, span, E0564,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/variance/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {

ty::ReFree(..) |
ty::ReScope(..) |
ty::ReScopeAnon(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) |
ty::ReEmpty |
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ impl Clean<Option<Lifetime>> for ty::RegionKind {
ty::ReLateBound(..) |
ty::ReFree(..) |
ty::ReScope(..) |
ty::ReScopeAnon(..) |
ty::ReVar(..) |
ty::ReSkolemized(..) |
ty::ReEmpty |
Expand Down

0 comments on commit 8ecb35b

Please sign in to comment.