Skip to content

Commit 78f24d8

Browse files
committed
Auto merge of #47124 - estebank:loan-paths, r=nikomatsakis
Reword reason for move note On move errors, when encountering an enum variant, be more ambiguous and do not refer to the type on the cause note, to avoid referring to `(maybe as std::prelude::v1::Some).0`, and instead refer to `the value`. Sidesteps part of the problem with #41962: ``` error[E0382]: use of partially moved value: `maybe` --> file.rs:5:30 | 5 | if let Some(thing) = maybe { | ----- ^^^^^ value used here after move | | | value moved here = note: move occurs because the value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait error[E0382]: use of moved value: `(maybe as std::prelude::v1::Some).0` --> file.rs:5:21 | 5 | if let Some(thing) = maybe { | ^^^^^ value moved here in previous iteration of loop = note: move occurs because the value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait error: aborting due to 2 previous errors ``` Previous discussion: #44360 r? @arielb1
2 parents 608aae9 + a4d46b3 commit 78f24d8

File tree

6 files changed

+104
-21
lines changed

6 files changed

+104
-21
lines changed

src/librustc_borrowck/borrowck/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,16 @@ impl<'tcx> LoanPath<'tcx> {
346346
}
347347

348348
fn to_type(&self) -> Ty<'tcx> { self.ty }
349+
350+
fn has_downcast(&self) -> bool {
351+
match self.kind {
352+
LpDowncast(_, _) => true,
353+
LpExtend(ref lp, _, LpInterior(_, _)) => {
354+
lp.has_downcast()
355+
}
356+
_ => false,
357+
}
358+
}
349359
}
350360

351361
// FIXME (pnkfelix): See discussion here
@@ -721,16 +731,20 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
721731
move_note));
722732
err
723733
} else {
724-
err.span_label(use_span, format!("value {} here after move", verb_participle))
725-
.span_label(move_span, format!("value moved{} here", move_note));
734+
err.span_label(use_span, format!("value {} here after move", verb_participle));
735+
err.span_label(move_span, format!("value moved{} here", move_note));
726736
err
727737
};
728738

729739
if need_note {
730-
err.note(&format!("move occurs because `{}` has type `{}`, \
731-
which does not implement the `Copy` trait",
732-
self.loan_path_to_string(moved_lp),
733-
moved_lp.ty));
740+
err.note(&format!(
741+
"move occurs because {} has type `{}`, which does not implement the `Copy` trait",
742+
if moved_lp.has_downcast() {
743+
"the value".to_string()
744+
} else {
745+
format!("`{}`", self.loan_path_to_string(moved_lp))
746+
},
747+
moved_lp.ty));
734748
}
735749

736750
// Note: we used to suggest adding a `ref binding` or calling
@@ -1414,7 +1428,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
14141428
LpDowncast(ref lp_base, variant_def_id) => {
14151429
out.push('(');
14161430
self.append_autoderefd_loan_path_to_string(&lp_base, out);
1417-
out.push(':');
1431+
out.push_str(DOWNCAST_PRINTED_OPERATOR);
14181432
out.push_str(&self.tcx.item_path_str(variant_def_id));
14191433
out.push(')');
14201434
}

src/librustc_mir/borrow_check/error_reporting.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
6262
Origin::Mir,
6363
);
6464

65-
err.span_label(
66-
span,
67-
format!(
68-
"value {} here after move",
69-
desired_action.as_verb_in_past_tense()
70-
),
71-
);
65+
let mut is_loop_move = false;
7266
for moi in mois {
7367
let move_msg = ""; //FIXME: add " (into closure)"
7468
let move_span = self.mir.source_info(self.move_data.moves[*moi].source).span;
@@ -77,10 +71,20 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
7771
span,
7872
format!("value moved{} here in previous iteration of loop", move_msg),
7973
);
74+
is_loop_move = true;
8075
} else {
8176
err.span_label(move_span, format!("value moved{} here", move_msg));
8277
};
8378
}
79+
if !is_loop_move {
80+
err.span_label(
81+
span,
82+
format!(
83+
"value {} here after move",
84+
desired_action.as_verb_in_past_tense()
85+
),
86+
);
87+
}
8488

8589
if let Some(ty) = self.retrieve_type_for_place(place) {
8690
let needs_note = match ty.sty {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ 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
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
19+
//~| NOTE move occurs because `x` has type `NoCopy`
2020
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ 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
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
23+
//~| NOTE move occurs because `x` has type `std::string::String`
2424
}
2525

2626
fn f05() {
2727
let x = "hi".to_string();
2828
let _y = Foo { f:(((x))) };
29-
//~^ value moved here
29+
//~^ NOTE value moved here
3030
touch(&x); //~ ERROR use of moved value: `x`
31+
//~^ NOTE value used here after move
32+
//~| NOTE move occurs because `x` has type `std::string::String`
3133
}
3234

3335
fn f10() {

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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 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+
// compile-flags: -Z borrowck=compare
12+
13+
pub fn main(){
14+
let maybe = Some(vec![true, true]);
15+
16+
loop {
17+
if let Some(thing) = maybe {
18+
//~^ ERROR use of partially moved value: `maybe` (Ast) [E0382]
19+
//~| ERROR use of moved value: `(maybe as std::prelude::v1::Some).0` (Ast) [E0382]
20+
//~| ERROR use of moved value: `maybe` (Mir) [E0382]
21+
//~| ERROR use of moved value: `maybe.0` (Mir) [E0382]
22+
}
23+
}
24+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0382]: use of partially moved value: `maybe` (Ast)
2+
--> $DIR/issue-41962.rs:17:30
3+
|
4+
17 | if let Some(thing) = maybe {
5+
| ----- ^^^^^ value used here after move
6+
| |
7+
| value moved here
8+
|
9+
= note: move occurs because the value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
10+
11+
error[E0382]: use of moved value: `(maybe as std::prelude::v1::Some).0` (Ast)
12+
--> $DIR/issue-41962.rs:17:21
13+
|
14+
17 | if let Some(thing) = maybe {
15+
| ^^^^^ value moved here in previous iteration of loop
16+
|
17+
= note: move occurs because the value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
18+
19+
error[E0382]: use of moved value: `maybe` (Mir)
20+
--> $DIR/issue-41962.rs:17:16
21+
|
22+
17 | if let Some(thing) = maybe {
23+
| ^^^^^-----^
24+
| | |
25+
| | value moved here
26+
| value used here after move
27+
|
28+
= note: move occurs because `maybe` has type `std::option::Option<std::vec::Vec<bool>>`, which does not implement the `Copy` trait
29+
30+
error[E0382]: use of moved value: `maybe.0` (Mir)
31+
--> $DIR/issue-41962.rs:17:21
32+
|
33+
17 | if let Some(thing) = maybe {
34+
| ^^^^^ value moved here in previous iteration of loop
35+
|
36+
= note: move occurs because `maybe.0` has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
37+
38+
error: aborting due to 4 previous errors
39+

0 commit comments

Comments
 (0)