]> git.lizzy.rs Git - rust.git/commitdiff
refactor with extract_binding_mode
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 14 Dec 2019 22:18:39 +0000 (23:18 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 23 Dec 2019 13:47:19 +0000 (14:47 +0100)
src/librustc/ty/context.rs
src/librustc_mir/build/mod.rs
src/librustc_mir/hair/pattern/check_match.rs
src/librustc_typeck/check/regionck.rs
src/librustc_typeck/check/writeback.rs
src/librustc_typeck/expr_use_visitor.rs

index 0806e2d77650e58a316cfd02b6445a68bdc0093d..e36b11ae0050c0074d007c835b352726182aa542 100644 (file)
@@ -648,6 +648,13 @@ pub fn is_method_call(&self, expr: &hir::Expr) -> bool {
         }
     }
 
+    pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
+        self.pat_binding_modes().get(id).copied().or_else(|| {
+            s.delay_span_bug(sp, "missing binding mode");
+            None
+        })
+    }
+
     pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
         LocalTableInContext {
             local_id_root: self.local_id_root,
index 3b85a5d3c911bd10245080ee3e5c45bc2943b834..3a2a2dc412e72c7a39a936309ae3b29da4581387 100644 (file)
@@ -816,15 +816,12 @@ fn args_and_body(&mut self,
                 if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
                     if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
                         name = ident.name;
-
-                        if let Some(&bm) = hir_tables.pat_binding_modes().get(pat.hir_id) {
-                            if bm == ty::BindByValue(hir::Mutability::Mut) {
+                        match hir_tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
+                            Some(ty::BindByValue(hir::Mutability::Mut)) => {
                                 mutability = Mutability::Mut;
-                            } else {
-                                mutability = Mutability::Not;
                             }
-                        } else {
-                            tcx.sess.delay_span_bug(pat.span, "missing binding mode");
+                            Some(_) => mutability = Mutability::Not,
+                            _ => {}
                         }
                     }
                 }
index 3f7a17183e7227a506ccac89d29356bd216a972e..3cfafaf180634e9cf7b128d0b46c99d32564ca78 100644 (file)
@@ -271,11 +271,9 @@ fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, pa
 fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
     pat.walk(|p| {
         if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
-            if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
-                if bm != ty::BindByValue(hir::Mutability::Not) {
-                    // Nothing to check.
-                    return true;
-                }
+            if let Some(ty::BindByValue(hir::Mutability::Not)) =
+                cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
+            {
                 let pat_ty = cx.tables.pat_ty(p);
                 if let ty::Adt(edef, _) = pat_ty.kind {
                     if edef.is_enum()
@@ -303,8 +301,6 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
                         err.emit();
                     }
                 }
-            } else {
-                cx.tcx.sess.delay_span_bug(p.span, "missing binding mode");
             }
         }
         true
@@ -581,15 +577,14 @@ fn maybe_point_at_variant(ty: Ty<'_>, patterns: &[super::Pat<'_>]) -> Vec<Span>
 
 // Check the legality of legality of by-move bindings.
 fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: bool, pat: &Pat) {
+    let sess = cx.tcx.sess;
+    let tables = cx.tables;
+
     // Find all by-ref spans.
     let mut by_ref_spans = Vec::new();
     pat.each_binding(|_, hir_id, span, _| {
-        if let Some(&bm) = cx.tables.pat_binding_modes().get(hir_id) {
-            if let ty::BindByReference(..) = bm {
-                by_ref_spans.push(span);
-            }
-        } else {
-            cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
+        if let Some(ty::BindByReference(_)) = tables.extract_binding_mode(sess, hir_id, span) {
+            by_ref_spans.push(span);
         }
     });
 
@@ -600,7 +595,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
         //
         // `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
         if sub.map_or(false, |p| p.contains_bindings()) {
-            struct_span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings")
+            struct_span_err!(sess, p.span, E0007, "cannot bind by-move with sub-bindings")
                 .span_label(p.span, "binds an already bound by-move value by moving it")
                 .emit();
         } else if !has_guard && !by_ref_spans.is_empty() {
@@ -609,15 +604,11 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
     };
     pat.walk(|p| {
         if let hir::PatKind::Binding(.., sub) = &p.kind {
-            if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
-                if let ty::BindByValue(..) = bm {
-                    let pat_ty = cx.tables.node_type(p.hir_id);
-                    if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
-                        check_move(p, sub.as_deref());
-                    }
+            if let Some(ty::BindByValue(_)) = tables.extract_binding_mode(sess, p.hir_id, p.span) {
+                let pat_ty = tables.node_type(p.hir_id);
+                if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
+                    check_move(p, sub.as_deref());
                 }
-            } else {
-                cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
             }
         }
         true
@@ -626,7 +617,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
     // Found some bad by-move spans, error!
     if !by_move_spans.is_empty() {
         let mut err = struct_span_err!(
-            cx.tcx.sess,
+            sess,
             MultiSpan::from_spans(by_move_spans.clone()),
             E0009,
             "cannot bind by-move and by-ref in the same pattern",
@@ -642,14 +633,12 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
 }
 
 fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
+    let tab = cx.tables;
+    let sess = cx.tcx.sess;
     // Get the mutability of `p` if it's by-ref.
-    let extract_binding_mut = |hir_id, span| match cx.tables.pat_binding_modes().get(hir_id) {
-        None => {
-            cx.tcx.sess.delay_span_bug(span, "missing binding mode");
-            None
-        }
-        Some(ty::BindByValue(..)) => None,
-        Some(ty::BindByReference(m)) => Some(*m),
+    let extract_binding_mut = |hir_id, span| match tab.extract_binding_mode(sess, hir_id, span)? {
+        ty::BindByValue(_) => None,
+        ty::BindByReference(m) => Some(m),
     };
     pat.walk(|pat| {
         // Extract `sub` in `binding @ sub`.
@@ -671,8 +660,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
         sub.each_binding(|_, hir_id, span, _| {
             if let Some(mut_inner) = extract_binding_mut(hir_id, span) {
                 match (mut_outer, mut_inner) {
-                    (Mutability::Immutable, Mutability::Immutable) => {}
-                    (Mutability::Mutable, Mutability::Mutable) => conflicts_mut_mut.push(span),
+                    (Mutability::Not, Mutability::Not) => {}
+                    (Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push(span),
                     _ => conflicts_mut_ref.push(span),
                 }
             }
@@ -683,7 +672,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
         if !conflicts_mut_mut.is_empty() {
             // Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
             let msg = &format!("cannot borrow `{}` as mutable more than once at a time", name);
-            let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
+            let mut err = sess.struct_span_err(pat.span, msg);
             err.span_label(binding_span, "first mutable borrow occurs here");
             for sp in conflicts_mut_mut {
                 err.span_label(sp, "another mutable borrow occurs here");
@@ -695,14 +684,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
         } else if !conflicts_mut_ref.is_empty() {
             // Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
             let (primary, also) = match mut_outer {
-                Mutability::Mutable => ("mutable", "immutable"),
-                Mutability::Immutable => ("immutable", "mutable"),
+                Mutability::Mut => ("mutable", "immutable"),
+                Mutability::Not => ("immutable", "mutable"),
             };
             let msg = &format!(
                 "cannot borrow `{}` as {} because it is also borrowed as {}",
                 name, primary, also,
             );
-            let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
+            let mut err = sess.struct_span_err(pat.span, msg);
             err.span_label(binding_span, &format!("{} borrow occurs here", primary));
             for sp in conflicts_mut_ref {
                 err.span_label(sp, &format!("{} borrow occurs here", also));
index 8abfc2981a6a4fddd021178098e52bc12aa46ed7..149f27ed305fb825d43f0e7a72001141bd9d3779 100644 (file)
@@ -1007,20 +1007,13 @@ fn link_fn_params(&self, params: &[hir::Param]) {
     fn link_pattern(&self, discr_cmt: mc::Place<'tcx>, root_pat: &hir::Pat) {
         debug!("link_pattern(discr_cmt={:?}, root_pat={:?})", discr_cmt, root_pat);
         ignore_err!(self.with_mc(|mc| {
-            mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, sub_pat| {
+            mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, hir::Pat { kind, span, hir_id }| {
                 // `ref x` pattern
-                if let PatKind::Binding(..) = sub_pat.kind {
-                    if let Some(&bm) = mc.tables.pat_binding_modes().get(sub_pat.hir_id) {
-                        if let ty::BindByReference(mutbl) = bm {
-                            self.link_region_from_node_type(
-                                sub_pat.span,
-                                sub_pat.hir_id,
-                                mutbl,
-                                &sub_cmt,
-                            );
-                        }
-                    } else {
-                        self.tcx.sess.delay_span_bug(sub_pat.span, "missing binding mode");
+                if let PatKind::Binding(..) = kind {
+                    if let Some(ty::BindByReference(mutbl)) =
+                        mc.tables.extract_binding_mode(self.tcx.sess, *hir_id, *span)
+                    {
+                        self.link_region_from_node_type(*span, *hir_id, mutbl, &sub_cmt);
                     }
                 }
             })
index 8ed8751f65c9a49a029c08ff67a1491a2bd29f9c..5ef5f4c648e8ce73ef7263944198d1065cc4e4fc 100644 (file)
@@ -284,10 +284,9 @@ fn visit_block(&mut self, b: &'tcx hir::Block) {
     fn visit_pat(&mut self, p: &'tcx hir::Pat) {
         match p.kind {
             hir::PatKind::Binding(..) => {
-                if let Some(&bm) = self.fcx.tables.borrow().pat_binding_modes().get(p.hir_id) {
+                let tables = self.fcx.tables.borrow();
+                if let Some(bm) = tables.extract_binding_mode(self.tcx().sess, p.hir_id, p.span) {
                     self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
-                } else {
-                    self.tcx().sess.delay_span_bug(p.span, "missing binding mode");
                 }
             }
             hir::PatKind::Struct(_, ref fields, _) => {
index 58baf24438bd05f3529926481b017ab0d7b04905..788dda1cd5a0e26165972eb604d4ab3ef5ab9b87 100644 (file)
@@ -534,7 +534,7 @@ fn walk_pat(&mut self, discr_place: &Place<'tcx>, pat: &hir::Pat) {
         return_if_err!(mc.cat_pattern(discr_place.clone(), pat, |place, pat| {
             if let PatKind::Binding(_, canonical_id, ..) = pat.kind {
                 debug!("walk_pat: binding place={:?} pat={:?}", place, pat,);
-                if let Some(&bm) = mc.tables.pat_binding_modes().get(pat.hir_id) {
+                if let Some(bm) = mc.tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
                     debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
 
                     // pat_ty: the type of the binding being produced.
@@ -560,8 +560,6 @@ fn walk_pat(&mut self, discr_place: &Place<'tcx>, pat: &hir::Pat) {
                             delegate.consume(place, mode);
                         }
                     }
-                } else {
-                    tcx.sess.delay_span_bug(pat.span, "missing binding mode");
                 }
             }
         }));