Skip to content

Commit 4bde46e

Browse files
authored
Rollup merge of rust-lang#71587 - matthewjasper:promoted-move-errors, r=nikomatsakis
Report cannot move errors in promoted MIR Closes rust-lang#70934
2 parents 2454a68 + 34eb2c1 commit 4bde46e

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

src/librustc_mir/borrow_check/mod.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,14 @@ fn do_mir_borrowck<'a, 'tcx>(
180180
let location_table = &LocationTable::new(&body);
181181

182182
let mut errors_buffer = Vec::new();
183-
let (move_data, move_errors): (MoveData<'tcx>, Option<Vec<(Place<'tcx>, MoveError<'tcx>)>>) =
183+
let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
184184
match MoveData::gather_moves(&body, tcx, param_env) {
185-
Ok(move_data) => (move_data, None),
186-
Err((move_data, move_errors)) => (move_data, Some(move_errors)),
185+
Ok(move_data) => (move_data, Vec::new()),
186+
Err((move_data, move_errors)) => (move_data, move_errors),
187187
};
188+
let promoted_errors = promoted
189+
.iter_enumerated()
190+
.map(|(idx, body)| (idx, MoveData::gather_moves(&body, tcx, param_env)));
188191

189192
let mdpe = MoveDataParamEnv { move_data, param_env };
190193

@@ -264,6 +267,41 @@ fn do_mir_borrowck<'a, 'tcx>(
264267
_ => true,
265268
};
266269

270+
for (idx, move_data_results) in promoted_errors {
271+
let promoted_body = &promoted[idx];
272+
let dominators = promoted_body.dominators();
273+
274+
if let Err((move_data, move_errors)) = move_data_results {
275+
let mut promoted_mbcx = MirBorrowckCtxt {
276+
infcx,
277+
body: promoted_body,
278+
mir_def_id: def_id.to_def_id(),
279+
move_data: &move_data,
280+
location_table: &LocationTable::new(promoted_body),
281+
movable_generator,
282+
locals_are_invalidated_at_exit,
283+
access_place_error_reported: Default::default(),
284+
reservation_error_reported: Default::default(),
285+
reservation_warnings: Default::default(),
286+
move_error_reported: BTreeMap::new(),
287+
uninitialized_error_reported: Default::default(),
288+
errors_buffer,
289+
regioncx: regioncx.clone(),
290+
used_mut: Default::default(),
291+
used_mut_upvars: SmallVec::new(),
292+
borrow_set: borrow_set.clone(),
293+
dominators,
294+
upvars: Vec::new(),
295+
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
296+
region_names: RefCell::default(),
297+
next_region_name: RefCell::new(1),
298+
polonius_output: None,
299+
};
300+
promoted_mbcx.report_move_errors(move_errors);
301+
errors_buffer = promoted_mbcx.errors_buffer;
302+
};
303+
}
304+
267305
let dominators = body.dominators();
268306

269307
let mut mbcx = MirBorrowckCtxt {
@@ -301,9 +339,7 @@ fn do_mir_borrowck<'a, 'tcx>(
301339
borrows: flow_borrows,
302340
};
303341

304-
if let Some(errors) = move_errors {
305-
mbcx.report_move_errors(errors);
306-
}
342+
mbcx.report_move_errors(move_errors);
307343

308344
dataflow::visit_results(
309345
&body,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for #70934
2+
3+
struct S;
4+
5+
fn foo() {
6+
&([S][0],);
7+
//~^ ERROR cannot move out of type `[S; 1]`
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0508]: cannot move out of type `[S; 1]`, a non-copy array
2+
--> $DIR/move-error-in-promoted-2.rs:6:7
3+
|
4+
LL | &([S][0],);
5+
| ^^^^^^
6+
| |
7+
| cannot move out of here
8+
| move occurs because value has type `S`, which does not implement the `Copy` trait
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0508`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Regression test for #70934
2+
3+
fn f() {
4+
const C: [S2; 1] = [S2];
5+
let _ = S1(C[0]).clone();
6+
//~^ ERROR cannot move out of type `[S2; 1]`
7+
}
8+
9+
#[derive(Clone)]
10+
struct S1(S2);
11+
12+
#[derive(Clone)]
13+
struct S2;
14+
15+
fn main() {
16+
f();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0508]: cannot move out of type `[S2; 1]`, a non-copy array
2+
--> $DIR/move-error-in-promoted.rs:5:16
3+
|
4+
LL | let _ = S1(C[0]).clone();
5+
| ^^^^
6+
| |
7+
| cannot move out of here
8+
| move occurs because value has type `S2`, which does not implement the `Copy` trait
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0508`.

0 commit comments

Comments
 (0)