Skip to content

Commit 5a8baa2

Browse files
committed
refactor with extract_binding_mode
1 parent eed311f commit 5a8baa2

File tree

6 files changed

+45
-62
lines changed

6 files changed

+45
-62
lines changed

src/librustc/ty/context.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,13 @@ impl<'tcx> TypeckTables<'tcx> {
648648
}
649649
}
650650

651+
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
652+
self.pat_binding_modes().get(id).copied().or_else(|| {
653+
s.delay_span_bug(sp, "missing binding mode");
654+
None
655+
})
656+
}
657+
651658
pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
652659
LocalTableInContext {
653660
local_id_root: self.local_id_root,

src/librustc_mir/build/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
816816
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
817817
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
818818
name = ident.name;
819-
820-
if let Some(&bm) = hir_tables.pat_binding_modes().get(pat.hir_id) {
821-
if bm == ty::BindByValue(hir::Mutability::Mut) {
819+
match hir_tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
820+
Some(ty::BindByValue(hir::Mutability::Mut)) => {
822821
mutability = Mutability::Mut;
823-
} else {
824-
mutability = Mutability::Not;
825822
}
826-
} else {
827-
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
823+
Some(_) => mutability = Mutability::Not,
824+
_ => {}
828825
}
829826
}
830827
}

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,9 @@ fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, pa
271271
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
272272
pat.walk(|p| {
273273
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
274-
if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
275-
if bm != ty::BindByValue(hir::Mutability::Not) {
276-
// Nothing to check.
277-
return true;
278-
}
274+
if let Some(ty::BindByValue(hir::Mutability::Not)) =
275+
cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
276+
{
279277
let pat_ty = cx.tables.pat_ty(p);
280278
if let ty::Adt(edef, _) = pat_ty.kind {
281279
if edef.is_enum()
@@ -303,8 +301,6 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
303301
err.emit();
304302
}
305303
}
306-
} else {
307-
cx.tcx.sess.delay_span_bug(p.span, "missing binding mode");
308304
}
309305
}
310306
true
@@ -581,15 +577,14 @@ fn maybe_point_at_variant(ty: Ty<'_>, patterns: &[super::Pat<'_>]) -> Vec<Span>
581577

582578
// Check the legality of legality of by-move bindings.
583579
fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: bool, pat: &Pat) {
580+
let sess = cx.tcx.sess;
581+
let tables = cx.tables;
582+
584583
// Find all by-ref spans.
585584
let mut by_ref_spans = Vec::new();
586585
pat.each_binding(|_, hir_id, span, _| {
587-
if let Some(&bm) = cx.tables.pat_binding_modes().get(hir_id) {
588-
if let ty::BindByReference(..) = bm {
589-
by_ref_spans.push(span);
590-
}
591-
} else {
592-
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
586+
if let Some(ty::BindByReference(_)) = tables.extract_binding_mode(sess, hir_id, span) {
587+
by_ref_spans.push(span);
593588
}
594589
});
595590

@@ -600,7 +595,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
600595
//
601596
// `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
602597
if sub.map_or(false, |p| p.contains_bindings()) {
603-
struct_span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings")
598+
struct_span_err!(sess, p.span, E0007, "cannot bind by-move with sub-bindings")
604599
.span_label(p.span, "binds an already bound by-move value by moving it")
605600
.emit();
606601
} 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
609604
};
610605
pat.walk(|p| {
611606
if let hir::PatKind::Binding(.., sub) = &p.kind {
612-
if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
613-
if let ty::BindByValue(..) = bm {
614-
let pat_ty = cx.tables.node_type(p.hir_id);
615-
if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
616-
check_move(p, sub.as_deref());
617-
}
607+
if let Some(ty::BindByValue(_)) = tables.extract_binding_mode(sess, p.hir_id, p.span) {
608+
let pat_ty = tables.node_type(p.hir_id);
609+
if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
610+
check_move(p, sub.as_deref());
618611
}
619-
} else {
620-
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
621612
}
622613
}
623614
true
@@ -626,7 +617,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
626617
// Found some bad by-move spans, error!
627618
if !by_move_spans.is_empty() {
628619
let mut err = struct_span_err!(
629-
cx.tcx.sess,
620+
sess,
630621
MultiSpan::from_spans(by_move_spans.clone()),
631622
E0009,
632623
"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
642633
}
643634

644635
fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
636+
let tab = cx.tables;
637+
let sess = cx.tcx.sess;
645638
// Get the mutability of `p` if it's by-ref.
646-
let extract_binding_mut = |hir_id, span| match cx.tables.pat_binding_modes().get(hir_id) {
647-
None => {
648-
cx.tcx.sess.delay_span_bug(span, "missing binding mode");
649-
None
650-
}
651-
Some(ty::BindByValue(..)) => None,
652-
Some(ty::BindByReference(m)) => Some(*m),
639+
let extract_binding_mut = |hir_id, span| match tab.extract_binding_mode(sess, hir_id, span)? {
640+
ty::BindByValue(_) => None,
641+
ty::BindByReference(m) => Some(m),
653642
};
654643
pat.walk(|pat| {
655644
// Extract `sub` in `binding @ sub`.
@@ -671,8 +660,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
671660
sub.each_binding(|_, hir_id, span, _| {
672661
if let Some(mut_inner) = extract_binding_mut(hir_id, span) {
673662
match (mut_outer, mut_inner) {
674-
(Mutability::Immutable, Mutability::Immutable) => {}
675-
(Mutability::Mutable, Mutability::Mutable) => conflicts_mut_mut.push(span),
663+
(Mutability::Not, Mutability::Not) => {}
664+
(Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push(span),
676665
_ => conflicts_mut_ref.push(span),
677666
}
678667
}
@@ -683,7 +672,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
683672
if !conflicts_mut_mut.is_empty() {
684673
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
685674
let msg = &format!("cannot borrow `{}` as mutable more than once at a time", name);
686-
let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
675+
let mut err = sess.struct_span_err(pat.span, msg);
687676
err.span_label(binding_span, "first mutable borrow occurs here");
688677
for sp in conflicts_mut_mut {
689678
err.span_label(sp, "another mutable borrow occurs here");
@@ -695,14 +684,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
695684
} else if !conflicts_mut_ref.is_empty() {
696685
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
697686
let (primary, also) = match mut_outer {
698-
Mutability::Mutable => ("mutable", "immutable"),
699-
Mutability::Immutable => ("immutable", "mutable"),
687+
Mutability::Mut => ("mutable", "immutable"),
688+
Mutability::Not => ("immutable", "mutable"),
700689
};
701690
let msg = &format!(
702691
"cannot borrow `{}` as {} because it is also borrowed as {}",
703692
name, primary, also,
704693
);
705-
let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
694+
let mut err = sess.struct_span_err(pat.span, msg);
706695
err.span_label(binding_span, &format!("{} borrow occurs here", primary));
707696
for sp in conflicts_mut_ref {
708697
err.span_label(sp, &format!("{} borrow occurs here", also));

src/librustc_typeck/check/regionck.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,20 +1007,13 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
10071007
fn link_pattern(&self, discr_cmt: mc::Place<'tcx>, root_pat: &hir::Pat) {
10081008
debug!("link_pattern(discr_cmt={:?}, root_pat={:?})", discr_cmt, root_pat);
10091009
ignore_err!(self.with_mc(|mc| {
1010-
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, sub_pat| {
1010+
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, hir::Pat { kind, span, hir_id }| {
10111011
// `ref x` pattern
1012-
if let PatKind::Binding(..) = sub_pat.kind {
1013-
if let Some(&bm) = mc.tables.pat_binding_modes().get(sub_pat.hir_id) {
1014-
if let ty::BindByReference(mutbl) = bm {
1015-
self.link_region_from_node_type(
1016-
sub_pat.span,
1017-
sub_pat.hir_id,
1018-
mutbl,
1019-
&sub_cmt,
1020-
);
1021-
}
1022-
} else {
1023-
self.tcx.sess.delay_span_bug(sub_pat.span, "missing binding mode");
1012+
if let PatKind::Binding(..) = kind {
1013+
if let Some(ty::BindByReference(mutbl)) =
1014+
mc.tables.extract_binding_mode(self.tcx.sess, *hir_id, *span)
1015+
{
1016+
self.link_region_from_node_type(*span, *hir_id, mutbl, &sub_cmt);
10241017
}
10251018
}
10261019
})

src/librustc_typeck/check/writeback.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,9 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
284284
fn visit_pat(&mut self, p: &'tcx hir::Pat) {
285285
match p.kind {
286286
hir::PatKind::Binding(..) => {
287-
if let Some(&bm) = self.fcx.tables.borrow().pat_binding_modes().get(p.hir_id) {
287+
let tables = self.fcx.tables.borrow();
288+
if let Some(bm) = tables.extract_binding_mode(self.tcx().sess, p.hir_id, p.span) {
288289
self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
289-
} else {
290-
self.tcx().sess.delay_span_bug(p.span, "missing binding mode");
291290
}
292291
}
293292
hir::PatKind::Struct(_, ref fields, _) => {

src/librustc_typeck/expr_use_visitor.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
534534
return_if_err!(mc.cat_pattern(discr_place.clone(), pat, |place, pat| {
535535
if let PatKind::Binding(_, canonical_id, ..) = pat.kind {
536536
debug!("walk_pat: binding place={:?} pat={:?}", place, pat,);
537-
if let Some(&bm) = mc.tables.pat_binding_modes().get(pat.hir_id) {
537+
if let Some(bm) = mc.tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
538538
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
539539

540540
// pat_ty: the type of the binding being produced.
@@ -560,8 +560,6 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
560560
delegate.consume(place, mode);
561561
}
562562
}
563-
} else {
564-
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
565563
}
566564
}
567565
}));

0 commit comments

Comments
 (0)