Skip to content

Commit bd318be

Browse files
committed
move_ref_pattern: change pov in diagnostics & add binding names
1 parent 8d4973f commit bd318be

15 files changed

+501
-489
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

+44-32
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
658658
name,
659659
tables.node_type(pat.hir_id),
660660
);
661-
sess.struct_span_err(pat.span, &format!("borrow of moved value: `{}`", name))
662-
.span_label(binding_span, "value moved here")
661+
sess.struct_span_err(pat.span, "borrow of moved value")
662+
.span_label(binding_span, format!("value moved into `{}` here", name))
663663
.span_label(binding_span, occurs_because)
664664
.span_labels(conflicts_ref, "value borrowed here after move")
665665
.emit();
@@ -675,50 +675,62 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat<'_
675675
let mut conflicts_move = Vec::new();
676676
let mut conflicts_mut_mut = Vec::new();
677677
let mut conflicts_mut_ref = Vec::new();
678-
sub.each_binding(|_, hir_id, span, _| match tables.extract_binding_mode(sess, hir_id, span) {
679-
Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) {
680-
(Mutability::Not, Mutability::Not) => {} // Both sides are `ref`.
681-
(Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push(span), // 2x `ref mut`.
682-
_ => conflicts_mut_ref.push(span), // `ref` + `ref mut` in either direction.
683-
},
684-
Some(ty::BindByValue(_)) if is_binding_by_move(cx, hir_id, span) => {
685-
conflicts_move.push(span) // `ref mut?` + by-move conflict.
678+
sub.each_binding(|_, hir_id, span, name| {
679+
match tables.extract_binding_mode(sess, hir_id, span) {
680+
Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) {
681+
(Mutability::Not, Mutability::Not) => {} // Both sides are `ref`.
682+
(Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push((span, name)), // 2x `ref mut`.
683+
_ => conflicts_mut_ref.push((span, name)), // `ref` + `ref mut` in either direction.
684+
},
685+
Some(ty::BindByValue(_)) if is_binding_by_move(cx, hir_id, span) => {
686+
conflicts_move.push((span, name)) // `ref mut?` + by-move conflict.
687+
}
688+
Some(ty::BindByValue(_)) | None => {} // `ref mut?` + by-copy is fine.
686689
}
687-
Some(ty::BindByValue(_)) | None => {} // `ref mut?` + by-copy is fine.
688690
});
689691

690692
// Report errors if any.
691693
if !conflicts_mut_mut.is_empty() {
692694
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
693-
let msg = &format!("cannot borrow `{}` as mutable more than once at a time", name);
694-
sess.struct_span_err(pat.span, msg)
695-
.span_label(binding_span, "first mutable borrow occurs here")
696-
.span_labels(conflicts_mut_mut, "another mutable borrow occurs here")
697-
.span_labels(conflicts_mut_ref, "also borrowed as immutable here")
698-
.span_labels(conflicts_move, "also moved here")
699-
.emit();
695+
let mut err = sess
696+
.struct_span_err(pat.span, "cannot borrow value as mutable more than once at a time");
697+
err.span_label(binding_span, format!("first mutable borrow, by `{}`, occurs here", name));
698+
for (span, name) in conflicts_mut_mut {
699+
err.span_label(span, format!("another mutable borrow, by `{}`, occurs here", name));
700+
}
701+
for (span, name) in conflicts_mut_ref {
702+
err.span_label(span, format!("also borrowed as immutable, by `{}`, here", name));
703+
}
704+
for (span, name) in conflicts_move {
705+
err.span_label(span, format!("also moved into `{}` here", name));
706+
}
707+
err.emit();
700708
} else if !conflicts_mut_ref.is_empty() {
701709
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
702710
let (primary, also) = match mut_outer {
703711
Mutability::Mut => ("mutable", "immutable"),
704712
Mutability::Not => ("immutable", "mutable"),
705713
};
706-
let msg = &format!(
707-
"cannot borrow `{}` as {} because it is also borrowed as {}",
708-
name, also, primary,
709-
);
710-
sess.struct_span_err(pat.span, msg)
711-
.span_label(binding_span, format!("{} borrow occurs here", primary))
712-
.span_labels(conflicts_mut_ref, format!("{} borrow occurs here", also))
713-
.span_labels(conflicts_move, "also moved here")
714-
.emit();
714+
let msg =
715+
format!("cannot borrow value as {} because it is also borrowed as {}", also, primary);
716+
let mut err = sess.struct_span_err(pat.span, &msg);
717+
err.span_label(binding_span, format!("{} borrow, by `{}`, occurs here", primary, name));
718+
for (span, name) in conflicts_mut_ref {
719+
err.span_label(span, format!("{} borrow, by `{}`, occurs here", also, name));
720+
}
721+
for (span, name) in conflicts_move {
722+
err.span_label(span, format!("also moved into `{}` here", name));
723+
}
724+
err.emit();
715725
} else if !conflicts_move.is_empty() {
716726
// Report by-ref and by-move conflicts, e.g. `ref x @ y`.
717-
let msg = &format!("cannot move out of `{}` because it is borrowed", name);
718-
sess.struct_span_err(pat.span, msg)
719-
.span_label(binding_span, format!("borrow of `{}` occurs here", name))
720-
.span_labels(conflicts_move, format!("move out of `{}` occurs here", name))
721-
.emit();
727+
let mut err =
728+
sess.struct_span_err(pat.span, "cannot move out of value because it is borrowed");
729+
err.span_label(binding_span, format!("value borrowed, by `{}`, here", name));
730+
for (span, name) in conflicts_move {
731+
err.span_label(span, format!("value moved into `{}` here", name));
732+
}
733+
err.emit();
722734
}
723735
}
724736

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct X {
1212
fn main() {
1313
let x = Some(X { x: () });
1414
match x {
15-
Some(ref _y @ _z) => {} //~ ERROR cannot move out of `_y` because it is borrowed
15+
Some(ref _y @ _z) => {} //~ ERROR cannot move out of value because it is borrowed
1616
None => panic!(),
1717
}
1818

@@ -26,7 +26,7 @@ fn main() {
2626

2727
let mut x = Some(X { x: () });
2828
match x {
29-
Some(ref mut _y @ _z) => {} //~ ERROR cannot move out of `_y` because it is borrowed
29+
Some(ref mut _y @ _z) => {} //~ ERROR cannot move out of value because it is borrowed
3030
None => panic!(),
3131
}
3232

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
error: cannot move out of `_y` because it is borrowed
1+
error: cannot move out of value because it is borrowed
22
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:15:14
33
|
44
LL | Some(ref _y @ _z) => {}
55
| ------^^^--
66
| | |
7-
| | move out of `_y` occurs here
8-
| borrow of `_y` occurs here
7+
| | value moved into `_z` here
8+
| value borrowed, by `_y`, here
99

10-
error: borrow of moved value: `_z`
10+
error: borrow of moved value
1111
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:21:14
1212
|
1313
LL | Some(_z @ ref _y) => {}
1414
| --^^^------
1515
| | |
1616
| | value borrowed here after move
17-
| value moved here
17+
| value moved into `_z` here
1818
| move occurs because `_z` has type `X` which does implement the `Copy` trait
1919

20-
error: cannot move out of `_y` because it is borrowed
20+
error: cannot move out of value because it is borrowed
2121
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:29:14
2222
|
2323
LL | Some(ref mut _y @ _z) => {}
2424
| ----------^^^--
2525
| | |
26-
| | move out of `_y` occurs here
27-
| borrow of `_y` occurs here
26+
| | value moved into `_z` here
27+
| value borrowed, by `_y`, here
2828

29-
error: borrow of moved value: `_z`
29+
error: borrow of moved value
3030
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:35:14
3131
|
3232
LL | Some(_z @ ref mut _y) => {}
3333
| --^^^----------
3434
| | |
3535
| | value borrowed here after move
36-
| value moved here
36+
| value moved into `_z` here
3737
| move occurs because `_z` has type `X` which does implement the `Copy` trait
3838

3939
error[E0382]: borrow of moved value

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,37 @@ fn main() {
3434
a @ box b => {} //~ ERROR use of moved value
3535
}
3636

37-
let ref a @ box b = Box::new(NC); //~ ERROR cannot move out of `a` because it is borrowed
37+
let ref a @ box b = Box::new(NC); //~ ERROR cannot move out of value because it is borrowed
3838

3939
let ref a @ box ref mut b = Box::new(nc());
40-
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
40+
//~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
4141
let ref a @ box ref mut b = Box::new(NC);
42-
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
42+
//~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
4343
let ref a @ box ref mut b = Box::new(NC);
44-
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
44+
//~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
4545
*b = NC;
4646
let ref a @ box ref mut b = Box::new(NC);
47-
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
47+
//~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
4848
//~| ERROR cannot borrow `_` as mutable because it is also borrowed as immutable
4949
*b = NC;
5050
drop(a);
5151

5252
let ref mut a @ box ref b = Box::new(NC);
53-
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
53+
//~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
5454
//~| ERROR cannot borrow `_` as immutable because it is also borrowed as mutable
5555
*a = Box::new(NC);
5656
drop(b);
5757

5858
fn f5(ref mut a @ box ref b: Box<NC>) {
59-
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
59+
//~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
6060
//~| ERROR cannot borrow `_` as immutable because it is also borrowed as mutable
6161
*a = Box::new(NC);
6262
drop(b);
6363
}
6464

6565
match Box::new(nc()) {
6666
ref mut a @ box ref b => {
67-
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
67+
//~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
6868
//~| ERROR cannot borrow `_` as immutable because it is also borrowed as mutable
6969
*a = Box::new(NC);
7070
drop(b);

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

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
1-
error: cannot move out of `a` because it is borrowed
1+
error: cannot move out of value because it is borrowed
22
--> $DIR/borrowck-pat-at-and-box.rs:37:9
33
|
44
LL | let ref a @ box b = Box::new(NC);
55
| -----^^^^^^^-
66
| | |
7-
| | move out of `a` occurs here
8-
| borrow of `a` occurs here
7+
| | value moved into `b` here
8+
| value borrowed, by `a`, here
99

10-
error: cannot borrow `a` as mutable because it is also borrowed as immutable
10+
error: cannot borrow value as mutable because it is also borrowed as immutable
1111
--> $DIR/borrowck-pat-at-and-box.rs:39:9
1212
|
1313
LL | let ref a @ box ref mut b = Box::new(nc());
1414
| -----^^^^^^^---------
1515
| | |
16-
| | mutable borrow occurs here
17-
| immutable borrow occurs here
16+
| | mutable borrow, by `b`, occurs here
17+
| immutable borrow, by `a`, occurs here
1818

19-
error: cannot borrow `a` as mutable because it is also borrowed as immutable
19+
error: cannot borrow value as mutable because it is also borrowed as immutable
2020
--> $DIR/borrowck-pat-at-and-box.rs:41:9
2121
|
2222
LL | let ref a @ box ref mut b = Box::new(NC);
2323
| -----^^^^^^^---------
2424
| | |
25-
| | mutable borrow occurs here
26-
| immutable borrow occurs here
25+
| | mutable borrow, by `b`, occurs here
26+
| immutable borrow, by `a`, occurs here
2727

28-
error: cannot borrow `a` as mutable because it is also borrowed as immutable
28+
error: cannot borrow value as mutable because it is also borrowed as immutable
2929
--> $DIR/borrowck-pat-at-and-box.rs:43:9
3030
|
3131
LL | let ref a @ box ref mut b = Box::new(NC);
3232
| -----^^^^^^^---------
3333
| | |
34-
| | mutable borrow occurs here
35-
| immutable borrow occurs here
34+
| | mutable borrow, by `b`, occurs here
35+
| immutable borrow, by `a`, occurs here
3636

37-
error: cannot borrow `a` as mutable because it is also borrowed as immutable
37+
error: cannot borrow value as mutable because it is also borrowed as immutable
3838
--> $DIR/borrowck-pat-at-and-box.rs:46:9
3939
|
4040
LL | let ref a @ box ref mut b = Box::new(NC);
4141
| -----^^^^^^^---------
4242
| | |
43-
| | mutable borrow occurs here
44-
| immutable borrow occurs here
43+
| | mutable borrow, by `b`, occurs here
44+
| immutable borrow, by `a`, occurs here
4545

46-
error: cannot borrow `a` as immutable because it is also borrowed as mutable
46+
error: cannot borrow value as immutable because it is also borrowed as mutable
4747
--> $DIR/borrowck-pat-at-and-box.rs:52:9
4848
|
4949
LL | let ref mut a @ box ref b = Box::new(NC);
5050
| ---------^^^^^^^-----
5151
| | |
52-
| | immutable borrow occurs here
53-
| mutable borrow occurs here
52+
| | immutable borrow, by `b`, occurs here
53+
| mutable borrow, by `a`, occurs here
5454

55-
error: cannot borrow `a` as immutable because it is also borrowed as mutable
55+
error: cannot borrow value as immutable because it is also borrowed as mutable
5656
--> $DIR/borrowck-pat-at-and-box.rs:66:9
5757
|
5858
LL | ref mut a @ box ref b => {
5959
| ---------^^^^^^^-----
6060
| | |
61-
| | immutable borrow occurs here
62-
| mutable borrow occurs here
61+
| | immutable borrow, by `b`, occurs here
62+
| mutable borrow, by `a`, occurs here
6363

64-
error: cannot borrow `a` as immutable because it is also borrowed as mutable
64+
error: cannot borrow value as immutable because it is also borrowed as mutable
6565
--> $DIR/borrowck-pat-at-and-box.rs:58:11
6666
|
6767
LL | fn f5(ref mut a @ box ref b: Box<NC>) {
6868
| ---------^^^^^^^-----
6969
| | |
70-
| | immutable borrow occurs here
71-
| mutable borrow occurs here
70+
| | immutable borrow, by `b`, occurs here
71+
| mutable borrow, by `a`, occurs here
7272

7373
error[E0382]: use of moved value
7474
--> $DIR/borrowck-pat-at-and-box.rs:21:18

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: borrow of moved value: `a`
1+
error: borrow of moved value
22
--> $DIR/borrowck-pat-by-move-and-ref-inverse-promotion.rs:9:9
33
|
44
LL | let a @ ref b = U;
55
| -^^^-----
66
| | |
77
| | value borrowed here after move
8-
| value moved here
8+
| value moved into `a` here
99
| move occurs because `a` has type `main::U` which does implement the `Copy` trait
1010

1111
error: aborting due to previous error

0 commit comments

Comments
 (0)