Skip to content

Commit 65e9bc0

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
store the CodeExtent directly in FreeRegion
this makes the code cleaner
1 parent fc30438 commit 65e9bc0

File tree

16 files changed

+36
-55
lines changed

16 files changed

+36
-55
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
225225
}
226226
'f' => {
227227
assert_eq!(self.next(), '[');
228-
let scope = self.parse_destruction_scope_data();
228+
let scope = self.parse_scope();
229229
assert_eq!(self.next(), '|');
230230
let br = self.parse_bound_region();
231231
assert_eq!(self.next(), ']');
@@ -284,11 +284,6 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
284284
})
285285
}
286286

287-
fn parse_destruction_scope_data(&mut self) -> region::DestructionScopeData {
288-
let node_id = self.parse_uint() as ast::NodeId;
289-
region::DestructionScopeData::new(node_id)
290-
}
291-
292287
fn parse_opt<T, F>(&mut self, f: F) -> Option<T>
293288
where F: FnOnce(&mut TyDecoder<'a, 'tcx>) -> T,
294289
{

src/librustc/metadata/tyencode.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn enc_region(w: &mut Encoder, cx: &ctxt, r: ty::Region) {
255255
}
256256
ty::ReFree(ref fr) => {
257257
mywrite!(w, "f[");
258-
enc_destruction_scope_data(w, fr.scope);
258+
enc_scope(w, cx, fr.scope);
259259
mywrite!(w, "|");
260260
enc_bound_region(w, cx, fr.bound_region);
261261
mywrite!(w, "]");
@@ -289,11 +289,6 @@ fn enc_scope(w: &mut Encoder, cx: &ctxt, scope: region::CodeExtent) {
289289
}
290290
}
291291

292-
fn enc_destruction_scope_data(w: &mut Encoder,
293-
d: region::DestructionScopeData) {
294-
mywrite!(w, "{}", d.node_id);
295-
}
296-
297292
fn enc_bound_region(w: &mut Encoder, cx: &ctxt, br: ty::BoundRegion) {
298293
match br {
299294
ty::BrAnon(idx) => {

src/librustc/middle/free_region.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ impl FreeRegionMap {
135135
tcx.region_maps.is_subscope_of(sub_scope, super_scope),
136136

137137
(ty::ReScope(sub_scope), ty::ReFree(fr)) =>
138-
tcx.region_maps.is_subscope_of(sub_scope,
139-
fr.scope.to_code_extent(&tcx.region_maps)) ||
138+
tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
140139
self.is_static(fr),
141140

142141
(ty::ReFree(sub_fr), ty::ReFree(super_fr)) =>

src/librustc/middle/infer/error_reporting.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'tcx> ty::ctxt<'tcx> {
172172
}
173173
};
174174

175-
match self.map.find(fr.scope.node_id) {
175+
match self.map.find(fr.scope.node_id(&self.region_maps)) {
176176
Some(ast_map::NodeBlock(ref blk)) => {
177177
let (msg, opt_span) = explain_span(self, "block", blk.span);
178178
(format!("{} {}", prefix, msg), opt_span)
@@ -183,7 +183,8 @@ impl<'tcx> ty::ctxt<'tcx> {
183183
(format!("{} {}", prefix, msg), opt_span)
184184
}
185185
Some(_) | None => {
186-
// this really should not happen
186+
// this really should not happen, but it does:
187+
// FIXME(#27942)
187188
(format!("{} unknown free region bounded by scope {:?}",
188189
prefix, fr.scope), None)
189190
}
@@ -422,7 +423,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
422423
return None
423424
}
424425
assert!(fr1.scope == fr2.scope);
425-
(fr1.scope.node_id, fr1, fr2)
426+
(fr1.scope.node_id(&tcx.region_maps), fr1, fr2)
426427
},
427428
_ => return None
428429
};

src/librustc/middle/infer/region_inference/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,9 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
790790
// A "free" region can be interpreted as "some region
791791
// at least as big as the block fr.scope_id". So, we can
792792
// reasonably compare free regions and scopes:
793-
let fr_scope = fr.scope.to_code_extent(&self.tcx.region_maps);
794-
let r_id = self.tcx.region_maps.nearest_common_ancestor(fr_scope, s_id);
793+
let r_id = self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id);
795794

796-
if r_id == fr_scope {
795+
if r_id == fr.scope {
797796
// if the free region's scope `fr.scope_id` is bigger than
798797
// the scope region `s_id`, then the LUB is the free
799798
// region itself:
@@ -871,8 +870,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
871870
// than the scope `s_id`, then we can say that the GLB
872871
// is the scope `s_id`. Otherwise, as we do not know
873872
// big the free region is precisely, the GLB is undefined.
874-
let fr_scope = fr.scope.to_code_extent(&self.tcx.region_maps);
875-
if self.tcx.region_maps.nearest_common_ancestor(fr_scope, s_id) == fr_scope ||
873+
if self.tcx.region_maps.nearest_common_ancestor(fr.scope, s_id) == fr.scope ||
876874
free_regions.is_static(fr) {
877875
Ok(s)
878876
} else {
@@ -927,8 +925,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
927925
Ok(ty::ReFree(*b))
928926
} else {
929927
this.intersect_scopes(ty::ReFree(*a), ty::ReFree(*b),
930-
a.scope.to_code_extent(&this.tcx.region_maps),
931-
b.scope.to_code_extent(&this.tcx.region_maps))
928+
a.scope, b.scope)
932929
}
933930
}
934931
}

src/librustc/middle/liveness.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ use self::VarKind::*;
111111

112112
use middle::def::*;
113113
use middle::pat_util;
114-
use middle::region;
115114
use middle::ty;
116115
use lint;
117116
use util::nodemap::NodeMap;
@@ -1509,7 +1508,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15091508
// within the fn body, late-bound regions are liberated:
15101509
let fn_ret =
15111510
self.ir.tcx.liberate_late_bound_regions(
1512-
region::DestructionScopeData::new(body.id),
1511+
self.ir.tcx.region_maps.item_extent(body.id),
15131512
&self.fn_ret(id));
15141513

15151514
match fn_ret {

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ use middle::def_id::DefId;
7777
use middle::infer;
7878
use middle::check_const;
7979
use middle::def;
80-
use middle::region;
8180
use middle::ty::{self, Ty};
8281

8382
use syntax::ast::{MutImmutable, MutMutable};
@@ -749,7 +748,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
749748
// The environment of a closure is guaranteed to
750749
// outlive any bindings introduced in the body of the
751750
// closure itself.
752-
scope: region::DestructionScopeData::new(fn_body_id),
751+
scope: self.tcx().region_maps.item_extent(fn_body_id),
753752
bound_region: ty::BrEnv
754753
});
755754

src/librustc/middle/region.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ impl RegionMaps {
309309
pub fn lookup_code_extent(&self, e: CodeExtentData) -> CodeExtent {
310310
self.code_extent_interner.borrow()[&e]
311311
}
312+
pub fn node_extent(&self, n: ast::NodeId) -> CodeExtent {
313+
self.lookup_code_extent(CodeExtentData::Misc(n))
314+
}
315+
// Returns the code extent for an item - the destruction scope.
316+
pub fn item_extent(&self, n: ast::NodeId) -> CodeExtent {
317+
self.lookup_code_extent(CodeExtentData::DestructionScope(n))
318+
}
312319
pub fn intern_code_extent(&self,
313320
e: CodeExtentData,
314321
parent: CodeExtent) -> CodeExtent {
@@ -350,9 +357,6 @@ impl RegionMaps {
350357
parent: CodeExtent) -> CodeExtent {
351358
self.intern_code_extent(CodeExtentData::Misc(n), parent)
352359
}
353-
pub fn node_extent(&self, n: ast::NodeId) -> CodeExtent {
354-
self.lookup_code_extent(CodeExtentData::Misc(n))
355-
}
356360
pub fn code_extent_data(&self, e: CodeExtent) -> CodeExtentData {
357361
self.code_extents.borrow()[e.0 as usize]
358362
}

src/librustc/middle/ty.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ impl Region {
16791679
/// A "free" region `fr` can be interpreted as "some region
16801680
/// at least as big as the scope `fr.scope`".
16811681
pub struct FreeRegion {
1682-
pub scope: region::DestructionScopeData,
1682+
pub scope: region::CodeExtent,
16831683
pub bound_region: BoundRegion
16841684
}
16851685

@@ -6610,7 +6610,7 @@ impl<'tcx> ctxt<'tcx> {
66106610
types.push(def.space, self.mk_param_from_def(def));
66116611
}
66126612

6613-
let free_id_outlive = region::DestructionScopeData::new(free_id);
6613+
let free_id_outlive = self.region_maps.item_extent(free_id);
66146614

66156615
// map bound 'a => free 'a
66166616
let mut regions = VecPerParamSpace::empty();
@@ -6641,7 +6641,7 @@ impl<'tcx> ctxt<'tcx> {
66416641
//
66426642

66436643
let free_substs = self.construct_free_substs(generics, free_id);
6644-
let free_id_outlive = region::DestructionScopeData::new(free_id);
6644+
let free_id_outlive = self.region_maps.item_extent(free_id);
66456645

66466646
//
66476647
// Compute the bounds on Self and the type parameters.
@@ -6673,8 +6673,7 @@ impl<'tcx> ctxt<'tcx> {
66736673
let unnormalized_env = ty::ParameterEnvironment {
66746674
tcx: self,
66756675
free_substs: free_substs,
6676-
implicit_region_bound: ty::ReScope(
6677-
free_id_outlive.to_code_extent(&self.region_maps)),
6676+
implicit_region_bound: ty::ReScope(free_id_outlive),
66786677
caller_bounds: predicates,
66796678
selection_cache: traits::SelectionCache::new(),
66806679
free_id: free_id,
@@ -6838,7 +6837,7 @@ impl<'tcx> ctxt<'tcx> {
68386837
/// Replace any late-bound regions bound in `value` with free variants attached to scope-id
68396838
/// `scope_id`.
68406839
pub fn liberate_late_bound_regions<T>(&self,
6841-
all_outlive_scope: region::DestructionScopeData,
6840+
all_outlive_scope: region::CodeExtent,
68426841
value: &Binder<T>)
68436842
-> T
68446843
where T : TypeFoldable<'tcx>

src/librustc_borrowck/borrowck/gather_loans/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
360360
let loan_scope = match loan_region {
361361
ty::ReScope(scope) => scope,
362362

363-
ty::ReFree(ref fr) => {
364-
fr.scope.to_code_extent(&self.tcx().region_maps)
365-
}
363+
ty::ReFree(ref fr) => fr.scope,
366364

367365
ty::ReStatic => {
368366
// If we get here, an error must have been

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub fn ast_region_to_region(tcx: &ty::ctxt, lifetime: &ast::Lifetime)
179179

180180
Some(&rl::DefFreeRegion(scope, id)) => {
181181
ty::ReFree(ty::FreeRegion {
182-
scope: scope,
182+
scope: tcx.region_maps.item_extent(scope.node_id),
183183
bound_region: ty::BrNamed(DefId::local(id),
184184
lifetime.name)
185185
})

src/librustc_typeck/check/closure.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use super::{check_fn, Expectation, FnCtxt};
1414

1515
use astconv;
1616
use middle::def_id::DefId;
17-
use middle::region;
1817
use middle::subst;
1918
use middle::ty::{self, ToPolyTraitRef, Ty};
2019
use std::cmp;
@@ -77,7 +76,7 @@ fn check_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
7776
fcx.write_ty(expr.id, closure_type);
7877

7978
let fn_sig = fcx.tcx().liberate_late_bound_regions(
80-
region::DestructionScopeData::new(body.id), &fn_ty.sig);
79+
fcx.tcx().region_maps.item_extent(body.id), &fn_ty.sig);
8180

8281
check_fn(fcx.ccx,
8382
ast::Unsafety::Normal,

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ use middle::infer;
9191
use middle::infer::type_variable;
9292
use middle::pat_util::{self, pat_id_map};
9393
use middle::privacy::{AllPublic, LastMod};
94-
use middle::region::{self};
9594
use middle::subst::{self, Subst, Substs, VecPerParamSpace, ParamSpace, TypeSpace};
9695
use middle::traits::{self, report_fulfillment_errors};
9796
use middle::ty::{FnSig, GenericPredicates, TypeScheme};
@@ -455,11 +454,11 @@ fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
455454
let inh = Inherited::new(ccx.tcx, &tables, param_env);
456455

457456
// Compute the fty from point of view of inside fn.
457+
let fn_scope = ccx.tcx.region_maps.item_extent(body.id);
458458
let fn_sig =
459459
fn_ty.sig.subst(ccx.tcx, &inh.infcx.parameter_environment.free_substs);
460460
let fn_sig =
461-
ccx.tcx.liberate_late_bound_regions(region::DestructionScopeData::new(body.id),
462-
&fn_sig);
461+
ccx.tcx.liberate_late_bound_regions(fn_scope, &fn_sig);
463462
let fn_sig =
464463
inh.normalize_associated_types_in(body.span,
465464
body.id,

src/librustc_typeck/check/wf.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,7 @@ pub struct BoundsChecker<'cx,'tcx:'cx> {
466466
fcx: &'cx FnCtxt<'cx,'tcx>,
467467
span: Span,
468468

469-
// This field is often attached to item impls; it is not clear
470-
// that `CodeExtent` is well-defined for such nodes, so pnkfelix
471-
// has left it as a NodeId rather than porting to CodeExtent.
472-
scope: ast::NodeId,
469+
scope: region::CodeExtent,
473470

474471
binding_count: usize,
475472
cache: Option<&'cx mut HashSet<Ty<'tcx>>>,
@@ -480,6 +477,7 @@ impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
480477
scope: ast::NodeId,
481478
cache: Option<&'cx mut HashSet<Ty<'tcx>>>)
482479
-> BoundsChecker<'cx,'tcx> {
480+
let scope = fcx.tcx().region_maps.item_extent(scope);
483481
BoundsChecker { fcx: fcx, span: DUMMY_SP, scope: scope,
484482
cache: cache, binding_count: 0 }
485483
}
@@ -532,7 +530,7 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
532530
{
533531
self.binding_count += 1;
534532
let value = self.fcx.tcx().liberate_late_bound_regions(
535-
region::DestructionScopeData::new(self.scope),
533+
self.scope,
536534
binder);
537535
debug!("BoundsChecker::fold_binder: late-bound regions replaced: {:?} at scope: {:?}",
538536
value, self.scope);

src/librustc_typeck/check/wfcheck.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use check::{FnCtxt, Inherited, blank_fn_ctxt, regionck};
1313
use constrained_type_params::{identify_constrained_type_params, Parameter};
1414
use CrateCtxt;
1515
use middle::def_id::DefId;
16-
use middle::region::DestructionScopeData;
1716
use middle::subst::{self, TypeSpace, FnSpace, ParamSpace, SelfSpace};
1817
use middle::traits;
1918
use middle::ty::{self, Ty};
@@ -362,7 +361,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
362361
{
363362
let free_substs = &fcx.inh.infcx.parameter_environment.free_substs;
364363
let fty = fcx.instantiate_type_scheme(span, free_substs, fty);
365-
let free_id_outlive = DestructionScopeData::new(free_id);
364+
let free_id_outlive = fcx.tcx().region_maps.item_extent(free_id);
366365
let sig = fcx.tcx().liberate_late_bound_regions(free_id_outlive, &fty.sig);
367366

368367
for &input_ty in &sig.inputs {

src/librustc_typeck/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,7 @@ fn check_method_self_type<'a, 'tcx, RS:RegionScope>(
23112311
_ => typ,
23122312
};
23132313

2314-
let body_scope = region::DestructionScopeData::new(body_id);
2314+
let body_scope = tcx.region_maps.item_extent(body_id);
23152315

23162316
// "Required type" comes from the trait definition. It may
23172317
// contain late-bound regions from the method, but not the
@@ -2363,7 +2363,7 @@ fn check_method_self_type<'a, 'tcx, RS:RegionScope>(
23632363

23642364
fn liberate_early_bound_regions<'tcx,T>(
23652365
tcx: &ty::ctxt<'tcx>,
2366-
scope: region::DestructionScopeData,
2366+
scope: region::CodeExtent,
23672367
value: &T)
23682368
-> T
23692369
where T : TypeFoldable<'tcx>

0 commit comments

Comments
 (0)