Skip to content

Commit 6a87f99

Browse files
committed
check_legality_of_move_bindings: generalize diagnostics & add comments
1 parent 25b6a28 commit 6a87f99

12 files changed

+33
-30
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -579,18 +579,20 @@ fn maybe_point_at_variant(ty: Ty<'_>, patterns: &[super::Pat<'_>]) -> Vec<Span>
579579

580580
// Check the legality of legality of by-move bindings.
581581
fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: bool, pat: &Pat) {
582-
let mut by_ref_span = None;
582+
// Find all by-ref spans.
583+
let mut by_ref_spans = Vec::new();
583584
pat.each_binding(|_, hir_id, span, _| {
584585
if let Some(&bm) = cx.tables.pat_binding_modes().get(hir_id) {
585586
if let ty::BindByReference(..) = bm {
586-
by_ref_span = Some(span);
587+
by_ref_spans.push(span);
587588
}
588589
} else {
589590
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
590591
}
591592
});
592593

593-
let span_vec = &mut Vec::new();
594+
// Find bad by-move spans:
595+
let by_move_spans = &mut Vec::new();
594596
let mut check_move = |p: &Pat, sub: Option<&Pat>| {
595597
// Check legality of moving out of the enum.
596598
//
@@ -599,11 +601,10 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
599601
struct_span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings")
600602
.span_label(p.span, "binds an already bound by-move value by moving it")
601603
.emit();
602-
} else if !has_guard && by_ref_span.is_some() {
603-
span_vec.push(p.span);
604+
} else if !has_guard && !by_ref_spans.is_empty() {
605+
by_move_spans.push(p.span);
604606
}
605607
};
606-
607608
pat.walk(|p| {
608609
if let hir::PatKind::Binding(.., sub) = &p.kind {
609610
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
620621
true
621622
});
622623

623-
if !span_vec.is_empty() {
624+
// Found some bad by-move spans, error!
625+
if !by_move_spans.is_empty() {
624626
let mut err = struct_span_err!(
625627
cx.tcx.sess,
626-
MultiSpan::from_spans(span_vec.clone()),
628+
MultiSpan::from_spans(by_move_spans.clone()),
627629
E0009,
628630
"cannot bind by-move and by-ref in the same pattern",
629631
);
630-
if let Some(by_ref_span) = by_ref_span {
631-
err.span_label(by_ref_span, "both by-ref and by-move used");
632+
for span in by_ref_spans.iter() {
633+
err.span_label(*span, "by-ref pattern here");
632634
}
633-
for span in span_vec.iter() {
635+
for span in by_move_spans.iter() {
634636
err.span_label(*span, "by-move pattern here");
635637
}
636638
err.emit();

src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
44
LL | Some((ref _y, _z)) => { },
55
| ------ ^^ by-move pattern here
66
| |
7-
| both by-ref and by-move used
7+
| by-ref pattern here
88

99
error: aborting due to previous error
1010

src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-3.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
44
LL | DoubleOption::Some2(ref _y, _z) => { },
55
| ------ ^^ by-move pattern here
66
| |
7-
| both by-ref and by-move used
7+
| by-ref pattern here
88

99
error: aborting due to previous error
1010

src/test/ui/bind-by-move/bind-by-move-neither-can-live-while-the-other-survives-4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
22
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-4.rs:12:15
33
|
44
LL | Some((_y, ref _z)) => { },
5-
| ^^ ------ both by-ref and by-move used
5+
| ^^ ------ by-ref pattern here
66
| |
77
| by-move pattern here
88

src/test/ui/error-codes/E0009.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
22
--> $DIR/E0009.rs:5:15
33
|
44
LL | Some((y, ref z)) => {},
5-
| ^ ----- both by-ref and by-move used
5+
| ^ ----- by-ref pattern here
66
| |
77
| by-move pattern here
88

src/test/ui/issues/issue-53840.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
22
--> $DIR/issue-53840.rs:13:16
33
|
44
LL | E::Foo(a, b, ref c) => {}
5-
| ^ ^ ----- both by-ref and by-move used
5+
| ^ ^ ----- by-ref pattern here
66
| | |
77
| | by-move pattern here
88
| by-move pattern here
@@ -11,7 +11,7 @@ error[E0009]: cannot bind by-move and by-ref in the same pattern
1111
--> $DIR/issue-53840.rs:17:14
1212
|
1313
LL | Bar {a, ref b} => {}
14-
| ^ ----- both by-ref and by-move used
14+
| ^ ----- by-ref pattern here
1515
| |
1616
| by-move pattern here
1717

src/test/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | Some(ref _y @ _z) => { },
1313
| ---------^^
1414
| | |
1515
| | by-move pattern here
16-
| both by-ref and by-move used
16+
| by-ref pattern here
1717

1818
error: aborting due to previous error
1919

src/test/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | let ref a @ box b = Box::new(NC);
2525
| ------------^
2626
| | |
2727
| | by-move pattern here
28-
| both by-ref and by-move used
28+
| by-ref pattern here
2929

3030
error[E0382]: use of moved value
3131
--> $DIR/borrowck-pat-at-and-box.rs:11:18

src/test/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | ref op_string_ref @ Some(s) => {},
1313
| -------------------------^-
1414
| | |
1515
| | by-move pattern here
16-
| both by-ref and by-move used
16+
| by-ref pattern here
1717

1818
error: aborting due to previous error
1919

src/test/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ LL | drop(a);
124124

125125
error: aborting due to 11 previous errors
126126

127-
Some errors have detailed explanations: E0502, E0507.
127+
Some errors have detailed explanations: E0502, E0507, E0594.
128128
For more information about an error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)