Skip to content

Commit 98e0881

Browse files
committed
Remove RefCells from RegionMaps
1 parent ed93010 commit 98e0881

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;
@@ -51,7 +50,7 @@ impl fmt::Debug for CodeExtent {
5150
let region_maps = tcx.region_maps();
5251
{
5352
let code_extents = &region_maps.code_extents;
54-
if let Some(data) = code_extents.borrow().get(self.0 as usize) {
53+
if let Some(data) = code_extents.get(self.0 as usize) {
5554
write!(f, "/{:?}", data)?;
5655
}
5756
mem::drop(code_extents); // FIXME why is this necessary?
@@ -259,34 +258,34 @@ impl CodeExtent {
259258

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

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

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

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

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

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

324323
// Generated maps:
325-
region_maps: &'a RegionMaps,
324+
region_maps: &'a mut RegionMaps,
326325

327326
cx: Context,
328327

@@ -355,11 +354,11 @@ struct RegionResolutionVisitor<'hir: 'a, 'a> {
355354
impl RegionMaps {
356355
/// create a bogus code extent for the regions in astencode types. Nobody
357356
/// really cares about the contents of these.
358-
pub fn bogus_code_extent(&self, e: CodeExtentData) -> CodeExtent {
357+
pub fn bogus_code_extent(&mut self, e: CodeExtentData) -> CodeExtent {
359358
self.intern_code_extent(e, DUMMY_CODE_EXTENT)
360359
}
361360
pub fn lookup_code_extent(&self, e: CodeExtentData) -> CodeExtent {
362-
match self.code_extent_interner.borrow().get(&e) {
361+
match self.code_extent_interner.get(&e) {
363362
Some(&d) => d,
364363
None => bug!("unknown code extent {:?}", e)
365364
}
@@ -376,12 +375,12 @@ impl RegionMaps {
376375
self.lookup_code_extent(CodeExtentData::CallSiteScope { fn_id: fn_id, body_id: body_id })
377376
}
378377
pub fn opt_destruction_extent(&self, n: ast::NodeId) -> Option<CodeExtent> {
379-
self.code_extent_interner.borrow().get(&CodeExtentData::DestructionScope(n)).cloned()
378+
self.code_extent_interner.get(&CodeExtentData::DestructionScope(n)).cloned()
380379
}
381-
pub fn intern_code_extent(&self,
380+
pub fn intern_code_extent(&mut self,
382381
e: CodeExtentData,
383382
parent: CodeExtent) -> CodeExtent {
384-
match self.code_extent_interner.borrow_mut().entry(e) {
383+
match self.code_extent_interner.entry(e) {
385384
Entry::Occupied(o) => {
386385
// this can happen when the bogus code extents from tydecode
387386
// have (bogus) NodeId-s that overlap items created during
@@ -393,91 +392,90 @@ impl RegionMaps {
393392
info!("CodeExtent({}) = {:?} [parent={}] BOGUS!",
394393
idx.0, e, parent.0);
395394
} else {
396-
assert_eq!(self.scope_map.borrow()[idx.0 as usize],
395+
assert_eq!(self.scope_map[idx.0 as usize],
397396
DUMMY_CODE_EXTENT);
398397
info!("CodeExtent({}) = {:?} [parent={}] RECLAIMED!",
399398
idx.0, e, parent.0);
400-
self.scope_map.borrow_mut()[idx.0 as usize] = parent;
399+
self.scope_map[idx.0 as usize] = parent;
401400
}
402401
idx
403402
}
404403
Entry::Vacant(v) => {
405-
if self.code_extents.borrow().len() > 0xffffffffusize {
404+
if self.code_extents.len() > 0xffffffffusize {
406405
bug!() // should pass a sess,
407406
// but this isn't the only place
408407
}
409-
let idx = CodeExtent(self.code_extents.borrow().len() as u32);
408+
let idx = CodeExtent(self.code_extents.len() as u32);
410409
debug!("CodeExtent({}) = {:?} [parent={}]", idx.0, e, parent.0);
411-
self.code_extents.borrow_mut().push(e);
412-
self.scope_map.borrow_mut().push(parent);
410+
self.code_extents.push(e);
411+
self.scope_map.push(parent);
413412
*v.insert(idx)
414413
}
415414
}
416415
}
417-
pub fn intern_node(&self,
416+
pub fn intern_node(&mut self,
418417
n: ast::NodeId,
419418
parent: CodeExtent) -> CodeExtent {
420419
self.intern_code_extent(CodeExtentData::Misc(n), parent)
421420
}
422421
pub fn code_extent_data(&self, e: CodeExtent) -> CodeExtentData {
423-
self.code_extents.borrow()[e.0 as usize]
422+
self.code_extents[e.0 as usize]
424423
}
425424
pub fn each_encl_scope<E>(&self, mut e:E) where E: FnMut(&CodeExtent, &CodeExtent) {
426-
for child_id in 1..self.code_extents.borrow().len() {
425+
for child_id in 1..self.code_extents.len() {
427426
let child = CodeExtent(child_id as u32);
428427
if let Some(parent) = self.opt_encl_scope(child) {
429428
e(&child, &parent)
430429
}
431430
}
432431
}
433432
pub fn each_var_scope<E>(&self, mut e:E) where E: FnMut(&ast::NodeId, &CodeExtent) {
434-
for (child, parent) in self.var_map.borrow().iter() {
433+
for (child, parent) in self.var_map.iter() {
435434
e(child, parent)
436435
}
437436
}
438437

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

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

460-
fn record_var_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
458+
fn record_var_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
461459
debug!("record_var_scope(sub={:?}, sup={:?})", var, lifetime);
462460
assert!(var != lifetime.node_id(self));
463-
self.var_map.borrow_mut().insert(var, lifetime);
461+
self.var_map.insert(var, lifetime);
464462
}
465463

466-
fn record_rvalue_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
464+
fn record_rvalue_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
467465
debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
468466
assert!(var != lifetime.node_id(self));
469-
self.rvalue_scopes.borrow_mut().insert(var, lifetime);
467+
self.rvalue_scopes.insert(var, lifetime);
470468
}
471469

472-
fn record_shrunk_rvalue_scope(&self, var: ast::NodeId, lifetime: CodeExtent) {
470+
fn record_shrunk_rvalue_scope(&mut self, var: ast::NodeId, lifetime: CodeExtent) {
473471
debug!("record_rvalue_scope(sub={:?}, sup={:?})", var, lifetime);
474472
assert!(var != lifetime.node_id(self));
475-
self.shrunk_rvalue_scopes.borrow_mut().insert(var, lifetime);
473+
self.shrunk_rvalue_scopes.insert(var, lifetime);
476474
}
477475

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

483481
#[allow(dead_code)] // used in cfg
@@ -488,15 +486,15 @@ impl RegionMaps {
488486

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

497495
pub fn temporary_scope2(&self, expr_id: ast::NodeId) -> (Option<CodeExtent>, bool) {
498496
let temporary_scope = self.temporary_scope(expr_id);
499-
let was_shrunk = match self.shrunk_rvalue_scopes.borrow().get(&expr_id) {
497+
let was_shrunk = match self.shrunk_rvalue_scopes.get(&expr_id) {
500498
Some(&s) => {
501499
info!("temporary_scope2({:?}, scope={:?}, shrunk={:?})",
502500
expr_id, temporary_scope, s);
@@ -514,21 +512,21 @@ impl RegionMaps {
514512
let temporary_scope = self.temporary_scope(expr_id);
515513
(temporary_scope,
516514
self.shrunk_rvalue_scopes
517-
.borrow().get(&expr_id).cloned()
515+
.get(&expr_id).cloned()
518516
.or(temporary_scope))
519517
}
520518

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

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

533531
// else, locate the innermost terminating scope
534532
// if there's one. Static items, for instance, won't
@@ -602,7 +600,7 @@ impl RegionMaps {
602600
let mut a_vec: Vec<CodeExtent> = vec![];
603601
let mut b_buf: [CodeExtent; 32] = [ROOT_CODE_EXTENT; 32];
604602
let mut b_vec: Vec<CodeExtent> = vec![];
605-
let scope_map : &[CodeExtent] = &self.scope_map.borrow();
603+
let scope_map : &[CodeExtent] = &self.scope_map;
606604
let a_ancestors = ancestors_of(scope_map,
607605
scope_a, &mut a_buf, &mut a_vec);
608606
let b_ancestors = ancestors_of(scope_map,
@@ -1217,7 +1215,7 @@ impl<'hir, 'a> RegionResolutionVisitor<'hir, 'a> {
12171215
// functions put their destruction scopes *inside* their parameter
12181216
// scopes.
12191217
let scope = CodeExtentData::DestructionScope(id);
1220-
if !self.region_maps.code_extent_interner.borrow().contains_key(&scope) {
1218+
if !self.region_maps.code_extent_interner.contains_key(&scope) {
12211219
self.region_maps.intern_code_extent(scope, ROOT_CODE_EXTENT);
12221220
}
12231221
}
@@ -1279,14 +1277,14 @@ fn region_resolve_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateN
12791277

12801278
let krate = hir_map.krate();
12811279

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

0 commit comments

Comments
 (0)