Skip to content

Remove the wrapper RefCell from around DefMap #24096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
let guard_exit = self.expr(&**guard, guard_start);

let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
&self.tcx.def_map, &**pat);
&self.tcx.def_map.borrow(), &**pat);

// If both this pattern and the previous pattern
// were free of bindings, they must consist only
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ fn is_useful(cx: &MatchCheckCtxt,

Some(constructor) => {
let matrix = rows.iter().filter_map(|r| {
if pat_is_binding_or_wild(&cx.tcx.def_map, raw_pat(r[0])) {
if pat_is_binding_or_wild(&cx.tcx.def_map.borrow(), raw_pat(r[0])) {
Some(r.tail().to_vec())
} else {
None
Expand Down Expand Up @@ -1069,7 +1069,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
let def_map = &tcx.def_map;
let mut by_ref_span = None;
for pat in pats {
pat_bindings(def_map, &**pat, |bm, _, span, _path| {
pat_bindings(&def_map.borrow(), &**pat, |bm, _, span, _path| {
match bm {
ast::BindByRef(_) => {
by_ref_span = Some(span);
Expand All @@ -1084,7 +1084,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
// check legality of moving out of the enum

// x @ Foo(..) is legal, but x @ Foo(y) isn't.
if sub.map_or(false, |p| pat_contains_bindings(def_map, &*p)) {
if sub.map_or(false, |p| pat_contains_bindings(&def_map.borrow(), &*p)) {
span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings");
} else if has_guard {
span_err!(cx.tcx.sess, p.span, E0008, "cannot bind by-move into a pattern guard");
Expand All @@ -1097,7 +1097,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,

for pat in pats {
ast_util::walk_pat(&**pat, |p| {
if pat_is_binding(def_map, &*p) {
if pat_is_binding(&def_map.borrow(), &*p) {
match p.node {
ast::PatIdent(ast::BindByValue(_), _, ref sub) => {
let pat_ty = ty::node_id_to_type(tcx, p.id);
Expand Down Expand Up @@ -1182,7 +1182,7 @@ struct AtBindingPatternVisitor<'a, 'b:'a, 'tcx:'b> {

impl<'a, 'b, 'tcx, 'v> Visitor<'v> for AtBindingPatternVisitor<'a, 'b, 'tcx> {
fn visit_pat(&mut self, pat: &Pat) {
if !self.bindings_allowed && pat_is_binding(&self.cx.tcx.def_map, pat) {
if !self.bindings_allowed && pat_is_binding(&self.cx.tcx.def_map.borrow(), pat) {
span_err!(self.cx.tcx.sess, pat.span, E0303,
"pattern bindings are not allowed \
after an `@`");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/check_static_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'a, 'ast, 'v> Visitor<'v> for CheckItemRecursionVisitor<'a, 'ast> {
fn visit_expr(&mut self, e: &ast::Expr) {
match e.node {
ast::ExprPath(..) => {
match self.def_map.borrow().get(&e.id).map(|d| d.base_def) {
match self.def_map.get(&e.id).map(|d| d.base_def) {
Some(DefStatic(def_id, _)) |
Some(DefConst(def_id)) if
ast_util::is_local(def_id) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
ast::PatStruct(_, ref fields, _) => {
self.handle_field_pattern_match(pat, fields);
}
_ if pat_util::pat_is_const(def_map, pat) => {
_ if pat_util::pat_is_const(&def_map.borrow(), pat) => {
// it might be the only use of a const
self.lookup_and_handle_definition(&pat.id)
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use util::nodemap::NodeMap;
use syntax::ast;
use syntax::ast_util::local_def;

use std::cell::RefCell;

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Def {
DefFn(ast::DefId, bool /* is_ctor */),
Expand Down Expand Up @@ -99,7 +97,7 @@ impl PathResolution {
}

// Definition mapping
pub type DefMap = RefCell<NodeMap<PathResolution>>;
pub type DefMap = NodeMap<PathResolution>;
// This is the replacement export map. It maps a module to all of the exports
// within.
pub type ExportMap = NodeMap<Vec<Export>>;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
match local.init {
None => {
let delegate = &mut self.delegate;
pat_util::pat_bindings(&self.typer.tcx().def_map, &*local.pat,
pat_util::pat_bindings(&self.typer.tcx().def_map.borrow(), &*local.pat,
|_, id, span, _| {
delegate.decl_without_init(id, span);
})
Expand Down Expand Up @@ -969,7 +969,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
return_if_err!(self.mc.cat_pattern(cmt_discr, pat, |_mc, cmt_pat, pat| {
let tcx = self.tcx();
let def_map = &self.tcx().def_map;
if pat_util::pat_is_binding(def_map, pat) {
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
match pat.node {
ast::PatIdent(ast::BindByRef(_), _, _) =>
mode.lub(BorrowingMatch),
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
let def_map = &self.tcx().def_map;
let delegate = &mut self.delegate;
return_if_err!(mc.cat_pattern(cmt_discr.clone(), pat, |mc, cmt_pat, pat| {
if pat_util::pat_is_binding(def_map, pat) {
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
let tcx = typer.tcx();

debug!("binding cmt_pat={} pat={} match_mode={:?}",
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn visit_fn(ir: &mut IrMaps,
debug!("creating fn_maps: {:?}", &fn_maps as *const IrMaps);

for arg in &decl.inputs {
pat_util::pat_bindings(&ir.tcx.def_map,
pat_util::pat_bindings(&ir.tcx.def_map.borrow(),
&*arg.pat,
|_bm, arg_id, _x, path1| {
debug!("adding argument {}", arg_id);
Expand Down Expand Up @@ -416,7 +416,7 @@ fn visit_fn(ir: &mut IrMaps,
}

fn visit_local(ir: &mut IrMaps, local: &ast::Local) {
pat_util::pat_bindings(&ir.tcx.def_map, &*local.pat, |_, p_id, sp, path1| {
pat_util::pat_bindings(&ir.tcx.def_map.borrow(), &*local.pat, |_, p_id, sp, path1| {
debug!("adding local variable {}", p_id);
let name = path1.node;
ir.add_live_node_for_node(p_id, VarDefNode(sp));
Expand All @@ -430,7 +430,7 @@ fn visit_local(ir: &mut IrMaps, local: &ast::Local) {

fn visit_arm(ir: &mut IrMaps, arm: &ast::Arm) {
for pat in &arm.pats {
pat_util::pat_bindings(&ir.tcx.def_map, &**pat, |bm, p_id, sp, path1| {
pat_util::pat_bindings(&ir.tcx.def_map.borrow(), &**pat, |bm, p_id, sp, path1| {
debug!("adding local variable {} from match with bm {:?}",
p_id, bm);
let name = path1.node;
Expand Down Expand Up @@ -599,7 +599,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn pat_bindings<F>(&mut self, pat: &ast::Pat, mut f: F) where
F: FnMut(&mut Liveness<'a, 'tcx>, LiveNode, Variable, Span, NodeId),
{
pat_util::pat_bindings(&self.ir.tcx.def_map, pat, |_bm, p_id, sp, _n| {
pat_util::pat_bindings(&self.ir.tcx.def_map.borrow(), pat, |_bm, p_id, sp, _n| {
let ln = self.live_node(p_id, sp);
let var = self.variable(p_id, sp);
f(self, ln, var, sp, p_id);
Expand Down Expand Up @@ -1595,7 +1595,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

fn warn_about_unused_args(&self, decl: &ast::FnDecl, entry_ln: LiveNode) {
for arg in &decl.inputs {
pat_util::pat_bindings(&self.ir.tcx.def_map,
pat_util::pat_bindings(&self.ir.tcx.def_map.borrow(),
&*arg.pat,
|_bm, p_id, sp, path1| {
let var = self.variable(p_id, sp);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &ast::Pat) -> bool {
ast::PatEnum(_, _) |
ast::PatIdent(_, _, None) |
ast::PatStruct(..) => {
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(DefVariant(..)) => true,
_ => false
}
Expand All @@ -49,7 +49,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
ast::PatEnum(_, _) |
ast::PatIdent(_, _, None) |
ast::PatStruct(..) => {
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(DefVariant(..)) | Some(DefStruct(..)) => true,
_ => false
}
Expand All @@ -61,7 +61,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &ast::Pat) -> bool {
pub fn pat_is_const(dm: &DefMap, pat: &ast::Pat) -> bool {
match pat.node {
ast::PatIdent(_, _, None) | ast::PatEnum(..) => {
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) => true,
_ => false
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
ast::TyPath(None, ref path) => {
// if this path references a trait, then this will resolve to
// a trait ref, which introduces a binding scope.
match self.def_map.borrow().get(&ty.id).map(|d| (d.base_def, d.depth)) {
match self.def_map.get(&ty.id).map(|d| (d.base_def, d.depth)) {
Some((def::DefTrait(..), 0)) => {
self.with(LateScope(&Vec::new(), self.scope), |_, this| {
this.visit_path(path, ty.id);
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ pub struct ctxt<'tcx> {
pub types: CommonTypes<'tcx>,

pub sess: Session,
pub def_map: DefMap,
pub def_map: RefCell<DefMap>,

pub named_region_map: resolve_lifetime::NamedRegionMap,

Expand Down Expand Up @@ -2611,7 +2611,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
item_variance_map: RefCell::new(DefIdMap()),
variance_computed: Cell::new(false),
sess: s,
def_map: def_map,
def_map: RefCell::new(def_map),
region_maps: region_maps,
node_types: RefCell::new(FnvHashMap()),
item_substs: RefCell::new(NodeMap()),
Expand Down Expand Up @@ -2733,11 +2733,11 @@ impl<'tcx> ctxt<'tcx> {
}

pub fn pat_contains_ref_binding(&self, pat: &ast::Pat) -> bool {
pat_util::pat_contains_ref_binding(&self.def_map, pat)
pat_util::pat_contains_ref_binding(&self.def_map.borrow(), pat)
}

pub fn arm_contains_ref_binding(&self, arm: &ast::Arm) -> bool {
pat_util::arm_contains_ref_binding(&self.def_map, arm)
pat_util::arm_contains_ref_binding(&self.def_map.borrow(), arm)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ impl UnusedMut {

let mut mutables = FnvHashMap();
for p in pats {
pat_util::pat_bindings(&cx.tcx.def_map, &**p, |mode, id, _, path1| {
pat_util::pat_bindings(&cx.tcx.def_map.borrow(), &**p, |mode, id, _, path1| {
let ident = path1.node;
if let ast::BindByValue(ast::MutMutable) = mode {
if !token::get_ident(ident).starts_with("_") {
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
"unused import".to_string());
}

let mut def_map = self.def_map.borrow_mut();
let path_res = if let Some(r) = def_map.get_mut(&id) {
let path_res = if let Some(r) = self.resolver.def_map.get_mut(&id) {
r
} else {
return;
Expand All @@ -81,12 +80,12 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
}
};

let mut v_used = if self.used_imports.contains(&(id, ValueNS)) {
let mut v_used = if self.resolver.used_imports.contains(&(id, ValueNS)) {
Used
} else {
Unused
};
let t_used = if self.used_imports.contains(&(id, TypeNS)) {
let t_used = if self.resolver.used_imports.contains(&(id, TypeNS)) {
Used
} else {
Unused
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

primitive_type_table: PrimitiveTypeTable::new(),

def_map: RefCell::new(NodeMap()),
def_map: NodeMap(),
freevars: RefCell::new(NodeMap()),
freevars_seen: RefCell::new(NodeMap()),
export_map: NodeMap(),
Expand Down Expand Up @@ -1871,7 +1871,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ItemUse(ref view_path) => {
// check for imports shadowing primitive types
if let ast::ViewPathSimple(ident, _) = view_path.node {
match self.def_map.borrow().get(&item.id).map(|d| d.full_def()) {
match self.def_map.get(&item.id).map(|d| d.full_def()) {
Some(DefTy(..)) | Some(DefStruct(..)) | Some(DefTrait(..)) | None => {
self.check_if_primitive_type_name(ident.name, item.span);
}
Expand Down Expand Up @@ -2991,7 +2991,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

if allowed == Everything {
// Look for a field with the same name in the current self_type.
match self.def_map.borrow().get(&node_id).map(|d| d.full_def()) {
match self.def_map.get(&node_id).map(|d| d.full_def()) {
Some(DefTy(did, _)) |
Some(DefStruct(did)) |
Some(DefVariant(_, did, _)) => match self.structs.get(&did) {
Expand Down Expand Up @@ -3402,7 +3402,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
assert!(match resolution.last_private {LastImport{..} => false, _ => true},
"Import should only be used for `use` directives");

if let Some(prev_res) = self.def_map.borrow_mut().insert(node_id, resolution) {
if let Some(prev_res) = self.def_map.insert(node_id, resolution) {
let span = self.ast_map.opt_span(node_id).unwrap_or(codemap::DUMMY_SP);
self.session.span_bug(span, &format!("path resolved multiple times \
({:?} before, {:?} now)",
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,14 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
};

if let Some((def, _)) = value_def_and_priv {
self.resolver.def_map.borrow_mut().insert(directive.id, PathResolution {
self.resolver.def_map.insert(directive.id, PathResolution {
base_def: def,
last_private: import_lp,
depth: 0
});
}
if let Some((def, _)) = type_def_and_priv {
self.resolver.def_map.borrow_mut().insert(directive.id, PathResolution {
self.resolver.def_map.insert(directive.id, PathResolution {
base_def: def,
last_private: import_lp,
depth: 0
Expand Down Expand Up @@ -815,7 +815,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {

// Record the destination of this import
if let Some(did) = target_module.def_id.get() {
self.resolver.def_map.borrow_mut().insert(id, PathResolution {
self.resolver.def_map.insert(id, PathResolution {
base_def: DefMod(did),
last_private: lp,
depth: 0
Expand Down
Loading