Skip to content

Commit 88a28ff

Browse files
committed
Auto merge of #45936 - mikhail-m1:mir-borrowck-storage-dead, r=arielb1
add `StorageDead` handling fix #45642 r? @arielb1
2 parents ce2b8a4 + 9e35fd2 commit 88a28ff

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

src/librustc_mir/dataflow/drop_flag_effects.rs

-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax_pos::DUMMY_SP;
12-
1311
use rustc::mir::{self, Mir, Location};
1412
use rustc::ty::{self, TyCtxt};
1513
use util::elaborate_drops::DropFlagState;
@@ -187,23 +185,13 @@ pub(crate) fn drop_flag_effects_for_location<'a, 'gcx, 'tcx, F>(
187185
where F: FnMut(MovePathIndex, DropFlagState)
188186
{
189187
let move_data = &ctxt.move_data;
190-
let param_env = ctxt.param_env;
191188
debug!("drop_flag_effects_for_location({:?})", loc);
192189

193190
// first, move out of the RHS
194191
for mi in &move_data.loc_map[loc] {
195192
let path = mi.move_path_index(move_data);
196193
debug!("moving out of path {:?}", move_data.move_paths[path]);
197194

198-
// don't move out of non-Copy things
199-
let lvalue = &move_data.move_paths[path].lvalue;
200-
let ty = lvalue.ty(mir, tcx).to_ty(tcx);
201-
let gcx = tcx.global_tcx();
202-
let erased_ty = gcx.lift(&tcx.erase_regions(&ty)).unwrap();
203-
if !erased_ty.moves_by_default(gcx, param_env, DUMMY_SP) {
204-
continue;
205-
}
206-
207195
on_all_children_bits(tcx, mir, move_data,
208196
path,
209197
|mpi| callback(mpi, DropFlagState::Absent))

src/librustc_mir/dataflow/impls/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,24 @@ impl<'a, 'gcx, 'tcx> BitDenotation for MovingOutStatements<'a, 'gcx, 'tcx> {
456456
let path_map = &move_data.path_map;
457457
let rev_lookup = &move_data.rev_lookup;
458458

459-
debug!("stmt {:?} at loc {:?} moves out of move_indexes {:?}",
460-
stmt, location, &loc_map[location]);
461-
for move_index in &loc_map[location] {
462-
// Every path deinitialized by a *particular move*
463-
// has corresponding bit, "gen'ed" (i.e. set)
464-
// here, in dataflow vector
465-
zero_to_one(sets.gen_set.words_mut(), *move_index);
459+
match stmt.kind {
460+
// this analysis only tries to find moves explicitly
461+
// written by the user, so we ignore the move-outs
462+
// created by `StorageDead` and at the beginning
463+
// of a function.
464+
mir::StatementKind::StorageDead(_) => {}
465+
_ => {
466+
debug!("stmt {:?} at loc {:?} moves out of move_indexes {:?}",
467+
stmt, location, &loc_map[location]);
468+
for move_index in &loc_map[location] {
469+
// Every path deinitialized by a *particular move*
470+
// has corresponding bit, "gen'ed" (i.e. set)
471+
// here, in dataflow vector
472+
zero_to_one(sets.gen_set.words_mut(), *move_index);
473+
}
474+
}
466475
}
476+
467477
let bits_per_block = self.bits_per_block();
468478
match stmt.kind {
469479
mir::StatementKind::SetDiscriminant { .. } => {

src/librustc_mir/dataflow/move_paths/builder.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
250250
}
251251
self.gather_rvalue(rval);
252252
}
253-
StatementKind::StorageLive(_) |
254-
StatementKind::StorageDead(_) => {}
253+
StatementKind::StorageLive(_) => {}
254+
StatementKind::StorageDead(local) => {
255+
self.gather_move(&Lvalue::Local(local), true);
256+
}
255257
StatementKind::SetDiscriminant{ .. } => {
256258
span_bug!(stmt.source_info.span,
257259
"SetDiscriminant should not exist during borrowck");
@@ -309,7 +311,7 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
309311
TerminatorKind::Unreachable => { }
310312

311313
TerminatorKind::Return => {
312-
self.gather_move(&Lvalue::Local(RETURN_POINTER));
314+
self.gather_move(&Lvalue::Local(RETURN_POINTER), false);
313315
}
314316

315317
TerminatorKind::Assert { .. } |
@@ -322,7 +324,7 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
322324
}
323325

324326
TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
325-
self.gather_move(location);
327+
self.gather_move(location, false);
326328
}
327329
TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
328330
self.create_move_path(location);
@@ -344,19 +346,19 @@ impl<'b, 'a, 'gcx, 'tcx> Gatherer<'b, 'a, 'gcx, 'tcx> {
344346
match *operand {
345347
Operand::Constant(..) => {} // not-a-move
346348
Operand::Consume(ref lval) => { // a move
347-
self.gather_move(lval);
349+
self.gather_move(lval, false);
348350
}
349351
}
350352
}
351353

352-
fn gather_move(&mut self, lval: &Lvalue<'tcx>) {
354+
fn gather_move(&mut self, lval: &Lvalue<'tcx>, force: bool) {
353355
debug!("gather_move({:?}, {:?})", self.loc, lval);
354356

355357
let tcx = self.builder.tcx;
356358
let gcx = tcx.global_tcx();
357359
let lv_ty = lval.ty(self.builder.mir, tcx).to_ty(tcx);
358360
let erased_ty = gcx.lift(&tcx.erase_regions(&lv_ty)).unwrap();
359-
if !erased_ty.moves_by_default(gcx, self.builder.param_env, DUMMY_SP) {
361+
if !force && !erased_ty.moves_by_default(gcx, self.builder.param_env, DUMMY_SP) {
360362
debug!("gather_move({:?}, {:?}) - {:?} is Copy. skipping", self.loc, lval, lv_ty);
361363
return
362364
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2012 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 emit-end-regions -Z borrowck-mir
12+
13+
fn ok() {
14+
loop {
15+
let _x = 1;
16+
}
17+
}
18+
19+
fn fail() {
20+
loop {
21+
let x: i32;
22+
let _ = x + 1; //~ERROR (Ast) [E0381]
23+
//~^ ERROR (Mir) [E0381]
24+
}
25+
}
26+
27+
fn main() {
28+
ok();
29+
fail();
30+
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ enum Sexpression {
1818

1919
fn causes_ice(mut l: &mut Sexpression) {
2020
loop { match l {
21-
&mut Sexpression::Num(ref mut n) => {}, //[mir]~ ERROR (Mir) [E0384]
21+
&mut Sexpression::Num(ref mut n) => {},
2222
&mut Sexpression::Cons(ref mut expr) => { //[ast]~ ERROR [E0499]
2323
//[mir]~^ ERROR (Ast) [E0499]
2424
//[mir]~| ERROR (Mir) [E0506]
25-
//[mir]~| ERROR (Mir) [E0384]
2625
//[mir]~| ERROR (Mir) [E0499]
2726
l = &mut **expr; //[ast]~ ERROR [E0506]
2827
//[mir]~^ ERROR (Ast) [E0506]

0 commit comments

Comments
 (0)