]> git.lizzy.rs Git - rust.git/commitdiff
check_legality_of_move_bindings: generalize diagnostics & add comments
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 14 Dec 2019 17:20:13 +0000 (18:20 +0100)
committerMazdak Farrokhzad <twingoow@gmail.com>
Mon, 23 Dec 2019 13:47:19 +0000 (14:47 +0100)
12 files changed:
src/librustc_mir/hair/pattern/check_match.rs
src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-2.stderr
src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-3.stderr
src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-4.stderr
src/test/ui/error-codes/E0009.stderr
src/test/ui/issues/issue-53840.stderr
src/test/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr
src/test/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr
src/test/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr
src/test/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr
src/test/ui/pattern/bindings-after-at/default-binding-modes-both-sides-independent.stderr
src/test/ui/rfc-2005-default-binding-mode/for.stderr

index 0094a6ed7e5c320d6e8437ec86b532a163e869f6..c3768e74385976d5a4c742796c762acfcffd4e3d 100644 (file)
@@ -579,18 +579,20 @@ 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 mut by_ref_span = None;
+    // 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_span = Some(span);
+                by_ref_spans.push(span);
             }
         } else {
             cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
         }
     });
 
-    let span_vec = &mut Vec::new();
+    // Find bad by-move spans:
+    let by_move_spans = &mut Vec::new();
     let mut check_move = |p: &Pat, sub: Option<&Pat>| {
         // Check legality of moving out of the enum.
         //
@@ -599,11 +601,10 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
             struct_span_err!(cx.tcx.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_span.is_some() {
-            span_vec.push(p.span);
+        } else if !has_guard && !by_ref_spans.is_empty() {
+            by_move_spans.push(p.span);
         }
     };
-
     pat.walk(|p| {
         if let hir::PatKind::Binding(.., sub) = &p.kind {
             if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
@@ -620,17 +621,18 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
         true
     });
 
-    if !span_vec.is_empty() {
+    // Found some bad by-move spans, error!
+    if !by_move_spans.is_empty() {
         let mut err = struct_span_err!(
             cx.tcx.sess,
-            MultiSpan::from_spans(span_vec.clone()),
+            MultiSpan::from_spans(by_move_spans.clone()),
             E0009,
             "cannot bind by-move and by-ref in the same pattern",
         );
-        if let Some(by_ref_span) = by_ref_span {
-            err.span_label(by_ref_span, "both by-ref and by-move used");
+        for span in by_ref_spans.iter() {
+            err.span_label(*span, "by-ref pattern here");
         }
-        for span in span_vec.iter() {
+        for span in by_move_spans.iter() {
             err.span_label(*span, "by-move pattern here");
         }
         err.emit();
index 9157fe0b070d09d0c350f2f4ddf1e51f0262a8b4..ff00aa8caa8d3bcfa7692c899b6e32d43c829eb0 100644 (file)
@@ -4,7 +4,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
 LL |         Some((ref _y, _z)) => { },
    |               ------  ^^ by-move pattern here
    |               |
-   |               both by-ref and by-move used
+   |               by-ref pattern here
 
 error: aborting due to previous error
 
index d53547178db1164d0fd8e8dde0ab3c6bd518bc5c..3e8358da3507da058d783590bd2a91a44639a37c 100644 (file)
@@ -4,7 +4,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
 LL |         DoubleOption::Some2(ref _y, _z) => { },
    |                             ------  ^^ by-move pattern here
    |                             |
-   |                             both by-ref and by-move used
+   |                             by-ref pattern here
 
 error: aborting due to previous error
 
index 267a9dff926a22fca86ebd2edb4616b3dc8d06c3..00e0c70d6494b6d658ba149f64337498cd950aa1 100644 (file)
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/bind-by-move-neither-can-live-while-the-other-survives-4.rs:12:15
    |
 LL |         Some((_y, ref _z)) => { },
-   |               ^^  ------ both by-ref and by-move used
+   |               ^^  ------ by-ref pattern here
    |               |
    |               by-move pattern here
 
index f8acb9a09d9780c26a975578069b035ba672b120..446a436d6477952c8b15cd7e1029fb14e653abe8 100644 (file)
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/E0009.rs:5:15
    |
 LL |         Some((y, ref z)) => {},
-   |               ^  ----- both by-ref and by-move used
+   |               ^  ----- by-ref pattern here
    |               |
    |               by-move pattern here
 
index 0032f60a221f830adb4b8921d63a210cf2052241..9cb034e7592daa90ce5697d09d001ba869f1b1d8 100644 (file)
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/issue-53840.rs:13:16
    |
 LL |         E::Foo(a, b, ref c) => {}
-   |                ^  ^  ----- both by-ref and by-move used
+   |                ^  ^  ----- by-ref pattern here
    |                |  |
    |                |  by-move pattern here
    |                by-move pattern here
@@ -11,7 +11,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/issue-53840.rs:17:14
    |
 LL |         Bar {a, ref b} => {}
-   |              ^  ----- both by-ref and by-move used
+   |              ^  ----- by-ref pattern here
    |              |
    |              by-move pattern here
 
index c4afdc576a16a908ee1d2c7ca92c37464b915ffc..63866238f565ec98d8a3f7993949294091722a5c 100644 (file)
@@ -13,7 +13,7 @@ LL |         Some(ref _y @ _z) => { },
    |              ---------^^
    |              |        |
    |              |        by-move pattern here
-   |              both by-ref and by-move used
+   |              by-ref pattern here
 
 error: aborting due to previous error
 
index fbf9dfff7feec7d13c5a105b2d057dd4b3b9b6a5..af0bcb69bbbeace1d6c9ae980471d74b033e0c17 100644 (file)
@@ -25,7 +25,7 @@ LL |     let ref a @ box b = Box::new(NC);
    |         ------------^
    |         |           |
    |         |           by-move pattern here
-   |         both by-ref and by-move used
+   |         by-ref pattern here
 
 error[E0382]: use of moved value
   --> $DIR/borrowck-pat-at-and-box.rs:11:18
index 9c8a4e25fb8447a08d728e59663dffa9cbb378e0..36dc1b2879263ab46415d838dac219afae43187c 100644 (file)
@@ -13,7 +13,7 @@ LL |         ref op_string_ref @ Some(s) => {},
    |         -------------------------^-
    |         |                        |
    |         |                        by-move pattern here
-   |         both by-ref and by-move used
+   |         by-ref pattern here
 
 error: aborting due to previous error
 
index ce79b562ec99bc013926608d924d8a3cbb877897..27b94e055a0a4fdb24199f1726bd09785e9a446e 100644 (file)
@@ -124,5 +124,5 @@ LL |     drop(a);
 
 error: aborting due to 11 previous errors
 
-Some errors have detailed explanations: E0502, E0507.
+Some errors have detailed explanations: E0502, E0507, E0594.
 For more information about an error, try `rustc --explain E0502`.
index bb0d893887e29d8b973d88b2d265f85a8c8e2ad4..3cd756d11ce5ea43b2c7c572bc61d0b1a51a868a 100644 (file)
@@ -13,7 +13,7 @@ LL |     let ref a @ b = NotCopy;
    |         --------^
    |         |       |
    |         |       by-move pattern here
-   |         both by-ref and by-move used
+   |         by-ref pattern here
 
 error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/default-binding-modes-both-sides-independent.rs:22:21
@@ -22,17 +22,18 @@ LL |     let ref mut a @ b = NotCopy;
    |         ------------^
    |         |           |
    |         |           by-move pattern here
-   |         both by-ref and by-move used
+   |         by-ref pattern here
 
 error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/default-binding-modes-both-sides-independent.rs:24:20
    |
 LL |         Ok(ref a @ b) | Err(ref a @ b) => {}
-   |                    ^        --------^
-   |                    |        |       |
-   |                    |        |       by-move pattern here
-   |                    |        both by-ref and by-move used
-   |                    by-move pattern here
+   |            --------^        --------^
+   |            |       |        |       |
+   |            |       |        |       by-move pattern here
+   |            |       |        by-ref pattern here
+   |            |       by-move pattern here
+   |            by-ref pattern here
 
 error[E0009]: cannot bind by-move and by-ref in the same pattern
   --> $DIR/default-binding-modes-both-sides-independent.rs:28:17
@@ -41,7 +42,7 @@ LL |         ref a @ b => {}
    |         --------^
    |         |       |
    |         |       by-move pattern here
-   |         both by-ref and by-move used
+   |         by-ref pattern here
 
 error: aborting due to 4 previous errors
 
index 8a1ded1d5b94a2c2dcaba369a51e57ccfc3c6814..ebc6ff5d8c3fe5e63dbb9832447bad3cbb0e1a5b 100644 (file)
@@ -4,7 +4,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
 LL |     for (n, mut m) in &tups {
    |          -  ^^^^^ by-move pattern here
    |          |
-   |          both by-ref and by-move used
+   |          by-ref pattern here
 
 error[E0507]: cannot move out of a shared reference
   --> $DIR/for.rs:6:23