Skip to content

Commit 79e4371

Browse files
committed
Move reason for move to label
1 parent 088216f commit 79e4371

8 files changed

+87
-58
lines changed

src/librustc_borrowck/borrowck/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -664,28 +664,28 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
664664
}, " (into closure)"),
665665
};
666666

667+
let extra_move_label = if need_note {
668+
format!(" because it has type `{}`, which does not implement the `Copy` trait",
669+
moved_lp.ty)
670+
} else {
671+
String::new()
672+
};
667673
// Annotate the use and the move in the span. Watch out for
668674
// the case where the use and the move are the same. This
669675
// means the use is in a loop.
670676
err = if use_span == move_span {
671677
err.span_label(
672678
use_span,
673-
format!("value moved{} here in previous iteration of loop",
674-
move_note));
679+
format!("value moved{} here in previous iteration of loop{}",
680+
move_note,
681+
extra_move_label));
675682
err
676683
} else {
677684
err.span_label(use_span, format!("value {} here after move", verb_participle))
678-
.span_label(move_span, format!("value moved{} here", move_note));
685+
.span_label(move_span, format!("value moved{} here{}", move_note, extra_move_label));
679686
err
680687
};
681688

682-
if need_note {
683-
err.note(&format!("move occurs because `{}` has type `{}`, \
684-
which does not implement the `Copy` trait",
685-
self.loan_path_to_string(moved_lp),
686-
moved_lp.ty));
687-
}
688-
689689
// Note: we used to suggest adding a `ref binding` or calling
690690
// `clone` but those suggestions have been removed because
691691
// they are often not what you actually want to do, and were
@@ -1365,7 +1365,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
13651365
LpDowncast(ref lp_base, variant_def_id) => {
13661366
out.push('(');
13671367
self.append_autoderefd_loan_path_to_string(&lp_base, out);
1368-
out.push(':');
1368+
out.push_str(DOWNCAST_PRINTED_OPERATOR);
13691369
out.push_str(&self.tcx.item_path_str(variant_def_id));
13701370
out.push(')');
13711371
}

src/test/compile-fail/augmented-assignments.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,16 +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
25-
//~| note: move occurs because `x` has type `Int`
23+
x
24+
//~^ error: use of moved value: `x`
25+
//~| note: value used here after move
2626
+=
27-
x; //~ value moved here
27+
x;
28+
//~^ note: value moved here because it has type `Int`, which does not implement the `Copy`
2829

2930
let y = Int(2);
30-
//~^ consider changing this to `mut y`
31-
y //~ error: cannot borrow immutable local variable `y` as mutable
32-
//~| 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
3335
+=
3436
Int(1);
3537
}

src/test/compile-fail/borrowck/borrowck-box-insensitivity.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,25 @@ 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() {
@@ -63,7 +60,7 @@ fn move_after_borrow() {
6360
//~^ NOTE borrow of `a.x` occurs here
6461
let _y = a.y;
6562
//~^ ERROR cannot move
66-
//~| move out of
63+
//~| NOTE move out of
6764
}
6865

6966
fn copy_after_mut_borrow() {
@@ -80,15 +77,15 @@ fn move_after_mut_borrow() {
8077
//~^ NOTE borrow of `a.x` occurs here
8178
let _y = a.y;
8279
//~^ ERROR cannot move
83-
//~| move out of
80+
//~| NOTE move out of
8481
}
8582

8683
fn borrow_after_mut_borrow() {
8784
let mut a: Box<_> = box A { x: box 0, y: 1 };
8885
let _x = &mut a.x;
8986
//~^ NOTE mutable borrow occurs here (via `a.x`)
9087
let _y = &a.y; //~ ERROR cannot borrow
91-
//~^ immutable borrow occurs here (via `a.y`)
88+
//~^ NOTE immutable borrow occurs here (via `a.y`)
9289
}
9390
//~^ NOTE mutable borrow ends here
9491

@@ -97,44 +94,41 @@ fn mut_borrow_after_borrow() {
9794
let _x = &a.x;
9895
//~^ NOTE immutable borrow occurs here (via `a.x`)
9996
let _y = &mut a.y; //~ ERROR cannot borrow
100-
//~^ mutable borrow occurs here (via `a.y`)
97+
//~^ NOTE mutable borrow occurs here (via `a.y`)
10198
}
10299
//~^ NOTE immutable borrow ends here
103100

104101
fn copy_after_move_nested() {
105102
let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 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-
//~^ NOTE move occurs because `a.x.x` has type `std::boxed::Box<isize>`
110-
//~| value used here after move
106+
//~^ NOTE value used here after move
111107
}
112108

113109
fn move_after_move_nested() {
114110
let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
115111
let _x = a.x.x;
116-
//~^ value moved here
112+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
117113
let _y = a.y; //~ ERROR use of collaterally moved
118-
//~^ NOTE move occurs because `a.x.x` has type `std::boxed::Box<isize>`
119-
//~| value used here after move
114+
//~^ NOTE value used here after move
120115
}
121116

122117
fn borrow_after_move_nested() {
123118
let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
124119
let _x = a.x.x;
125-
//~^ value moved here
120+
//~^ NOTE value moved here because it has type `std::boxed::Box<isize>`, which does not
126121
let _y = &a.y; //~ ERROR use of collaterally moved
127-
//~^ NOTE move occurs because `a.x.x` has type `std::boxed::Box<isize>`
128-
//~| value used here after move
122+
//~^ NOTE value used here after move
129123
}
130124

131125
fn move_after_borrow_nested() {
132126
let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
133127
let _x = &a.x.x;
134-
//~^ borrow of `a.x.x` occurs here
128+
//~^ NOTE borrow of `a.x.x` occurs here
135129
let _y = a.y;
136130
//~^ ERROR cannot move
137-
//~| move out of
131+
//~| NOTE move out of
138132
}
139133

140134
fn copy_after_mut_borrow_nested() {
@@ -151,24 +145,24 @@ fn move_after_mut_borrow_nested() {
151145
//~^ NOTE borrow of `a.x.x` occurs here
152146
let _y = a.y;
153147
//~^ ERROR cannot move
154-
//~| move out of
148+
//~| NOTE move out of
155149
}
156150

157151
fn borrow_after_mut_borrow_nested() {
158152
let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
159153
let _x = &mut a.x.x;
160-
//~^ mutable borrow occurs here
154+
//~^ NOTE mutable borrow occurs here
161155
let _y = &a.y; //~ ERROR cannot borrow
162-
//~^ immutable borrow occurs here
156+
//~^ NOTE immutable borrow occurs here
163157
}
164158
//~^ NOTE mutable borrow ends here
165159

166160
fn mut_borrow_after_borrow_nested() {
167161
let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
168162
let _x = &a.x.x;
169-
//~^ immutable borrow occurs here
163+
//~^ NOTE immutable borrow occurs here
170164
let _y = &mut a.y; //~ ERROR cannot borrow
171-
//~^ mutable borrow occurs here
165+
//~^ NOTE mutable borrow occurs here
172166
}
173167
//~^ NOTE immutable borrow ends here
174168

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

+2-3
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

+4-4
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/compile-fail/moves-based-on-type-match-bindings.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ fn f10() {
2020
let x = Foo {f: "hi".to_string()};
2121

2222
let y = match x {
23-
Foo {f} => {} //~ NOTE moved here
23+
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() {}

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

+18
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+
}
+16
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+

0 commit comments

Comments
 (0)