From 65e9bc0c93ec7e4b25b8fd0e03e377c09bfd1748 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Sat, 22 Aug 2015 17:39:21 +0300 Subject: [PATCH] store the CodeExtent directly in FreeRegion this makes the code cleaner --- src/librustc/metadata/tydecode.rs | 7 +------ src/librustc/metadata/tyencode.rs | 7 +------ src/librustc/middle/free_region.rs | 3 +-- src/librustc/middle/infer/error_reporting.rs | 7 ++++--- src/librustc/middle/infer/region_inference/mod.rs | 11 ++++------- src/librustc/middle/liveness.rs | 3 +-- src/librustc/middle/mem_categorization.rs | 3 +-- src/librustc/middle/region.rs | 10 +++++++--- src/librustc/middle/ty.rs | 11 +++++------ src/librustc_borrowck/borrowck/gather_loans/mod.rs | 4 +--- src/librustc_typeck/astconv.rs | 2 +- src/librustc_typeck/check/closure.rs | 3 +-- src/librustc_typeck/check/mod.rs | 5 ++--- src/librustc_typeck/check/wf.rs | 8 +++----- src/librustc_typeck/check/wfcheck.rs | 3 +-- src/librustc_typeck/collect.rs | 4 ++-- 16 files changed, 36 insertions(+), 55 deletions(-) diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 768e769efedc..c66d4084aca9 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -225,7 +225,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> { } 'f' => { assert_eq!(self.next(), '['); - let scope = self.parse_destruction_scope_data(); + let scope = self.parse_scope(); assert_eq!(self.next(), '|'); let br = self.parse_bound_region(); assert_eq!(self.next(), ']'); @@ -284,11 +284,6 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> { }) } - fn parse_destruction_scope_data(&mut self) -> region::DestructionScopeData { - let node_id = self.parse_uint() as ast::NodeId; - region::DestructionScopeData::new(node_id) - } - fn parse_opt(&mut self, f: F) -> Option where F: FnOnce(&mut TyDecoder<'a, 'tcx>) -> T, { diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc/metadata/tyencode.rs index a17d27acc2a2..70345dc8bad1 100644 --- a/src/librustc/metadata/tyencode.rs +++ b/src/librustc/metadata/tyencode.rs @@ -255,7 +255,7 @@ pub fn enc_region(w: &mut Encoder, cx: &ctxt, r: ty::Region) { } ty::ReFree(ref fr) => { mywrite!(w, "f["); - enc_destruction_scope_data(w, fr.scope); + enc_scope(w, cx, fr.scope); mywrite!(w, "|"); enc_bound_region(w, cx, fr.bound_region); mywrite!(w, "]"); @@ -289,11 +289,6 @@ fn enc_scope(w: &mut Encoder, cx: &ctxt, scope: region::CodeExtent) { } } -fn enc_destruction_scope_data(w: &mut Encoder, - d: region::DestructionScopeData) { - mywrite!(w, "{}", d.node_id); -} - fn enc_bound_region(w: &mut Encoder, cx: &ctxt, br: ty::BoundRegion) { match br { ty::BrAnon(idx) => { diff --git a/src/librustc/middle/free_region.rs b/src/librustc/middle/free_region.rs index 4b81117f2e9a..6ab56badbcf4 100644 --- a/src/librustc/middle/free_region.rs +++ b/src/librustc/middle/free_region.rs @@ -135,8 +135,7 @@ impl FreeRegionMap { tcx.region_maps.is_subscope_of(sub_scope, super_scope), (ty::ReScope(sub_scope), ty::ReFree(fr)) => - tcx.region_maps.is_subscope_of(sub_scope, - fr.scope.to_code_extent(&tcx.region_maps)) || + tcx.region_maps.is_subscope_of(sub_scope, fr.scope) || self.is_static(fr), (ty::ReFree(sub_fr), ty::ReFree(super_fr)) => diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs index c7261b4a781c..9044bf4db8c3 100644 --- a/src/librustc/middle/infer/error_reporting.rs +++ b/src/librustc/middle/infer/error_reporting.rs @@ -172,7 +172,7 @@ impl<'tcx> ty::ctxt<'tcx> { } }; - match self.map.find(fr.scope.node_id) { + match self.map.find(fr.scope.node_id(&self.region_maps)) { Some(ast_map::NodeBlock(ref blk)) => { let (msg, opt_span) = explain_span(self, "block", blk.span); (format!("{} {}", prefix, msg), opt_span) @@ -183,7 +183,8 @@ impl<'tcx> ty::ctxt<'tcx> { (format!("{} {}", prefix, msg), opt_span) } Some(_) | None => { - // this really should not happen + // this really should not happen, but it does: + // FIXME(#27942) (format!("{} unknown free region bounded by scope {:?}", prefix, fr.scope), None) } @@ -422,7 +423,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> { return None } assert!(fr1.scope == fr2.scope); - (fr1.scope.node_id, fr1, fr2) + (fr1.scope.node_id(&tcx.region_maps), fr1, fr2) }, _ => return None }; diff --git a/src/librustc/middle/infer/region_inference/mod.rs b/src/librustc/middle/infer/region_inference/mod.rs index 4a6c30853df4..1785fe09f87a 100644 --- a/src/librustc/middle/infer/region_inference/mod.rs +++ b/src/librustc/middle/infer/region_inference/mod.rs @@ -790,10 +790,9 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> { // A "free" region can be interpreted as "some region // at least as big as the block fr.scope_id". So, we can // reasonably compare free regions and scopes: - let fr_scope = fr.scope.to_code_extent(&self.tcx.region_maps); - let r_id = self.tcx.region_maps.nearest_common_ancestor(fr_scope, s_id); + let r_id = self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id); - if r_id == fr_scope { + if r_id == fr.scope { // if the free region's scope `fr.scope_id` is bigger than // the scope region `s_id`, then the LUB is the free // region itself: @@ -871,8 +870,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> { // than the scope `s_id`, then we can say that the GLB // is the scope `s_id`. Otherwise, as we do not know // big the free region is precisely, the GLB is undefined. - let fr_scope = fr.scope.to_code_extent(&self.tcx.region_maps); - if self.tcx.region_maps.nearest_common_ancestor(fr_scope, s_id) == fr_scope || + if self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id) == fr.scope || free_regions.is_static(fr) { Ok(s) } else { @@ -927,8 +925,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> { Ok(ty::ReFree(*b)) } else { this.intersect_scopes(ty::ReFree(*a), ty::ReFree(*b), - a.scope.to_code_extent(&this.tcx.region_maps), - b.scope.to_code_extent(&this.tcx.region_maps)) + a.scope, b.scope) } } } diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index 87b8e72f56f1..e1866d878bda 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -111,7 +111,6 @@ use self::VarKind::*; use middle::def::*; use middle::pat_util; -use middle::region; use middle::ty; use lint; use util::nodemap::NodeMap; @@ -1509,7 +1508,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { // within the fn body, late-bound regions are liberated: let fn_ret = self.ir.tcx.liberate_late_bound_regions( - region::DestructionScopeData::new(body.id), + self.ir.tcx.region_maps.item_extent(body.id), &self.fn_ret(id)); match fn_ret { diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 0cab4b610be7..a99140731020 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -77,7 +77,6 @@ use middle::def_id::DefId; use middle::infer; use middle::check_const; use middle::def; -use middle::region; use middle::ty::{self, Ty}; use syntax::ast::{MutImmutable, MutMutable}; @@ -749,7 +748,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { // The environment of a closure is guaranteed to // outlive any bindings introduced in the body of the // closure itself. - scope: region::DestructionScopeData::new(fn_body_id), + scope: self.tcx().region_maps.item_extent(fn_body_id), bound_region: ty::BrEnv }); diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index f4260dd700d6..f9faba8c3198 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -309,6 +309,13 @@ impl RegionMaps { pub fn lookup_code_extent(&self, e: CodeExtentData) -> CodeExtent { self.code_extent_interner.borrow()[&e] } + pub fn node_extent(&self, n: ast::NodeId) -> CodeExtent { + self.lookup_code_extent(CodeExtentData::Misc(n)) + } + // Returns the code extent for an item - the destruction scope. + pub fn item_extent(&self, n: ast::NodeId) -> CodeExtent { + self.lookup_code_extent(CodeExtentData::DestructionScope(n)) + } pub fn intern_code_extent(&self, e: CodeExtentData, parent: CodeExtent) -> CodeExtent { @@ -350,9 +357,6 @@ impl RegionMaps { parent: CodeExtent) -> CodeExtent { self.intern_code_extent(CodeExtentData::Misc(n), parent) } - pub fn node_extent(&self, n: ast::NodeId) -> CodeExtent { - self.lookup_code_extent(CodeExtentData::Misc(n)) - } pub fn code_extent_data(&self, e: CodeExtent) -> CodeExtentData { self.code_extents.borrow()[e.0 as usize] } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index a18bff5591dd..eff560653c14 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -1679,7 +1679,7 @@ impl Region { /// A "free" region `fr` can be interpreted as "some region /// at least as big as the scope `fr.scope`". pub struct FreeRegion { - pub scope: region::DestructionScopeData, + pub scope: region::CodeExtent, pub bound_region: BoundRegion } @@ -6610,7 +6610,7 @@ impl<'tcx> ctxt<'tcx> { types.push(def.space, self.mk_param_from_def(def)); } - let free_id_outlive = region::DestructionScopeData::new(free_id); + let free_id_outlive = self.region_maps.item_extent(free_id); // map bound 'a => free 'a let mut regions = VecPerParamSpace::empty(); @@ -6641,7 +6641,7 @@ impl<'tcx> ctxt<'tcx> { // let free_substs = self.construct_free_substs(generics, free_id); - let free_id_outlive = region::DestructionScopeData::new(free_id); + let free_id_outlive = self.region_maps.item_extent(free_id); // // Compute the bounds on Self and the type parameters. @@ -6673,8 +6673,7 @@ impl<'tcx> ctxt<'tcx> { let unnormalized_env = ty::ParameterEnvironment { tcx: self, free_substs: free_substs, - implicit_region_bound: ty::ReScope( - free_id_outlive.to_code_extent(&self.region_maps)), + implicit_region_bound: ty::ReScope(free_id_outlive), caller_bounds: predicates, selection_cache: traits::SelectionCache::new(), free_id: free_id, @@ -6838,7 +6837,7 @@ impl<'tcx> ctxt<'tcx> { /// Replace any late-bound regions bound in `value` with free variants attached to scope-id /// `scope_id`. pub fn liberate_late_bound_regions(&self, - all_outlive_scope: region::DestructionScopeData, + all_outlive_scope: region::CodeExtent, value: &Binder) -> T where T : TypeFoldable<'tcx> diff --git a/src/librustc_borrowck/borrowck/gather_loans/mod.rs b/src/librustc_borrowck/borrowck/gather_loans/mod.rs index c3801f436e58..cbdd0020a303 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/mod.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/mod.rs @@ -360,9 +360,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> { let loan_scope = match loan_region { ty::ReScope(scope) => scope, - ty::ReFree(ref fr) => { - fr.scope.to_code_extent(&self.tcx().region_maps) - } + ty::ReFree(ref fr) => fr.scope, ty::ReStatic => { // If we get here, an error must have been diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index f14f196f1aeb..944169fc45eb 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -179,7 +179,7 @@ pub fn ast_region_to_region(tcx: &ty::ctxt, lifetime: &ast::Lifetime) Some(&rl::DefFreeRegion(scope, id)) => { ty::ReFree(ty::FreeRegion { - scope: scope, + scope: tcx.region_maps.item_extent(scope.node_id), bound_region: ty::BrNamed(DefId::local(id), lifetime.name) }) diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 6d7919a84efb..a3714fead8e2 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -14,7 +14,6 @@ use super::{check_fn, Expectation, FnCtxt}; use astconv; use middle::def_id::DefId; -use middle::region; use middle::subst; use middle::ty::{self, ToPolyTraitRef, Ty}; use std::cmp; @@ -77,7 +76,7 @@ fn check_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, fcx.write_ty(expr.id, closure_type); let fn_sig = fcx.tcx().liberate_late_bound_regions( - region::DestructionScopeData::new(body.id), &fn_ty.sig); + fcx.tcx().region_maps.item_extent(body.id), &fn_ty.sig); check_fn(fcx.ccx, ast::Unsafety::Normal, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index bbea25525738..cf08490d720c 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -91,7 +91,6 @@ use middle::infer; use middle::infer::type_variable; use middle::pat_util::{self, pat_id_map}; use middle::privacy::{AllPublic, LastMod}; -use middle::region::{self}; use middle::subst::{self, Subst, Substs, VecPerParamSpace, ParamSpace, TypeSpace}; use middle::traits::{self, report_fulfillment_errors}; use middle::ty::{FnSig, GenericPredicates, TypeScheme}; @@ -455,11 +454,11 @@ fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let inh = Inherited::new(ccx.tcx, &tables, param_env); // Compute the fty from point of view of inside fn. + let fn_scope = ccx.tcx.region_maps.item_extent(body.id); let fn_sig = fn_ty.sig.subst(ccx.tcx, &inh.infcx.parameter_environment.free_substs); let fn_sig = - ccx.tcx.liberate_late_bound_regions(region::DestructionScopeData::new(body.id), - &fn_sig); + ccx.tcx.liberate_late_bound_regions(fn_scope, &fn_sig); let fn_sig = inh.normalize_associated_types_in(body.span, body.id, diff --git a/src/librustc_typeck/check/wf.rs b/src/librustc_typeck/check/wf.rs index d1e159d44560..0ef1d4b81aca 100644 --- a/src/librustc_typeck/check/wf.rs +++ b/src/librustc_typeck/check/wf.rs @@ -466,10 +466,7 @@ pub struct BoundsChecker<'cx,'tcx:'cx> { fcx: &'cx FnCtxt<'cx,'tcx>, span: Span, - // This field is often attached to item impls; it is not clear - // that `CodeExtent` is well-defined for such nodes, so pnkfelix - // has left it as a NodeId rather than porting to CodeExtent. - scope: ast::NodeId, + scope: region::CodeExtent, binding_count: usize, cache: Option<&'cx mut HashSet>>, @@ -480,6 +477,7 @@ impl<'cx,'tcx> BoundsChecker<'cx,'tcx> { scope: ast::NodeId, cache: Option<&'cx mut HashSet>>) -> BoundsChecker<'cx,'tcx> { + let scope = fcx.tcx().region_maps.item_extent(scope); BoundsChecker { fcx: fcx, span: DUMMY_SP, scope: scope, cache: cache, binding_count: 0 } } @@ -532,7 +530,7 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> { { self.binding_count += 1; let value = self.fcx.tcx().liberate_late_bound_regions( - region::DestructionScopeData::new(self.scope), + self.scope, binder); debug!("BoundsChecker::fold_binder: late-bound regions replaced: {:?} at scope: {:?}", value, self.scope); diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index cdad1257533c..4280e392d180 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -13,7 +13,6 @@ use check::{FnCtxt, Inherited, blank_fn_ctxt, regionck}; use constrained_type_params::{identify_constrained_type_params, Parameter}; use CrateCtxt; use middle::def_id::DefId; -use middle::region::DestructionScopeData; use middle::subst::{self, TypeSpace, FnSpace, ParamSpace, SelfSpace}; use middle::traits; use middle::ty::{self, Ty}; @@ -362,7 +361,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> { { let free_substs = &fcx.inh.infcx.parameter_environment.free_substs; let fty = fcx.instantiate_type_scheme(span, free_substs, fty); - let free_id_outlive = DestructionScopeData::new(free_id); + let free_id_outlive = fcx.tcx().region_maps.item_extent(free_id); let sig = fcx.tcx().liberate_late_bound_regions(free_id_outlive, &fty.sig); for &input_ty in &sig.inputs { diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index d3e414fd9c0e..544f6d9f0ed8 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2311,7 +2311,7 @@ fn check_method_self_type<'a, 'tcx, RS:RegionScope>( _ => typ, }; - let body_scope = region::DestructionScopeData::new(body_id); + let body_scope = tcx.region_maps.item_extent(body_id); // "Required type" comes from the trait definition. It may // contain late-bound regions from the method, but not the @@ -2363,7 +2363,7 @@ fn check_method_self_type<'a, 'tcx, RS:RegionScope>( fn liberate_early_bound_regions<'tcx,T>( tcx: &ty::ctxt<'tcx>, - scope: region::DestructionScopeData, + scope: region::CodeExtent, value: &T) -> T where T : TypeFoldable<'tcx>