Skip to content

Commit 119c38e

Browse files
cramertjnikomatsakis
authored andcommitted
Remove RefCells from RegionMaps
1 parent eff39b7 commit 119c38e

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

src/librustc/middle/region.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use session::Session;
2121
use util::nodemap::{FxHashMap, NodeMap, NodeSet};
2222
use ty;
2323

24-
use std::cell::RefCell;
2524
use std::collections::hash_map::Entry;
2625
use std::fmt;
2726
use std::mem;
@@ -50,7 +49,7 @@ impl fmt::Debug for CodeExtent {
5049
let region_maps = tcx.region_maps();
5150
{
5251
let code_extents = &region_maps.code_extents;
53-
if let Some(data) = code_extents.borrow().get(self.0 as usize) {
52+
if let Some(data) = code_extents.get(self.0 as usize) {
5453
write!(f, "/{:?}", data)?;
5554
}
5655
mem::drop(code_extents); // FIXME why is this necessary?
@@ -258,34 +257,34 @@ impl CodeExtent {
258257

259258
/// The region maps encode information about region relationships.
260259
pub struct RegionMaps {
261-
code_extents: RefCell<Vec<CodeExtentData>>,
262-
code_extent_interner: RefCell<FxHashMap<CodeExtentData, CodeExtent>>,
260+
code_extents: Vec<CodeExtentData>,
261+
code_extent_interner: FxHashMap<CodeExtentData, CodeExtent>,
263262
/// `scope_map` maps from a scope id to the enclosing scope id;
264263
/// this is usually corresponding to the lexical nesting, though
265264
/// in the case of closures the parent scope is the innermost
266265
/// conditional expression or repeating block. (Note that the
267266
/// enclosing scope id for the block associated with a closure is
268267
/// the closure itself.)
269-
scope_map: RefCell<Vec<CodeExtent>>,
268+
scope_map: Vec<CodeExtent>,
270269

271270
/// `var_map` maps from a variable or binding id to the block in
272271
/// which that variable is declared.
273-
var_map: RefCell<NodeMap<CodeExtent>>,
272+
var_map: NodeMap<CodeExtent>,
274273

275274
/// `rvalue_scopes` includes entries for those expressions whose cleanup scope is
276275
/// larger than the default. The map goes from the expression id
277276
/// to the cleanup scope id. For rvalues not present in this
278277
/// table, the appropriate cleanup scope is the innermost
279278
/// enclosing statement, conditional expression, or repeating
280279
/// block (see `terminating_scopes`).
281-
rvalue_scopes: RefCell<NodeMap<CodeExtent>>,
280+
rvalue_scopes: NodeMap<CodeExtent>,
282281

283282
/// Records the value of rvalue scopes before they were shrunk by
284283
/// #36082, for error reporting.
285284
///
286285
/// FIXME: this should be temporary. Remove this by 1.18.0 or
287286
/// so.
288-
shrunk_rvalue_scopes: RefCell<NodeMap<CodeExtent>>,
287+
shrunk_rvalue_scopes: NodeMap<CodeExtent>,
289288

290289
/// Encodes the hierarchy of fn bodies. Every fn body (including
291290
/// closures) forms its own distinct region hierarchy, rooted in
@@ -297,7 +296,7 @@ pub struct RegionMaps {
297296
/// closure defined by that fn. See the "Modeling closures"
298297
/// section of the README in infer::region_inference for
299298
/// more details.
300-
fn_tree: RefCell<NodeMap<ast::NodeId>>,
299+
fn_tree: NodeMap<ast::NodeId>,
301300
}
302301

303302
#[derive(Debug, Copy, Clone)]
@@ -321,7 +320,7 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
321320
sess: &'a Session,
322321

323322
// Generated maps:
324-
region_maps: &'a RegionMaps,
323+
region_maps: &'a mut RegionMaps,
325324

326325
cx: Context,
327326

@@ -354,11 +353,11 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
354353
impl RegionMaps {
355354
/// create a bogus code extent for the regions in astencode types. Nobody
356355
/// really cares about the contents of these.
357-
pub fn bogus_code_extent(&self, e: CodeExtentData) -> CodeExtent {
356+
pub fn bogus_code_extent(&mut self, e: CodeExtentData) -> CodeExtent {
358357
self.intern_code_extent(e, DUMMY_CODE_EXTENT)
359358
}
360359
pub fn lookup_code_extent(&self, e: CodeExtentData) -> CodeExtent {
361-
match self.code_extent_interner.borrow().get(&e) {
360+
match self.code_extent_interner.get(&e) {
362361
Some(&d) => d,
363362
None => bug!("unknown code extent {:?}", e)
364363
}
@@ -375,12 +374,12 @@ impl RegionMaps {
375374
self.lookup_code_extent(CodeExtentData::CallSiteScope { fn_id: fn_id, body_id: body_id })
376375
}
377376
pub fn opt_destruction_extent(&self, n: ast::NodeId) -> Option<CodeExtent> {
378-
self.code_extent_interner.borrow().get(&CodeExtentData::DestructionScope(n)).cloned()
377+
self.code_extent_interner.get(&CodeExtentData::DestructionScope(n)).cloned()
379378
}
380-
pub fn intern_code_extent(&self,
379+
pub fn intern_code_extent(&mut self,
381380
e: CodeExtentData,
382381
parent: CodeExtent) -> CodeExtent {
383-
match self.code_extent_interner.borrow_mut().entry(e) {
382+
match self.code_extent_interner.entry(e) {
384383
Entry::Occupied(o) => {
385384
// this can happen when the bogus code extents from tydecode
386385
// have (bogus) NodeId-s that overlap items created during
@@ -392,91 +391,90 @@ impl RegionMaps {
392391
info!("CodeExtent({}) = {:?} [parent={}] BOGUS!",
393392
idx.0, e, parent.0);
394393
} else {
395-
assert_eq!(self.scope_map.borrow()[idx.0 as usize],
394+
assert_eq!(self.scope_map[idx.0 as usize],
396395
DUMMY_CODE_EXTENT);
397396
info!("CodeExtent({}) = {:?} [parent={}] RECLAIMED!",
398397
idx.0, e, parent.0);
399-
self.scope_map.borrow_mut()[idx.0 as usize] = parent;
398+
self.scope_map[idx.0 as usize] = parent;
400399
}
401400
idx
402401
}
403402
Entry::Vacant(v) => {
404-
if self.code_extents.borrow().len() > 0xffffffffusize {
403+
if self.code_extents.len() > 0xffffffffusize {
405404
bug!() // should pass a sess,
406405
// but this isn't the only place
407406
}
408-
let idx = CodeExtent(self.code_extents.borrow().len() as u32);
407+
let idx = CodeExtent(self.code_extents.len() as u32);
409408
debug!("CodeExtent({}) = {:?} [parent={}]", idx.0, e, parent.0);
410-
self.code_extents.borrow_mut().push(e);
411-
self.scope_map.borrow_mut().push(parent);
409+
self.code_extents.push(e);
410+
self.scope_map.push(parent);
412411
*v.insert(idx)
413412
}
414413
}
415414
}
416-
pub fn intern_node(&self,
415+
pub fn intern_node(&mut self,
417416
n: ast::NodeId,
418417
parent: CodeExtent) -> CodeExtent {
419418
self.intern_code_extent(CodeExtentData::Misc(n), parent)
420419
}
421420
pub fn code_extent_data(&self, e: CodeExtent) -> CodeExtentData {
422-
self.code_extents.borrow()[e.0 as usize]
421+
self.code_extents[e.0 as usize]
423422
}
424423
pub fn each_encl_scope<E>(&self, mut e:E) where E: FnMut(&CodeExtent, &CodeExtent) {
425-
for child_id in 1..self.code_extents.borrow().len() {
424+
for child_id in 1..self.code_extents.len() {
426425
let child = CodeExtent(child_id as u32);
427426
if let Some(parent) = self.opt_encl_scope(child) {
428427
e(&child, &parent)
429428
}
430429
}
431430
}
432431
pub fn each_var_scope<E>(&self, mut e:E) where E: FnMut(&ast::NodeId, &CodeExtent) {
433-
for (child, parent) in self.var_map.borrow().iter() {
432+
for (child, parent) in self.var_map.iter() {
434433
e(child, parent)
435434
}
436435
}
437436

438437
/// Records that `sub_fn` is defined within `sup_fn`. These ids
439438
/// should be the id of the block that is the fn body, which is
440439
/// also the root of the region hierarchy for that fn.
441-
fn record_fn_parent(&self, sub_fn: ast::NodeId, sup_fn: ast::NodeId) {
440+
fn record_fn_parent(&mut self, sub_fn: ast::NodeId, sup_fn: ast::NodeId) {
442441
debug!("record_fn_parent(sub_fn={:?}, sup_fn={:?})", sub_fn, sup_fn);
443442
assert!(sub_fn != sup_fn);
444-
let previous = self.fn_tree.borrow_mut().insert(sub_fn, sup_fn);
443+
let previous = self.fn_tree.insert(sub_fn, sup_fn);
445444
assert!(previous.is_none());
446445
}
447446

448447
fn fn_is_enclosed_by(&self, mut sub_fn: ast::NodeId, sup_fn: ast::NodeId) -> bool {
449-
let fn_tree = self.fn_tree.borrow();
450448
loop {
451449
if sub_fn == sup_fn { return true; }
452-
match fn_tree.get(&sub_fn) {
450+
match self.fn_tree.get(&sub_fn) {
453451
Some(&s) => { sub_fn = s; }
454452
None => { return false; }
455453
}
456454
}
457455
}
458456

459-
fn record_var_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
457+
fn record_var_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
460458
debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
461459
assert!(var != lifetime.node_id(self));
462-
self.var_map.borrow_mut().insert(var, lifetime);
460+
self.var_map.insert(var, lifetime);
463461
}
464462

465-
fn record_rvalue_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
463+
fn record_rvalue_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
466464
debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
467465
assert!(var != lifetime.node_id(self));
468-
self.rvalue_scopes.borrow_mut().insert(var, lifetime);
466+
self.rvalue_scopes.insert(var, lifetime);
469467
}
470468

471-
fn record_shrunk_rvalue_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
469+
fn record_shrunk_rvalue_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
472470
debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
473471
assert!(var != lifetime.node_id(self));
474-
self.shrunk_rvalue_scopes.borrow_mut().insert(var, lifetime);
472+
self.shrunk_rvalue_scopes.insert(var, lifetime);
475473
}
476474

477475
pub fn opt_encl_scope(&self, id: CodeExtent) -> Option<CodeExtent> {
478476
//! Returns the narrowest scope that encloses `id`, if any.
479-
self.scope_map.borrow()[id.0 as usize].into_option()
477+
self.scope_map[id.0 as usize].into_option()
480478
}
481479

482480
#[allow(dead_code)] // used in cfg
@@ -487,15 +485,15 @@ impl RegionMaps {
487485

488486
/// Returns the lifetime of the local variable `var_id`
489487
pub fn var_scope(&self, var_id: ast::NodeId) -> CodeExtent {
490-
match self.var_map.borrow().get(&var_id) {
488+
match self.var_map.get(&var_id) {
491489
Some(&r) => r,
492490
None => { bug!("no enclosing scope for id {:?}", var_id); }
493491
}
494492
}
495493

496494
pub fn temporary_scope2(&self, expr_id: ast::NodeId) -> (Option<CodeExtent>, bool) {
497495
let temporary_scope = self.temporary_scope(expr_id);
498-
let was_shrunk = match self.shrunk_rvalue_scopes.borrow().get(&expr_id) {
496+
let was_shrunk = match self.shrunk_rvalue_scopes.get(&expr_id) {
499497
Some(&s) => {
500498
info!("temporary_scope2({:?}, scope={:?}, shrunk={:?})",
501499
expr_id, temporary_scope, s);
@@ -513,21 +511,21 @@ impl RegionMaps {
513511
let temporary_scope = self.temporary_scope(expr_id);
514512
(temporary_scope,
515513
self.shrunk_rvalue_scopes
516-
.borrow().get(&expr_id).cloned()
514+
.get(&expr_id).cloned()
517515
.or(temporary_scope))
518516
}
519517

520518
pub fn temporary_scope(&self, expr_id: ast::NodeId) -> Option<CodeExtent> {
521519
//! Returns the scope when temp created by expr_id will be cleaned up
522520
523521
// check for a designated rvalue scope
524-
if let Some(&s) = self.rvalue_scopes.borrow().get(&expr_id) {
522+
if let Some(&s) = self.rvalue_scopes.get(&expr_id) {
525523
debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
526524
return Some(s);
527525
}
528526

529-
let scope_map : &[CodeExtent] = &self.scope_map.borrow();
530-
let code_extents: &[CodeExtentData] = &self.code_extents.borrow();
527+
let scope_map : &[CodeExtent] = &self.scope_map;
528+
let code_extents: &[CodeExtentData] = &self.code_extents;
531529

532530
// else, locate the innermost terminating scope
533531
// if there's one. Static items, for instance, won't
@@ -601,7 +599,7 @@ impl RegionMaps {
601599
let mut a_vec: Vec<CodeExtent> = vec![];
602600
let mut b_buf: [CodeExtent; 32] = [ROOT_CODE_EXTENT; 32];
603601
let mut b_vec: Vec<CodeExtent> = vec![];
604-
let scope_map : &[CodeExtent] = &self.scope_map.borrow();
602+
let scope_map : &[CodeExtent] = &self.scope_map;
605603
let a_ancestors = ancestors_of(scope_map,
606604
scope_a, &mut a_buf, &mut a_vec);
607605
let b_ancestors = ancestors_of(scope_map,
@@ -1216,7 +1214,7 @@ impl<'hir, 'a> RegionResolutionVisitor<'hir, 'a> {
12161214
// functions put their destruction scopes *inside* their parameter
12171215
// scopes.
12181216
let scope = CodeExtentData::DestructionScope(id);
1219-
if !self.region_maps.code_extent_interner.borrow().contains_key(&scope) {
1217+
if !self.region_maps.code_extent_interner.contains_key(&scope) {
12201218
self.region_maps.intern_code_extent(scope, ROOT_CODE_EXTENT);
12211219
}
12221220
}
@@ -1278,14 +1276,14 @@ fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateN
12781276

12791277
let krate = hir_map.krate();
12801278

1281-
let maps = RegionMaps {
1282-
code_extents: RefCell::new(vec![]),
1283-
code_extent_interner: RefCell::new(FxHashMap()),
1284-
scope_map: RefCell::new(vec![]),
1285-
var_map: RefCell::new(NodeMap()),
1286-
rvalue_scopes: RefCell::new(NodeMap()),
1287-
shrunk_rvalue_scopes: RefCell::new(NodeMap()),
1288-
fn_tree: RefCell::new(NodeMap()),
1279+
let mut maps = RegionMaps {
1280+
code_extents: vec![],
1281+
code_extent_interner: FxHashMap(),
1282+
scope_map: vec![],
1283+
var_map: NodeMap(),
1284+
rvalue_scopes: NodeMap(),
1285+
shrunk_rvalue_scopes: NodeMap(),
1286+
fn_tree: NodeMap(),
12891287
};
12901288
let root_extent = maps.bogus_code_extent(
12911289
CodeExtentData::DestructionScope(ast::DUMMY_NODE_ID));
@@ -1296,7 +1294,7 @@ fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateN
12961294
{
12971295
let mut visitor = RegionResolutionVisitor {
12981296
sess: sess,
1299-
region_maps: &maps,
1297+
region_maps: &mut maps,
13001298
map: hir_map,
13011299
cx: Context {
13021300
root_id: None,

0 commit comments

Comments
 (0)