Add more safeguards to "missing binding mode" errors

This commit is contained in:
varkor 2018-07-01 17:56:06 +01:00
parent 998141f8ef
commit b00050f4cf
5 changed files with 65 additions and 59 deletions

View File

@ -840,6 +840,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
fn walk_pat(&mut self, cmt_discr: mc::cmt<'tcx>, pat: &hir::Pat, match_mode: MatchMode) { fn walk_pat(&mut self, cmt_discr: mc::cmt<'tcx>, pat: &hir::Pat, match_mode: MatchMode) {
debug!("walk_pat(cmt_discr={:?}, pat={:?})", cmt_discr, pat); debug!("walk_pat(cmt_discr={:?}, pat={:?})", cmt_discr, pat);
let tcx = self.tcx();
let ExprUseVisitor { ref mc, ref mut delegate, param_env } = *self; let ExprUseVisitor { ref mc, ref mut delegate, param_env } = *self;
return_if_err!(mc.cat_pattern(cmt_discr.clone(), pat, |cmt_pat, pat| { return_if_err!(mc.cat_pattern(cmt_discr.clone(), pat, |cmt_pat, pat| {
if let PatKind::Binding(_, canonical_id, ..) = pat.node { if let PatKind::Binding(_, canonical_id, ..) = pat.node {
@ -849,8 +850,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
pat, pat,
match_mode, match_mode,
); );
let bm = *mc.tables.pat_binding_modes().get(pat.hir_id) if let Some(&bm) = mc.tables.pat_binding_modes().get(pat.hir_id) {
.expect("missing binding mode");
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm); debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
// pat_ty: the type of the binding being produced. // pat_ty: the type of the binding being produced.
@ -878,6 +878,9 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
delegate.consume_pat(pat, &cmt_pat, mode); delegate.consume_pat(pat, &cmt_pat, mode);
} }
} }
} else {
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
}
} }
})); }));

View File

@ -54,16 +54,16 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
// Skip anything that looks like `&foo` or `&mut foo`, only look // Skip anything that looks like `&foo` or `&mut foo`, only look
// for by-value bindings // for by-value bindings
let bm = match self.bccx.tables.pat_binding_modes().get(hir_id) { if let Some(&bm) = self.bccx.tables.pat_binding_modes().get(hir_id) {
Some(&bm) => bm,
None => span_bug!(span, "missing binding mode"),
};
match bm { match bm {
ty::BindByValue(hir::MutMutable) => {} ty::BindByValue(hir::MutMutable) => {}
_ => return, _ => return,
} }
mutables.entry(ident.name).or_insert(Vec::new()).push((hir_id, span)); mutables.entry(ident.name).or_insert(Vec::new()).push((hir_id, span));
} else {
tcx.sess.delay_span_bug(span, "missing binding mode");
}
}); });
} }

View File

@ -541,14 +541,15 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
if let hir::PatKind::Binding(_, _, ident, _) = pat.node { if let hir::PatKind::Binding(_, _, ident, _) = pat.node {
decl.debug_name = ident.name; decl.debug_name = ident.name;
let bm = *hir.tables.pat_binding_modes() if let Some(&bm) = hir.tables.pat_binding_modes().get(pat.hir_id) {
.get(pat.hir_id)
.expect("missing binding mode");
if bm == ty::BindByValue(hir::MutMutable) { if bm == ty::BindByValue(hir::MutMutable) {
decl.mutability = Mutability::Mut; decl.mutability = Mutability::Mut;
} else { } else {
decl.mutability = Mutability::Not; decl.mutability = Mutability::Not;
} }
} else {
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
}
} }
} }
decl decl

View File

@ -516,13 +516,13 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
let mut by_ref_span = None; let mut by_ref_span = None;
for pat in pats { for pat in pats {
pat.each_binding(|_, hir_id, span, _path| { pat.each_binding(|_, hir_id, span, _path| {
let bm = *cx.tables if let Some(&bm) = cx.tables.pat_binding_modes().get(hir_id) {
.pat_binding_modes()
.get(hir_id)
.expect("missing binding mode");
if let ty::BindByReference(..) = bm { if let ty::BindByReference(..) = bm {
by_ref_span = Some(span); by_ref_span = Some(span);
} }
} else {
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
}
}) })
} }
@ -552,10 +552,7 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
for pat in pats { for pat in pats {
pat.walk(|p| { pat.walk(|p| {
if let PatKind::Binding(_, _, _, ref sub) = p.node { if let PatKind::Binding(_, _, _, ref sub) = p.node {
let bm = *cx.tables if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
.pat_binding_modes()
.get(p.hir_id)
.expect("missing binding mode");
match bm { match bm {
ty::BindByValue(..) => { ty::BindByValue(..) => {
let pat_ty = cx.tables.node_id_to_type(p.hir_id); let pat_ty = cx.tables.node_id_to_type(p.hir_id);
@ -565,6 +562,9 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
} }
_ => {} _ => {}
} }
} else {
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
}
} }
true true
}); });

View File

@ -1039,12 +1039,14 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
match sub_pat.node { match sub_pat.node {
// `ref x` pattern // `ref x` pattern
PatKind::Binding(..) => { PatKind::Binding(..) => {
let bm = *mc.tables.pat_binding_modes().get(sub_pat.hir_id) if let Some(&bm) = mc.tables.pat_binding_modes().get(sub_pat.hir_id) {
.expect("missing binding mode");
if let ty::BindByReference(mutbl) = bm { if let ty::BindByReference(mutbl) = bm {
self.link_region_from_node_type(sub_pat.span, sub_pat.hir_id, self.link_region_from_node_type(sub_pat.span, sub_pat.hir_id,
mutbl, &sub_cmt); mutbl, &sub_cmt);
} }
} else {
self.tcx.sess.delay_span_bug(sub_pat.span, "missing binding mode");
}
} }
_ => {} _ => {}
} }