Skip to content
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

Remove RefCell from the definition and some uses of DefMap #29581

Merged
merged 4 commits into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/middle/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,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
8 changes: 4 additions & 4 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,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[1..].to_vec())
} else {
None
Expand Down Expand Up @@ -1073,7 +1073,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 @@ -1086,7 +1086,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,

for pat in pats {
front_util::walk_pat(&**pat, |p| {
if pat_is_binding(def_map, &*p) {
if pat_is_binding(&def_map.borrow(), &*p) {
match p.node {
hir::PatIdent(hir::BindByValue(_), _, ref sub) => {
let pat_ty = tcx.node_id_to_type(p.id);
Expand Down Expand Up @@ -1181,7 +1181,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 @@ -237,7 +237,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
fn visit_expr(&mut self, e: &'ast hir::Expr) {
match e.node {
hir::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(DefAssociatedConst(def_id)) |
Some(DefConst(def_id)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
fn visit_arm(&mut self, arm: &hir::Arm) {
if arm.pats.len() == 1 {
let pat = &*arm.pats[0];
let variants = pat_util::necessary_variants(&self.tcx.def_map, pat);
let variants = pat_util::necessary_variants(&self.tcx.def_map.borrow(), pat);

// Inside the body, ignore constructions of variants
// necessary for the pattern to match. Those construction sites
Expand All @@ -270,7 +270,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
hir::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 rustc_front::hir;

use std::cell::RefCell;

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Def {
DefFn(DefId, bool /* is_ctor */),
Expand Down Expand Up @@ -103,7 +101,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
4 changes: 2 additions & 2 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
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 {
hir::PatIdent(hir::BindByRef(_), _, _) =>
mode.lub(BorrowingMatch),
Expand Down Expand Up @@ -969,7 +969,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
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
26 changes: 14 additions & 12 deletions src/librustc/middle/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ use rustc_front::hir;
use rustc_front::util::walk_pat;
use syntax::codemap::{respan, Span, Spanned, DUMMY_SP};

use std::cell::RefCell;

pub type PatIdMap = FnvHashMap<ast::Name, ast::NodeId>;

// This is used because same-named variables in alternative patterns need to
// use the NodeId of their namesake in the first pattern.
pub fn pat_id_map(dm: &DefMap, pat: &hir::Pat) -> PatIdMap {
pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
let mut map = FnvHashMap();
pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
map.insert(path1.node, p_id);
Expand All @@ -36,7 +38,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
hir::PatEnum(_, _) |
hir::PatIdent(_, _, None) |
hir::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 @@ -51,7 +53,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
hir::PatEnum(_, _) |
hir::PatIdent(_, _, None) |
hir::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 @@ -63,7 +65,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
match dm.get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
_ => false
}
Expand All @@ -77,7 +79,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
match pat.node {
hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
match dm.borrow().get(&pat.id)
match dm.get(&pat.id)
.and_then(|d| if d.depth == 0 { Some(d.base_def) }
else { None } ) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
Expand Down Expand Up @@ -108,12 +110,12 @@ pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {

/// Call `it` on every "binding" in a pattern, e.g., on `a` in
/// `match foo() { Some(a) => (), None => () }`
pub fn pat_bindings<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Name>),
{
walk_pat(pat, |p| {
match p.node {
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
}
_ => {}
Expand All @@ -122,12 +124,12 @@ pub fn pat_bindings<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
});
}

pub fn pat_bindings_hygienic<I>(dm: &DefMap, pat: &hir::Pat, mut it: I) where
pub fn pat_bindings_hygienic<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned<ast::Ident>),
{
walk_pat(pat, |p| {
match p.node {
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
}
_ => {}
Expand All @@ -153,7 +155,7 @@ pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {

/// Checks if the pattern contains any `ref` or `ref mut` bindings,
/// and if yes wether its containing mutable ones or just immutables ones.
pub fn pat_contains_ref_binding(dm: &DefMap, pat: &hir::Pat) -> Option<hir::Mutability> {
pub fn pat_contains_ref_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Option<hir::Mutability> {
let mut result = None;
pat_bindings(dm, pat, |mode, _, _, _| {
match mode {
Expand All @@ -172,7 +174,7 @@ pub fn pat_contains_ref_binding(dm: &DefMap, pat: &hir::Pat) -> Option<hir::Muta

/// Checks if the patterns for this arm contain any `ref` or `ref mut`
/// bindings, and if yes wether its containing mutable ones or just immutables ones.
pub fn arm_contains_ref_binding(dm: &DefMap, arm: &hir::Arm) -> Option<hir::Mutability> {
pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<hir::Mutability> {
arm.pats.iter()
.filter_map(|pat| pat_contains_ref_binding(dm, pat))
.max_by(|m| match *m {
Expand Down Expand Up @@ -226,7 +228,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
hir::PatEnum(_, _) |
hir::PatIdent(_, _, None) |
hir::PatStruct(..) => {
match dm.borrow().get(&p.id) {
match dm.get(&p.id) {
Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
variants.push(id);
}
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 @@ -205,7 +205,7 @@ impl<'a, 'v> Visitor<'v> for LifetimeContext<'a> {
hir::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
4 changes: 2 additions & 2 deletions src/librustc/middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub struct ctxt<'tcx> {
pub types: CommonTypes<'tcx>,

pub sess: &'tcx Session,
pub def_map: DefMap,
pub def_map: RefCell<DefMap>,

pub named_region_map: resolve_lifetime::NamedRegionMap,

Expand Down Expand Up @@ -453,7 +453,7 @@ impl<'tcx> ctxt<'tcx> {
/// reference to the context, to allow formatting values that need it.
pub fn create_and_enter<F, R>(s: &'tcx Session,
arenas: &'tcx CtxtArenas<'tcx>,
def_map: DefMap,
def_map: RefCell<DefMap>,
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map<'tcx>,
freevars: FreevarMap,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
syntax::ext::mtwt::clear_tables();
}

let named_region_map = time(time_passes, "lifetime resolution",
|| middle::resolve_lifetime::krate(sess, krate, &def_map));
let named_region_map = time(time_passes, "lifetime resolution", ||
middle::resolve_lifetime::krate(sess, krate, &def_map.borrow()));

time(time_passes, "looking for entry point",
|| middle::entry::find_entry_point(sess, &ast_map));
Expand All @@ -718,7 +718,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
middle::check_loop::check_crate(sess, krate));

time(time_passes, "static item recursion checking", ||
middle::check_static_recursion::check_crate(sess, krate, &def_map, &ast_map));
middle::check_static_recursion::check_crate(sess, krate, &def_map.borrow(), &ast_map));

ty::ctxt::create_and_enter(sess,
arenas,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn test_env<F>(source_string: &str,
let lang_items = lang_items::collect_language_items(&sess, &ast_map);
let resolve::CrateMap { def_map, freevars, .. } =
resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map);
let named_region_map = resolve_lifetime::krate(&sess, krate, &def_map.borrow());
let region_map = region::resolve_crate(&sess, krate);
ty::ctxt::create_and_enter(&sess,
&arenas,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/cx/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
},

hir::PatEnum(..) | hir::PatIdent(..) | hir::PatQPath(..)
if pat_is_resolved_const(&cx.tcx.def_map, self.pat) =>
if pat_is_resolved_const(&cx.tcx.def_map.borrow(), self.pat) =>
{
let def = cx.tcx.def_map.borrow().get(&self.pat.id).unwrap().full_def();
match def {
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
}

hir::PatIdent(bm, ref ident, ref sub)
if pat_is_binding(&cx.tcx.def_map, self.pat) =>
if pat_is_binding(&cx.tcx.def_map.borrow(), self.pat) =>
{
let id = match self.binding_map {
None => self.pat.id,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ pub struct Resolver<'a, 'tcx:'a> {
// The idents for the primitive types.
primitive_type_table: PrimitiveTypeTable,

def_map: DefMap,
def_map: RefCell<DefMap>,
freevars: FreevarMap,
freevars_seen: NodeMap<NodeMap<usize>>,
export_map: ExportMap,
Expand Down Expand Up @@ -4026,7 +4026,7 @@ fn module_to_string(module: &Module) -> String {


pub struct CrateMap {
pub def_map: DefMap,
pub def_map: RefCell<DefMap>,
pub freevars: FreevarMap,
pub export_map: ExportMap,
pub trait_map: TraitMap,
Expand Down
19 changes: 10 additions & 9 deletions src/librustc_trans/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ use util::nodemap::FnvHashMap;
use util::ppaux;

use std;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::fmt;
use std::rc::Rc;
Expand Down Expand Up @@ -495,7 +496,7 @@ fn expand_nested_bindings<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}

fn enter_match<'a, 'b, 'p, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
dm: &DefMap,
dm: &RefCell<DefMap>,
m: &[Match<'a, 'p, 'blk, 'tcx>],
col: usize,
val: MatchInput,
Expand All @@ -516,7 +517,7 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
let mut bound_ptrs = br.bound_ptrs.clone();
match this.node {
hir::PatIdent(_, ref path, None) => {
if pat_is_binding(dm, &*this) {
if pat_is_binding(&dm.borrow(), &*this) {
bound_ptrs.push((path.node.name, val.val));
}
}
Expand All @@ -541,7 +542,7 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
}

fn enter_default<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
dm: &DefMap,
dm: &RefCell<DefMap>,
m: &[Match<'a, 'p, 'blk, 'tcx>],
col: usize,
val: MatchInput)
Expand All @@ -555,7 +556,7 @@ fn enter_default<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,

// Collect all of the matches that can match against anything.
enter_match(bcx, dm, m, col, val, |pats| {
if pat_is_binding_or_wild(dm, &*pats[col]) {
if pat_is_binding_or_wild(&dm.borrow(), &*pats[col]) {
let mut r = pats[..col].to_vec();
r.push_all(&pats[col + 1..]);
Some(r)
Expand Down Expand Up @@ -596,7 +597,7 @@ fn enter_default<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
fn enter_opt<'a, 'p, 'blk, 'tcx>(
bcx: Block<'blk, 'tcx>,
_: ast::NodeId,
dm: &DefMap,
dm: &RefCell<DefMap>,
m: &[Match<'a, 'p, 'blk, 'tcx>],
opt: &Opt,
col: usize,
Expand Down Expand Up @@ -842,11 +843,11 @@ impl FailureHandler {
}
}

fn pick_column_to_specialize(def_map: &DefMap, m: &[Match]) -> Option<usize> {
fn pat_score(def_map: &DefMap, pat: &hir::Pat) -> usize {
fn pick_column_to_specialize(def_map: &RefCell<DefMap>, m: &[Match]) -> Option<usize> {
fn pat_score(def_map: &RefCell<DefMap>, pat: &hir::Pat) -> usize {
match pat.node {
hir::PatIdent(_, _, Some(ref inner)) => pat_score(def_map, &**inner),
_ if pat_is_refutable(def_map, pat) => 1,
_ if pat_is_refutable(&def_map.borrow(), pat) => 1,
_ => 0
}
}
Expand Down Expand Up @@ -1800,7 +1801,7 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let ccx = bcx.ccx();
match pat.node {
hir::PatIdent(pat_binding_mode, ref path1, ref inner) => {
if pat_is_binding(&tcx.def_map, &*pat) {
if pat_is_binding(&tcx.def_map.borrow(), &*pat) {
// Allocate the stack slot where the value of this
// binding will live and place it into the appropriate
// map.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/debuginfo/create_scope_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn walk_pattern(cx: &CrateContext,

// Check if this is a binding. If so we need to put it on the
// scope stack and maybe introduce an artificial scope
if pat_util::pat_is_binding(def_map, &*pat) {
if pat_util::pat_is_binding(&def_map.borrow(), &*pat) {

let name = path1.node.name;

Expand Down
Loading