Skip to content

Commit b1788ef

Browse files
committed
Remove use of RefCell<DefMap> in the simpler parts of pat_util
1 parent 1ca1874 commit b1788ef

File tree

11 files changed

+38
-37
lines changed

11 files changed

+38
-37
lines changed

src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
472472
let guard_exit = self.expr(&**guard, guard_start);
473473

474474
let this_has_bindings = pat_util::pat_contains_bindings_or_wild(
475-
&self.tcx.def_map, &**pat);
475+
&self.tcx.def_map.borrow(), &**pat);
476476

477477
// If both this pattern and the previous pattern
478478
// were free of bindings, they must consist only

src/librustc/middle/check_match.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ fn is_useful(cx: &MatchCheckCtxt,
702702

703703
Some(constructor) => {
704704
let matrix = rows.iter().filter_map(|r| {
705-
if pat_is_binding_or_wild(&cx.tcx.def_map, raw_pat(r[0])) {
705+
if pat_is_binding_or_wild(&cx.tcx.def_map.borrow(), raw_pat(r[0])) {
706706
Some(r[1..].to_vec())
707707
} else {
708708
None
@@ -1073,7 +1073,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
10731073
// check legality of moving out of the enum
10741074

10751075
// x @ Foo(..) is legal, but x @ Foo(y) isn't.
1076-
if sub.map_or(false, |p| pat_contains_bindings(def_map, &*p)) {
1076+
if sub.map_or(false, |p| pat_contains_bindings(&def_map.borrow(), &*p)) {
10771077
span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings");
10781078
} else if has_guard {
10791079
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,
10861086

10871087
for pat in pats {
10881088
front_util::walk_pat(&**pat, |p| {
1089-
if pat_is_binding(def_map, &*p) {
1089+
if pat_is_binding(&def_map.borrow(), &*p) {
10901090
match p.node {
10911091
hir::PatIdent(hir::BindByValue(_), _, ref sub) => {
10921092
let pat_ty = tcx.node_id_to_type(p.id);
@@ -1181,7 +1181,7 @@ struct AtBindingPatternVisitor<'a, 'b:'a, 'tcx:'b> {
11811181

11821182
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for AtBindingPatternVisitor<'a, 'b, 'tcx> {
11831183
fn visit_pat(&mut self, pat: &Pat) {
1184-
if !self.bindings_allowed && pat_is_binding(&self.cx.tcx.def_map, pat) {
1184+
if !self.bindings_allowed && pat_is_binding(&self.cx.tcx.def_map.borrow(), pat) {
11851185
span_err!(self.cx.tcx.sess, pat.span, E0303,
11861186
"pattern bindings are not allowed \
11871187
after an `@`");

src/librustc/middle/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
250250
fn visit_arm(&mut self, arm: &hir::Arm) {
251251
if arm.pats.len() == 1 {
252252
let pat = &*arm.pats[0];
253-
let variants = pat_util::necessary_variants(&self.tcx.def_map, pat);
253+
let variants = pat_util::necessary_variants(&self.tcx.def_map.borrow(), pat);
254254

255255
// Inside the body, ignore constructions of variants
256256
// necessary for the pattern to match. Those construction sites
@@ -270,7 +270,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
270270
hir::PatStruct(_, ref fields, _) => {
271271
self.handle_field_pattern_match(pat, fields);
272272
}
273-
_ if pat_util::pat_is_const(def_map, pat) => {
273+
_ if pat_util::pat_is_const(&def_map.borrow(), pat) => {
274274
// it might be the only use of a const
275275
self.lookup_and_handle_definition(&pat.id)
276276
}

src/librustc/middle/expr_use_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
934934
return_if_err!(self.mc.cat_pattern(cmt_discr, pat, |_mc, cmt_pat, pat| {
935935
let tcx = self.tcx();
936936
let def_map = &self.tcx().def_map;
937-
if pat_util::pat_is_binding(def_map, pat) {
937+
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
938938
match pat.node {
939939
hir::PatIdent(hir::BindByRef(_), _, _) =>
940940
mode.lub(BorrowingMatch),
@@ -969,7 +969,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
969969
let def_map = &self.tcx().def_map;
970970
let delegate = &mut self.delegate;
971971
return_if_err!(mc.cat_pattern(cmt_discr.clone(), pat, |mc, cmt_pat, pat| {
972-
if pat_util::pat_is_binding(def_map, pat) {
972+
if pat_util::pat_is_binding(&def_map.borrow(), pat) {
973973
let tcx = typer.tcx;
974974

975975
debug!("binding cmt_pat={:?} pat={:?} match_mode={:?}",

src/librustc/middle/pat_util.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ pub fn pat_id_map(dm: &RefCell<DefMap>, pat: &hir::Pat) -> PatIdMap {
3232
map
3333
}
3434

35-
pub fn pat_is_refutable(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
35+
pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
3636
match pat.node {
3737
hir::PatLit(_) | hir::PatRange(_, _) | hir::PatQPath(..) => true,
3838
hir::PatEnum(_, _) |
3939
hir::PatIdent(_, _, None) |
4040
hir::PatStruct(..) => {
41-
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
41+
match dm.get(&pat.id).map(|d| d.full_def()) {
4242
Some(DefVariant(..)) => true,
4343
_ => false
4444
}
@@ -48,12 +48,12 @@ pub fn pat_is_refutable(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
4848
}
4949
}
5050

51-
pub fn pat_is_variant_or_struct(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
51+
pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
5252
match pat.node {
5353
hir::PatEnum(_, _) |
5454
hir::PatIdent(_, _, None) |
5555
hir::PatStruct(..) => {
56-
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
56+
match dm.get(&pat.id).map(|d| d.full_def()) {
5757
Some(DefVariant(..)) | Some(DefStruct(..)) => true,
5858
_ => false
5959
}
@@ -62,10 +62,10 @@ pub fn pat_is_variant_or_struct(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
6262
}
6363
}
6464

65-
pub fn pat_is_const(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
65+
pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
6666
match pat.node {
6767
hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
68-
match dm.borrow().get(&pat.id).map(|d| d.full_def()) {
68+
match dm.get(&pat.id).map(|d| d.full_def()) {
6969
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
7070
_ => false
7171
}
@@ -76,10 +76,10 @@ pub fn pat_is_const(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
7676

7777
// Same as above, except that partially-resolved defs cause `false` to be
7878
// returned instead of a panic.
79-
pub fn pat_is_resolved_const(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
79+
pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
8080
match pat.node {
8181
hir::PatIdent(_, _, None) | hir::PatEnum(..) | hir::PatQPath(..) => {
82-
match dm.borrow().get(&pat.id)
82+
match dm.get(&pat.id)
8383
.and_then(|d| if d.depth == 0 { Some(d.base_def) }
8484
else { None } ) {
8585
Some(DefConst(..)) | Some(DefAssociatedConst(..)) => true,
@@ -90,7 +90,7 @@ pub fn pat_is_resolved_const(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
9090
}
9191
}
9292

93-
pub fn pat_is_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
93+
pub fn pat_is_binding(dm: &DefMap, pat: &hir::Pat) -> bool {
9494
match pat.node {
9595
hir::PatIdent(..) => {
9696
!pat_is_variant_or_struct(dm, pat) &&
@@ -100,7 +100,7 @@ pub fn pat_is_binding(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
100100
}
101101
}
102102

103-
pub fn pat_is_binding_or_wild(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
103+
pub fn pat_is_binding_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
104104
match pat.node {
105105
hir::PatIdent(..) => pat_is_binding(dm, pat),
106106
hir::PatWild => true,
@@ -115,7 +115,7 @@ pub fn pat_bindings<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I) where
115115
{
116116
walk_pat(pat, |p| {
117117
match p.node {
118-
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
118+
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
119119
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node.name));
120120
}
121121
_ => {}
@@ -129,7 +129,7 @@ pub fn pat_bindings_hygienic<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I)
129129
{
130130
walk_pat(pat, |p| {
131131
match p.node {
132-
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
132+
hir::PatIdent(binding_mode, ref pth, _) if pat_is_binding(&dm.borrow(), p) => {
133133
it(binding_mode, p.id, p.span, &respan(pth.span, pth.node));
134134
}
135135
_ => {}
@@ -140,7 +140,7 @@ pub fn pat_bindings_hygienic<I>(dm: &RefCell<DefMap>, pat: &hir::Pat, mut it: I)
140140

141141
/// Checks if the pattern contains any patterns that bind something to
142142
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
143-
pub fn pat_contains_bindings(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
143+
pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
144144
let mut contains_bindings = false;
145145
walk_pat(pat, |p| {
146146
if pat_is_binding(dm, p) {
@@ -185,7 +185,7 @@ pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<
185185

186186
/// Checks if the pattern contains any patterns that bind something to
187187
/// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
188-
pub fn pat_contains_bindings_or_wild(dm: &RefCell<DefMap>, pat: &hir::Pat) -> bool {
188+
pub fn pat_contains_bindings_or_wild(dm: &DefMap, pat: &hir::Pat) -> bool {
189189
let mut contains_bindings = false;
190190
walk_pat(pat, |p| {
191191
if pat_is_binding_or_wild(dm, p) {
@@ -221,14 +221,14 @@ pub fn def_to_path(tcx: &ty::ctxt, id: DefId) -> hir::Path {
221221
}
222222

223223
/// Return variants that are necessary to exist for the pattern to match.
224-
pub fn necessary_variants(dm: &RefCell<DefMap>, pat: &hir::Pat) -> Vec<DefId> {
224+
pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
225225
let mut variants = vec![];
226226
walk_pat(pat, |p| {
227227
match p.node {
228228
hir::PatEnum(_, _) |
229229
hir::PatIdent(_, _, None) |
230230
hir::PatStruct(..) => {
231-
match dm.borrow().get(&p.id) {
231+
match dm.get(&p.id) {
232232
Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
233233
variants.push(id);
234234
}

src/librustc_mir/hair/cx/pattern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'tcx> Mirror<'tcx> for PatNode<'tcx> {
155155
},
156156

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

233233
hir::PatIdent(bm, ref ident, ref sub)
234-
if pat_is_binding(&cx.tcx.def_map, self.pat) =>
234+
if pat_is_binding(&cx.tcx.def_map.borrow(), self.pat) =>
235235
{
236236
let id = match self.binding_map {
237237
None => self.pat.id,

src/librustc_trans/trans/_match.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ fn enter_match<'a, 'b, 'p, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
517517
let mut bound_ptrs = br.bound_ptrs.clone();
518518
match this.node {
519519
hir::PatIdent(_, ref path, None) => {
520-
if pat_is_binding(dm, &*this) {
520+
if pat_is_binding(&dm.borrow(), &*this) {
521521
bound_ptrs.push((path.node.name, val.val));
522522
}
523523
}
@@ -556,7 +556,7 @@ fn enter_default<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
556556

557557
// Collect all of the matches that can match against anything.
558558
enter_match(bcx, dm, m, col, val, |pats| {
559-
if pat_is_binding_or_wild(dm, &*pats[col]) {
559+
if pat_is_binding_or_wild(&dm.borrow(), &*pats[col]) {
560560
let mut r = pats[..col].to_vec();
561561
r.push_all(&pats[col + 1..]);
562562
Some(r)
@@ -847,7 +847,7 @@ fn pick_column_to_specialize(def_map: &RefCell<DefMap>, m: &[Match]) -> Option<u
847847
fn pat_score(def_map: &RefCell<DefMap>, pat: &hir::Pat) -> usize {
848848
match pat.node {
849849
hir::PatIdent(_, _, Some(ref inner)) => pat_score(def_map, &**inner),
850-
_ if pat_is_refutable(def_map, pat) => 1,
850+
_ if pat_is_refutable(&def_map.borrow(), pat) => 1,
851851
_ => 0
852852
}
853853
}
@@ -1801,7 +1801,7 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
18011801
let ccx = bcx.ccx();
18021802
match pat.node {
18031803
hir::PatIdent(pat_binding_mode, ref path1, ref inner) => {
1804-
if pat_is_binding(&tcx.def_map, &*pat) {
1804+
if pat_is_binding(&tcx.def_map.borrow(), &*pat) {
18051805
// Allocate the stack slot where the value of this
18061806
// binding will live and place it into the appropriate
18071807
// map.

src/librustc_trans/trans/debuginfo/create_scope_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn walk_pattern(cx: &CrateContext,
167167

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

172172
let name = path1.node.name;
173173

src/librustc_typeck/check/_match.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
133133
// subtyping doesn't matter here, as the value is some kind of scalar
134134
demand::eqtype(fcx, pat.span, expected, lhs_ty);
135135
}
136-
hir::PatEnum(..) | hir::PatIdent(..) if pat_is_resolved_const(&tcx.def_map, pat) => {
136+
hir::PatEnum(..) | hir::PatIdent(..)
137+
if pat_is_resolved_const(&tcx.def_map.borrow(), pat) => {
137138
let const_did = tcx.def_map.borrow().get(&pat.id).unwrap().def_id();
138139
let const_scheme = tcx.lookup_item_type(const_did);
139140
assert!(const_scheme.generics.is_empty());
@@ -149,7 +150,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
149150
// is good enough.
150151
demand::suptype(fcx, pat.span, expected, const_ty);
151152
}
152-
hir::PatIdent(bm, ref path, ref sub) if pat_is_binding(&tcx.def_map, pat) => {
153+
hir::PatIdent(bm, ref path, ref sub) if pat_is_binding(&tcx.def_map.borrow(), pat) => {
153154
let typ = fcx.local_ty(pat.span, pat.id);
154155
match bm {
155156
hir::BindByRef(mutbl) => {
@@ -410,7 +411,7 @@ pub fn check_dereferencable<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
410411
inner: &hir::Pat) -> bool {
411412
let fcx = pcx.fcx;
412413
let tcx = pcx.fcx.ccx.tcx;
413-
if pat_is_binding(&tcx.def_map, inner) {
414+
if pat_is_binding(&tcx.def_map.borrow(), inner) {
414415
let expected = fcx.infcx().shallow_resolve(expected);
415416
expected.builtin_deref(true, ty::NoPreference).map_or(true, |mt| match mt.ty.sty {
416417
ty::TyTrait(_) => {

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
529529
// Add pattern bindings.
530530
fn visit_pat(&mut self, p: &'tcx hir::Pat) {
531531
if let hir::PatIdent(_, ref path1, _) = p.node {
532-
if pat_util::pat_is_binding(&self.fcx.ccx.tcx.def_map, p) {
532+
if pat_util::pat_is_binding(&self.fcx.ccx.tcx.def_map.borrow(), p) {
533533
let var_ty = self.assign(p.span, p.id, None);
534534

535535
self.fcx.require_type_is_sized(var_ty, p.span,

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn resolve_type_vars_in_fn(fcx: &FnCtxt,
5757
wbcx.visit_pat(&*arg.pat);
5858

5959
// Privacy needs the type for the whole pattern, not just each binding
60-
if !pat_util::pat_is_binding(&fcx.tcx().def_map, &*arg.pat) {
60+
if !pat_util::pat_is_binding(&fcx.tcx().def_map.borrow(), &*arg.pat) {
6161
wbcx.visit_node_id(ResolvingPattern(arg.pat.span),
6262
arg.pat.id);
6363
}

0 commit comments

Comments
 (0)