1
Fork 0

check_match: misc cleanup.

This commit is contained in:
Mazdak Farrokhzad 2019-09-15 02:48:53 +02:00
parent 75fb42a11f
commit 89bbef3026
2 changed files with 17 additions and 27 deletions

View file

@ -563,40 +563,30 @@ fn check_legality_of_move_bindings(
} }
}) })
} }
let span_vec = &mut Vec::new();
let check_move = |
cx: &mut MatchVisitor<'_, '_>,
p: &Pat,
sub: Option<&Pat>,
span_vec: &mut Vec<Span>,
| {
// check legality of moving out of the enum
// x @ Foo(..) is legal, but x @ Foo(y) isn't. let span_vec = &mut Vec::new();
let mut check_move = |p: &Pat, sub: Option<&Pat>| {
// Check legality of moving out of the enum.
//
// `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
if sub.map_or(false, |p| p.contains_bindings()) { if sub.map_or(false, |p| p.contains_bindings()) {
struct_span_err!(cx.tcx.sess, p.span, E0007, struct_span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings")
"cannot bind by-move with sub-bindings")
.span_label(p.span, "binds an already bound by-move value by moving it") .span_label(p.span, "binds an already bound by-move value by moving it")
.emit(); .emit();
} else if !has_guard { } else if !has_guard && by_ref_span.is_some() {
if let Some(_by_ref_span) = by_ref_span { span_vec.push(p.span);
span_vec.push(p.span);
}
} }
}; };
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(.., sub) = &p.node {
if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) { if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
match bm { if let ty::BindByValue(..) = bm {
ty::BindByValue(..) => { let pat_ty = cx.tables.node_type(p.hir_id);
let pat_ty = cx.tables.node_type(p.hir_id); if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) { check_move(p, sub.as_deref());
check_move(cx, p, sub.as_ref().map(|p| &**p), span_vec);
}
} }
_ => {}
} }
} else { } else {
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode"); cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
@ -605,11 +595,10 @@ fn check_legality_of_move_bindings(
true true
}); });
} }
if !span_vec.is_empty(){ if !span_vec.is_empty() {
let span = MultiSpan::from_spans(span_vec.clone());
let mut err = struct_span_err!( let mut err = struct_span_err!(
cx.tcx.sess, cx.tcx.sess,
span, MultiSpan::from_spans(span_vec.clone()),
E0009, E0009,
"cannot bind by-move and by-ref in the same pattern", "cannot bind by-move and by-ref in the same pattern",
); );
@ -627,7 +616,7 @@ fn check_legality_of_move_bindings(
/// because of the way rvalues are handled in the borrow check. (See issue /// because of the way rvalues are handled in the borrow check. (See issue
/// #14587.) /// #14587.)
fn check_legality_of_bindings_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) { fn check_legality_of_bindings_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
AtBindingPatternVisitor { cx: cx, bindings_allowed: true }.visit_pat(pat); AtBindingPatternVisitor { cx, bindings_allowed: true }.visit_pat(pat);
} }
struct AtBindingPatternVisitor<'a, 'b, 'tcx> { struct AtBindingPatternVisitor<'a, 'b, 'tcx> {

View file

@ -6,6 +6,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(nll)] #![feature(nll)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(inner_deref)]
#![feature(slice_patterns)] #![feature(slice_patterns)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]