From 8a69a00941ddb06254abe8633a4f721fd0576266 Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Wed, 4 Nov 2015 00:02:22 -0600 Subject: [PATCH 1/4] Unwrap the RefCell around DefMap --- src/librustc/middle/check_static_recursion.rs | 6 ++-- src/librustc/middle/def.rs | 4 +-- src/librustc/middle/pat_util.rs | 30 ++++++++++--------- src/librustc/middle/resolve_lifetime.rs | 5 ++-- src/librustc/middle/ty/context.rs | 4 +-- src/librustc_resolve/lib.rs | 4 +-- src/librustc_trans/trans/_match.rs | 11 +++---- 7 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/librustc/middle/check_static_recursion.rs b/src/librustc/middle/check_static_recursion.rs index 45671367a5c3c..cfd7d3548e065 100644 --- a/src/librustc/middle/check_static_recursion.rs +++ b/src/librustc/middle/check_static_recursion.rs @@ -27,7 +27,7 @@ use std::cell::RefCell; struct CheckCrateVisitor<'a, 'ast: 'a> { sess: &'a Session, - def_map: &'a DefMap, + def_map: &'a RefCell, ast_map: &'a ast_map::Map<'ast>, // `discriminant_map` is a cache that associates the `NodeId`s of local // variant definitions with the discriminant expression that applies to @@ -92,7 +92,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> { pub fn check_crate<'ast>(sess: &Session, krate: &'ast hir::Crate, - def_map: &DefMap, + def_map: &RefCell, ast_map: &ast_map::Map<'ast>) { let mut visitor = CheckCrateVisitor { sess: sess, @@ -108,7 +108,7 @@ struct CheckItemRecursionVisitor<'a, 'ast: 'a> { root_span: &'a Span, sess: &'a Session, ast_map: &'a ast_map::Map<'ast>, - def_map: &'a DefMap, + def_map: &'a RefCell, discriminant_map: &'a RefCell>>, idstack: Vec, } diff --git a/src/librustc/middle/def.rs b/src/librustc/middle/def.rs index ef2b918a9f5d7..b1d2418795753 100644 --- a/src/librustc/middle/def.rs +++ b/src/librustc/middle/def.rs @@ -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 */), @@ -103,7 +101,7 @@ impl PathResolution { } // Definition mapping -pub type DefMap = RefCell>; +pub type DefMap = NodeMap; // This is the replacement export map. It maps a module to all of the exports // within. pub type ExportMap = NodeMap>; diff --git a/src/librustc/middle/pat_util.rs b/src/librustc/middle/pat_util.rs index c3555273850bc..77ffd0b606ebe 100644 --- a/src/librustc/middle/pat_util.rs +++ b/src/librustc/middle/pat_util.rs @@ -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; // 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, pat: &hir::Pat) -> PatIdMap { let mut map = FnvHashMap(); pat_bindings(dm, pat, |_bm, p_id, _s, path1| { map.insert(path1.node, p_id); @@ -30,7 +32,7 @@ pub fn pat_id_map(dm: &DefMap, pat: &hir::Pat) -> PatIdMap { map } -pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_is_refutable(dm: &RefCell, pat: &hir::Pat) -> bool { match pat.node { hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true, hir::PatEnum(_, _) | @@ -46,7 +48,7 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool { } } -pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_is_variant_or_struct(dm: &RefCell, pat: &hir::Pat) -> bool { match pat.node { hir::PatEnum(_, _) | hir::PatIdent(_, _, None) | @@ -60,7 +62,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 { +pub fn pat_is_const(dm: &RefCell, 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()) { @@ -74,7 +76,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool { // Same as above, except that partially-resolved defs cause `false` to be // returned instead of a panic. -pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_is_resolved_const(dm: &RefCell, pat: &hir::Pat) -> bool { match pat.node { hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => { match dm.borrow().get(&pat.id) @@ -88,7 +90,7 @@ pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool { } } -pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_is_binding(dm: &RefCell, pat: &hir::Pat) -> bool { match pat.node { hir::PatIdent(..) => { !pat_is_variant_or_struct(dm, pat) && @@ -98,7 +100,7 @@ pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool { } } -pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_is_binding_or_wild(dm: &RefCell, pat: &hir::Pat) -> bool { match pat.node { hir::PatIdent(..) => pat_is_binding(dm, pat), hir::PatWild => true, @@ -108,7 +110,7 @@ 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(dm: &DefMap, pat: &hir::Pat, mut it: I) where +pub fn pat_bindings(dm: &RefCell, pat: &hir::Pat, mut it: I) where I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned), { walk_pat(pat, |p| { @@ -122,7 +124,7 @@ pub fn pat_bindings(dm: &DefMap, pat: &hir::Pat, mut it: I) where }); } -pub fn pat_bindings_hygienic(dm: &DefMap, pat: &hir::Pat, mut it: I) where +pub fn pat_bindings_hygienic(dm: &RefCell, pat: &hir::Pat, mut it: I) where I: FnMut(hir::BindingMode, ast::NodeId, Span, &Spanned), { walk_pat(pat, |p| { @@ -138,7 +140,7 @@ pub fn pat_bindings_hygienic(dm: &DefMap, pat: &hir::Pat, mut it: I) where /// Checks if the pattern contains any patterns that bind something to /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`. -pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool { +pub fn pat_contains_bindings(dm: &RefCell, pat: &hir::Pat) -> bool { let mut contains_bindings = false; walk_pat(pat, |p| { if pat_is_binding(dm, p) { @@ -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 { +pub fn pat_contains_ref_binding(dm: &RefCell, pat: &hir::Pat) -> Option { let mut result = None; pat_bindings(dm, pat, |mode, _, _, _| { match mode { @@ -172,7 +174,7 @@ pub fn pat_contains_ref_binding(dm: &DefMap, pat: &hir::Pat) -> Option Option { +pub fn arm_contains_ref_binding(dm: &RefCell, arm: &hir::Arm) -> Option { arm.pats.iter() .filter_map(|pat| pat_contains_ref_binding(dm, pat)) .max_by(|m| match *m { @@ -183,7 +185,7 @@ pub fn arm_contains_ref_binding(dm: &DefMap, arm: &hir::Arm) -> Option bool { +pub fn pat_contains_bindings_or_wild(dm: &RefCell, pat: &hir::Pat) -> bool { let mut contains_bindings = false; walk_pat(pat, |p| { if pat_is_binding_or_wild(dm, p) { @@ -219,7 +221,7 @@ pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path { } /// Return variants that are necessary to exist for the pattern to match. -pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec { +pub fn necessary_variants(dm: &RefCell, pat: &hir::Pat) -> Vec { let mut variants = vec![]; walk_pat(pat, |p| { match p.node { diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index d9398a1c58cd9..5035ac12a85f7 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -23,6 +23,7 @@ use middle::def::{self, DefMap}; use middle::region; use middle::subst; use middle::ty; +use std::cell::RefCell; use std::fmt; use std::mem::replace; use syntax::ast; @@ -54,7 +55,7 @@ struct LifetimeContext<'a> { sess: &'a Session, named_region_map: &'a mut NamedRegionMap, scope: Scope<'a>, - def_map: &'a DefMap, + def_map: &'a RefCell, // Deep breath. Our representation for poly trait refs contains a single // binder and thus we only allow a single level of quantification. However, // the syntax of Rust permits quantification in two places, e.g., `T: for <'a> Foo<'a>` @@ -93,7 +94,7 @@ type Scope<'a> = &'a ScopeChain<'a>; static ROOT_SCOPE: ScopeChain<'static> = RootScope; -pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &DefMap) -> NamedRegionMap { +pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &RefCell) -> NamedRegionMap { let mut named_region_map = NodeMap(); visit::walk_crate(&mut LifetimeContext { sess: sess, diff --git a/src/librustc/middle/ty/context.rs b/src/librustc/middle/ty/context.rs index 2f4e0c58a38bf..d91cac4cc75b9 100644 --- a/src/librustc/middle/ty/context.rs +++ b/src/librustc/middle/ty/context.rs @@ -228,7 +228,7 @@ pub struct ctxt<'tcx> { pub types: CommonTypes<'tcx>, pub sess: &'tcx Session, - pub def_map: DefMap, + pub def_map: RefCell, pub named_region_map: resolve_lifetime::NamedRegionMap, @@ -453,7 +453,7 @@ impl<'tcx> ctxt<'tcx> { /// reference to the context, to allow formatting values that need it. pub fn create_and_enter(s: &'tcx Session, arenas: &'tcx CtxtArenas<'tcx>, - def_map: DefMap, + def_map: RefCell, named_region_map: resolve_lifetime::NamedRegionMap, map: ast_map::Map<'tcx>, freevars: FreevarMap, diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 4f602abfbb586..e9a0efe76cb42 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -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, freevars: FreevarMap, freevars_seen: NodeMap>, export_map: ExportMap, @@ -4026,7 +4026,7 @@ fn module_to_string(module: &Module) -> String { pub struct CrateMap { - pub def_map: DefMap, + pub def_map: RefCell, pub freevars: FreevarMap, pub export_map: ExportMap, pub trait_map: TraitMap, diff --git a/src/librustc_trans/trans/_match.rs b/src/librustc_trans/trans/_match.rs index 3c53d55886565..6678d1c34a0c4 100644 --- a/src/librustc_trans/trans/_match.rs +++ b/src/librustc_trans/trans/_match.rs @@ -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; @@ -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, m: &[Match<'a, 'p, 'blk, 'tcx>], col: usize, val: MatchInput, @@ -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, m: &[Match<'a, 'p, 'blk, 'tcx>], col: usize, val: MatchInput) @@ -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, m: &[Match<'a, 'p, 'blk, 'tcx>], opt: &Opt, col: usize, @@ -842,8 +843,8 @@ impl FailureHandler { } } -fn pick_column_to_specialize(def_map: &DefMap, m: &[Match]) -> Option { - fn pat_score(def_map: &DefMap, pat: &hir::Pat) -> usize { +fn pick_column_to_specialize(def_map: &RefCell, m: &[Match]) -> Option { + fn pat_score(def_map: &RefCell, 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, From f5781f143c1dfb2913e2402d880ea136ad8da43a Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Wed, 4 Nov 2015 00:20:31 -0600 Subject: [PATCH 2/4] Remove use of RefCell in check_static_recursion --- src/librustc/middle/check_static_recursion.rs | 8 ++++---- src/librustc_driver/driver.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc/middle/check_static_recursion.rs b/src/librustc/middle/check_static_recursion.rs index cfd7d3548e065..dd49010c43672 100644 --- a/src/librustc/middle/check_static_recursion.rs +++ b/src/librustc/middle/check_static_recursion.rs @@ -27,7 +27,7 @@ use std::cell::RefCell; struct CheckCrateVisitor<'a, 'ast: 'a> { sess: &'a Session, - def_map: &'a RefCell, + def_map: &'a DefMap, ast_map: &'a ast_map::Map<'ast>, // `discriminant_map` is a cache that associates the `NodeId`s of local // variant definitions with the discriminant expression that applies to @@ -92,7 +92,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckCrateVisitor<'a, 'ast> { pub fn check_crate<'ast>(sess: &Session, krate: &'ast hir::Crate, - def_map: &RefCell, + def_map: &DefMap, ast_map: &ast_map::Map<'ast>) { let mut visitor = CheckCrateVisitor { sess: sess, @@ -108,7 +108,7 @@ struct CheckItemRecursionVisitor<'a, 'ast: 'a> { root_span: &'a Span, sess: &'a Session, ast_map: &'a ast_map::Map<'ast>, - def_map: &'a RefCell, + def_map: &'a DefMap, discriminant_map: &'a RefCell>>, idstack: Vec, } @@ -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)) => { diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 06708a5127f34..558ecf85511c8 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -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, From 1ca1874986206b0d635b69e91f33195624bae74f Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Wed, 4 Nov 2015 00:26:41 -0600 Subject: [PATCH 3/4] Remove use of RefCell in resolve_lifetime --- src/librustc/middle/resolve_lifetime.rs | 7 +++---- src/librustc_driver/driver.rs | 4 ++-- src/librustc_driver/test.rs | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 5035ac12a85f7..fa0c6c41ce5cc 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -23,7 +23,6 @@ use middle::def::{self, DefMap}; use middle::region; use middle::subst; use middle::ty; -use std::cell::RefCell; use std::fmt; use std::mem::replace; use syntax::ast; @@ -55,7 +54,7 @@ struct LifetimeContext<'a> { sess: &'a Session, named_region_map: &'a mut NamedRegionMap, scope: Scope<'a>, - def_map: &'a RefCell, + def_map: &'a DefMap, // Deep breath. Our representation for poly trait refs contains a single // binder and thus we only allow a single level of quantification. However, // the syntax of Rust permits quantification in two places, e.g., `T: for <'a> Foo<'a>` @@ -94,7 +93,7 @@ type Scope<'a> = &'a ScopeChain<'a>; static ROOT_SCOPE: ScopeChain<'static> = RootScope; -pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &RefCell) -> NamedRegionMap { +pub fn krate(sess: &Session, krate: &hir::Crate, def_map: &DefMap) -> NamedRegionMap { let mut named_region_map = NodeMap(); visit::walk_crate(&mut LifetimeContext { sess: sess, @@ -206,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); diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 558ecf85511c8..2237e19c8e7da 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -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)); diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 4bbc22ef1a273..5cd6bfa879033 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -134,7 +134,7 @@ fn test_env(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, From b1788ef8e1b9e2142dbb20d1f5a325fc9b9cb592 Mon Sep 17 00:00:00 2001 From: Jonathan S Date: Wed, 4 Nov 2015 06:26:00 -0600 Subject: [PATCH 4/4] Remove use of RefCell in the simpler parts of pat_util --- src/librustc/middle/cfg/construct.rs | 2 +- src/librustc/middle/check_match.rs | 8 ++--- src/librustc/middle/dead.rs | 4 +-- src/librustc/middle/expr_use_visitor.rs | 4 +-- src/librustc/middle/pat_util.rs | 32 +++++++++---------- src/librustc_mir/hair/cx/pattern.rs | 4 +-- src/librustc_trans/trans/_match.rs | 8 ++--- .../trans/debuginfo/create_scope_map.rs | 2 +- src/librustc_typeck/check/_match.rs | 7 ++-- src/librustc_typeck/check/mod.rs | 2 +- src/librustc_typeck/check/writeback.rs | 2 +- 11 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/librustc/middle/cfg/construct.rs b/src/librustc/middle/cfg/construct.rs index 9f83cb9fddea4..5b931857decca 100644 --- a/src/librustc/middle/cfg/construct.rs +++ b/src/librustc/middle/cfg/construct.rs @@ -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 diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index fce96457d174b..f46e55e244140 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -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 @@ -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"); @@ -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); @@ -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 `@`"); diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 1a1a9d4b1b48f..b4280f86c7d58 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -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 @@ -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) } diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 6e469da33f98e..ce8d74bf191c7 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -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), @@ -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={:?}", diff --git a/src/librustc/middle/pat_util.rs b/src/librustc/middle/pat_util.rs index 77ffd0b606ebe..09132396054ff 100644 --- a/src/librustc/middle/pat_util.rs +++ b/src/librustc/middle/pat_util.rs @@ -32,13 +32,13 @@ pub fn pat_id_map(dm: &RefCell, pat: &hir::Pat) -> PatIdMap { map } -pub fn pat_is_refutable(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool { match pat.node { hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true, 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 } @@ -48,12 +48,12 @@ pub fn pat_is_refutable(dm: &RefCell, pat: &hir::Pat) -> bool { } } -pub fn pat_is_variant_or_struct(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool { match pat.node { 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 } @@ -62,10 +62,10 @@ pub fn pat_is_variant_or_struct(dm: &RefCell, pat: &hir::Pat) -> bool { } } -pub fn pat_is_const(dm: &RefCell, 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 } @@ -76,10 +76,10 @@ pub fn pat_is_const(dm: &RefCell, pat: &hir::Pat) -> bool { // Same as above, except that partially-resolved defs cause `false` to be // returned instead of a panic. -pub fn pat_is_resolved_const(dm: &RefCell, 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, @@ -90,7 +90,7 @@ pub fn pat_is_resolved_const(dm: &RefCell, pat: &hir::Pat) -> bool { } } -pub fn pat_is_binding(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool { match pat.node { hir::PatIdent(..) => { !pat_is_variant_or_struct(dm, pat) && @@ -100,7 +100,7 @@ pub fn pat_is_binding(dm: &RefCell, pat: &hir::Pat) -> bool { } } -pub fn pat_is_binding_or_wild(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool { match pat.node { hir::PatIdent(..) => pat_is_binding(dm, pat), hir::PatWild => true, @@ -115,7 +115,7 @@ pub fn pat_bindings(dm: &RefCell, pat: &hir::Pat, mut it: I) where { 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)); } _ => {} @@ -129,7 +129,7 @@ pub fn pat_bindings_hygienic(dm: &RefCell, pat: &hir::Pat, mut it: I) { 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)); } _ => {} @@ -140,7 +140,7 @@ pub fn pat_bindings_hygienic(dm: &RefCell, pat: &hir::Pat, mut it: I) /// Checks if the pattern contains any patterns that bind something to /// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`. -pub fn pat_contains_bindings(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool { let mut contains_bindings = false; walk_pat(pat, |p| { if pat_is_binding(dm, p) { @@ -185,7 +185,7 @@ pub fn arm_contains_ref_binding(dm: &RefCell, arm: &hir::Arm) -> Option< /// Checks if the pattern contains any patterns that bind something to /// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`, -pub fn pat_contains_bindings_or_wild(dm: &RefCell, pat: &hir::Pat) -> bool { +pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool { let mut contains_bindings = false; walk_pat(pat, |p| { if pat_is_binding_or_wild(dm, p) { @@ -221,14 +221,14 @@ pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path { } /// Return variants that are necessary to exist for the pattern to match. -pub fn necessary_variants(dm: &RefCell, pat: &hir::Pat) -> Vec { +pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec { let mut variants = vec![]; walk_pat(pat, |p| { match p.node { 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); } diff --git a/src/librustc_mir/hair/cx/pattern.rs b/src/librustc_mir/hair/cx/pattern.rs index 31dbffa0ae348..fa96780417905 100644 --- a/src/librustc_mir/hair/cx/pattern.rs +++ b/src/librustc_mir/hair/cx/pattern.rs @@ -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 { @@ -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, diff --git a/src/librustc_trans/trans/_match.rs b/src/librustc_trans/trans/_match.rs index 6678d1c34a0c4..0fb0407d3ba72 100644 --- a/src/librustc_trans/trans/_match.rs +++ b/src/librustc_trans/trans/_match.rs @@ -517,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)); } } @@ -556,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) @@ -847,7 +847,7 @@ fn pick_column_to_specialize(def_map: &RefCell, m: &[Match]) -> Option, 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 } } @@ -1801,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. diff --git a/src/librustc_trans/trans/debuginfo/create_scope_map.rs b/src/librustc_trans/trans/debuginfo/create_scope_map.rs index 01f5a32b1a3d4..0c424de9e10b8 100644 --- a/src/librustc_trans/trans/debuginfo/create_scope_map.rs +++ b/src/librustc_trans/trans/debuginfo/create_scope_map.rs @@ -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; diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index 694bb0e15ac79..cd7012cd4ec6d 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -133,7 +133,8 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, // subtyping doesn't matter here, as the value is some kind of scalar demand::eqtype(fcx, pat.span, expected, lhs_ty); } - hir::PatEnum(..) | hir::PatIdent(..) if pat_is_resolved_const(&tcx.def_map, pat) => { + hir::PatEnum(..) | hir::PatIdent(..) + if pat_is_resolved_const(&tcx.def_map.borrow(), pat) => { let const_did = tcx.def_map.borrow().get(&pat.id).unwrap().def_id(); let const_scheme = tcx.lookup_item_type(const_did); assert!(const_scheme.generics.is_empty()); @@ -149,7 +150,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, // is good enough. demand::suptype(fcx, pat.span, expected, const_ty); } - hir::PatIdent(bm, ref path, ref sub) if pat_is_binding(&tcx.def_map, pat) => { + hir::PatIdent(bm, ref path, ref sub) if pat_is_binding(&tcx.def_map.borrow(), pat) => { let typ = fcx.local_ty(pat.span, pat.id); match bm { hir::BindByRef(mutbl) => { @@ -410,7 +411,7 @@ pub fn check_dereferencable<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, inner: &hir::Pat) -> bool { let fcx = pcx.fcx; let tcx = pcx.fcx.ccx.tcx; - if pat_is_binding(&tcx.def_map, inner) { + if pat_is_binding(&tcx.def_map.borrow(), inner) { let expected = fcx.infcx().shallow_resolve(expected); expected.builtin_deref(true, ty::NoPreference).map_or(true, |mt| match mt.ty.sty { ty::TyTrait(_) => { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index dc2b2b75ab6d1..cfa32bc073a1f 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -529,7 +529,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> { // Add pattern bindings. fn visit_pat(&mut self, p: &'tcx hir::Pat) { if let hir::PatIdent(_, ref path1, _) = p.node { - if pat_util::pat_is_binding(&self.fcx.ccx.tcx.def_map, p) { + if pat_util::pat_is_binding(&self.fcx.ccx.tcx.def_map.borrow(), p) { let var_ty = self.assign(p.span, p.id, None); self.fcx.require_type_is_sized(var_ty, p.span, diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index cfab28f923e09..5b1fafe09fac9 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -57,7 +57,7 @@ pub fn resolve_type_vars_in_fn(fcx: &FnCtxt, wbcx.visit_pat(&*arg.pat); // Privacy needs the type for the whole pattern, not just each binding - if !pat_util::pat_is_binding(&fcx.tcx().def_map, &*arg.pat) { + if !pat_util::pat_is_binding(&fcx.tcx().def_map.borrow(), &*arg.pat) { wbcx.visit_node_id(ResolvingPattern(arg.pat.span), arg.pat.id); }