Skip to content

Commit be6734a

Browse files
committed
Move reason for move to label
1 parent 885011e commit be6734a

8 files changed

+86
-53
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -705,28 +705,28 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
705705
}, " (into closure)"),
706706
};
707707

708+
let extra_move_label = if need_note {
709+
format!(" because it has type `{}`, which does not implement the `Copy` trait",
710+
moved_lp.ty)
711+
} else {
712+
String::new()
713+
};
708714
// Annotate the use and the move in the span. Watch out for
709715
// the case where the use and the move are the same. This
710716
// means the use is in a loop.
711717
err = if use_span == move_span {
712718
err.span_label(
713719
use_span,
714-
format!("value moved{} here in previous iteration of loop",
715-
move_note));
720+
format!("value moved{} here in previous iteration of loop{}",
721+
move_note,
722+
extra_move_label));
716723
err
717724
} else {
718725
err.span_label(use_span, format!("value {} here after move", verb_participle))
719-
.span_label(move_span, format!("value moved{} here", move_note));
726+
.span_label(move_span, format!("value moved{} here{}", move_note, extra_move_label));
720727
err
721728
};
722729

723-
if need_note {
724-
err.note(&format!("move occurs because `{}` has type `{}`, \
725-
which does not implement the `Copy` trait",
726-
self.loan_path_to_string(moved_lp),
727-
moved_lp.ty));
728-
}
729-
730730
// Note: we used to suggest adding a `ref binding` or calling
731731
// `clone` but those suggestions have been removed because
732732
// they are often not what you actually want to do, and were
@@ -1391,7 +1391,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
13911391
LpDowncast(ref lp_base, variant_def_id) => {
13921392
out.push('(');
13931393
self.append_autoderefd_loan_path_to_string(&lp_base, out);
1394-
out.push(':');
1394+
out.push_str(DOWNCAST_PRINTED_OPERATOR);
13951395
out.push_str(&self.tcx.item_path_str(variant_def_id));
13961396
out.push(')');
13971397
}

src/test/compile-fail/issue-24357.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ struct NoCopy;
1212
fn main() {
1313
let x = NoCopy;
1414
let f = move || { let y = x; };
15-
//~^ value moved (into closure) here
15+
//~^ NOTE value moved (into closure) here because it has type `NoCopy`, which does not
1616
let z = x;
1717
//~^ ERROR use of moved value: `x`
18-
//~| value used here after move
19-
//~| move occurs because `x` has type `NoCopy`
18+
//~| NOTE value used here after move
2019
}

src/test/compile-fail/moves-based-on-type-distribute-copy-over-paren.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ fn touch<A>(_a: &A) {}
1717
fn f00() {
1818
let x = "hi".to_string();
1919
let _y = Foo { f:x };
20-
//~^ value moved here
20+
//~^ NOTE value moved here because it has type
2121
touch(&x); //~ ERROR use of moved value: `x`
22-
//~^ value used here after move
23-
//~| move occurs because `x` has type `std::string::String`
22+
//~^ NOTE value used here after move
2423
}
2524

2625
fn f05() {
2726
let x = "hi".to_string();
2827
let _y = Foo { f:(((x))) };
29-
//~^ value moved here
28+
//~^ NOTE value moved here because it has type
3029
touch(&x); //~ ERROR use of moved value: `x`
30+
//~^ NOTE value used here after move
3131
}
3232

3333
fn f10() {

src/test/ui/augmented-assignments.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ impl AddAssign for Int {
2020

2121
fn main() {
2222
let mut x = Int(1);
23-
x //~ error: use of moved value: `x`
24-
//~^ value used here after move
23+
x
24+
//~^ error: use of moved value: `x`
25+
//~| note: value used here after move
2526
+=
26-
x; //~ value moved here
27+
x;
28+
//~^ note: value moved here because it has type `Int`, which does not implement the `Copy`
2729

2830
let y = Int(2);
29-
//~^ consider changing this to `mut y`
30-
y //~ error: cannot borrow immutable local variable `y` as mutable
31-
//~| cannot borrow
31+
//~^ note: consider changing this to `mut y`
32+
y
33+
//~^ error: cannot borrow immutable local variable `y` as mutable
34+
//~| note: cannot borrow mutably
3235
+=
3336
Int(1);
3437
}

src/test/ui/borrowck/borrowck-box-insensitivity.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,33 @@ struct D {
3333
fn copy_after_move() {
3434
let a: Box<_> = box A { x: box 0, y: 1 };
3535
let _x = a.x;
36-
//~^ value moved here
37-
let _y = a.y; //~ ERROR use of moved
38-
//~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
39-
//~| value used here after move
36+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
37+
let _y = a.y; //~ ERROR use of moved value
38+
//~^ NOTE value used here after move
4039
}
4140

4241
fn move_after_move() {
4342
let a: Box<_> = box B { x: box 0, y: box 1 };
4443
let _x = a.x;
45-
//~^ value moved here
44+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
4645
let _y = a.y; //~ ERROR use of moved
47-
//~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
48-
//~| value used here after move
46+
//~^ NOTE value used here after move
4947
}
5048

5149
fn borrow_after_move() {
5250
let a: Box<_> = box A { x: box 0, y: 1 };
5351
let _x = a.x;
54-
//~^ value moved here
52+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
5553
let _y = &a.y; //~ ERROR use of moved
56-
//~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
57-
//~| value used here after move
54+
//~^ NOTE value used here after move
5855
}
5956

6057
fn move_after_borrow() {
6158
let a: Box<_> = box B { x: box 0, y: box 1 };
6259
let _x = &a.x;
6360
let _y = a.y;
6461
//~^ ERROR cannot move
65-
//~| move out of
62+
//~| NOTE move out of
6663
}
6764

6865
fn copy_after_mut_borrow() {
@@ -76,54 +73,54 @@ fn move_after_mut_borrow() {
7673
let _x = &mut a.x;
7774
let _y = a.y;
7875
//~^ ERROR cannot move
79-
//~| move out of
76+
//~| NOTE move out of
8077
}
8178

8279
fn borrow_after_mut_borrow() {
8380
let mut a: Box<_> = box A { x: box 0, y: 1 };
8481
let _x = &mut a.x;
8582
let _y = &a.y; //~ ERROR cannot borrow
86-
//~^ immutable borrow occurs here (via `a.y`)
83+
//~^ NOTE immutable borrow occurs here (via `a.y`)
8784
}
8885

8986
fn mut_borrow_after_borrow() {
9087
let mut a: Box<_> = box A { x: box 0, y: 1 };
9188
let _x = &a.x;
9289
let _y = &mut a.y; //~ ERROR cannot borrow
93-
//~^ mutable borrow occurs here (via `a.y`)
90+
//~^ NOTE mutable borrow occurs here (via `a.y`)
9491
}
9592

9693
fn copy_after_move_nested() {
9794
let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
9895
let _x = a.x.x;
99-
//~^ value moved here
96+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
10097
let _y = a.y; //~ ERROR use of collaterally moved
101-
//~| value used here after move
98+
//~^ NOTE value used here after move
10299
}
103100

104101
fn move_after_move_nested() {
105102
let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
106103
let _x = a.x.x;
107-
//~^ value moved here
104+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
108105
let _y = a.y; //~ ERROR use of collaterally moved
109-
//~| value used here after move
106+
//~^ NOTE value used here after move
110107
}
111108

112109
fn borrow_after_move_nested() {
113110
let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
114111
let _x = a.x.x;
115-
//~^ value moved here
112+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
116113
let _y = &a.y; //~ ERROR use of collaterally moved
117-
//~| value used here after move
114+
//~^ NOTE value used here after move
118115
}
119116

120117
fn move_after_borrow_nested() {
121118
let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
122119
let _x = &a.x.x;
123-
//~^ borrow of `a.x.x` occurs here
120+
//~^ NOTE borrow of `a.x.x` occurs here
124121
let _y = a.y;
125122
//~^ ERROR cannot move
126-
//~| move out of
123+
//~| NOTE move out of
127124
}
128125

129126
fn copy_after_mut_borrow_nested() {
@@ -137,23 +134,23 @@ fn move_after_mut_borrow_nested() {
137134
let _x = &mut a.x.x;
138135
let _y = a.y;
139136
//~^ ERROR cannot move
140-
//~| move out of
137+
//~| NOTE move out of
141138
}
142139

143140
fn borrow_after_mut_borrow_nested() {
144141
let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
145142
let _x = &mut a.x.x;
146-
//~^ mutable borrow occurs here
143+
//~^ NOTE mutable borrow occurs here
147144
let _y = &a.y; //~ ERROR cannot borrow
148-
//~^ immutable borrow occurs here
145+
//~^ NOTE immutable borrow occurs here
149146
}
150147

151148
fn mut_borrow_after_borrow_nested() {
152149
let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
153150
let _x = &a.x.x;
154-
//~^ immutable borrow occurs here
151+
//~^ NOTE immutable borrow occurs here
155152
let _y = &mut a.y; //~ ERROR cannot borrow
156-
//~^ mutable borrow occurs here
153+
//~^ NOTE mutable borrow occurs here
157154
}
158155

159156
fn main() {

src/test/ui/borrowck/issue-41962.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main(){
12+
let maybe = Some(vec![true, true]);
13+
14+
loop {
15+
if let Some(thing) = maybe {
16+
}
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0382]: use of partially moved value: `maybe`
2+
--> $DIR/issue-41962.rs:15:30
3+
|
4+
15 | if let Some(thing) = maybe {
5+
| ----- ^^^^^ value used here after move
6+
| |
7+
| value moved here because it has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
8+
9+
error[E0382]: use of moved value: `(maybe as std::prelude::v1::Some).0`
10+
--> $DIR/issue-41962.rs:15:21
11+
|
12+
15 | if let Some(thing) = maybe {
13+
| ^^^^^ value moved here in previous iteration of loop because it has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
14+
15+
error: aborting due to 2 previous errors
16+

src/test/ui/moves-based-on-type-match-bindings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ fn f10() {
2121

2222
let y = match x {
2323
Foo {f} => {}
24+
//~^ NOTE value moved here because it has type `std::string::String`, which does not
2425
};
2526

2627
touch(&x); //~ ERROR use of partially moved value: `x`
27-
//~^ value used here after move
28-
//~| move occurs because `x.f` has type `std::string::String`
28+
//~^ NOTE value used here after move
2929
}
3030

3131
fn main() {}

0 commit comments

Comments
 (0)