Skip to content

Commit 90f7e3a

Browse files
Cameron Zwarichalexcrichton
Cameron Zwarich
authored andcommitted
Reject double moves out of array elements
Fixes #14986.
1 parent f993495 commit 90f7e3a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/librustc/middle/borrowck/move_data.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use middle::dataflow::DataFlowContext;
2525
use middle::dataflow::BitwiseOperator;
2626
use middle::dataflow::DataFlowOperator;
2727
use euv = middle::expr_use_visitor;
28+
use mc = middle::mem_categorization;
2829
use middle::ty;
2930
use syntax::ast;
3031
use syntax::ast_util;
@@ -160,6 +161,22 @@ pub struct AssignDataFlowOperator;
160161

161162
pub type AssignDataFlow<'a> = DataFlowContext<'a, AssignDataFlowOperator>;
162163

164+
fn loan_path_is_precise(loan_path: &LoanPath) -> bool {
165+
match *loan_path {
166+
LpVar(_) => {
167+
true
168+
}
169+
LpExtend(_, _, LpInterior(mc::InteriorElement(_))) => {
170+
// Paths involving element accesses do not refer to a unique
171+
// location, as there is no accurate tracking of the indices.
172+
false
173+
}
174+
LpExtend(ref lp_base, _, _) => {
175+
loan_path_is_precise(&**lp_base)
176+
}
177+
}
178+
}
179+
163180
impl MoveData {
164181
pub fn new() -> MoveData {
165182
MoveData {
@@ -500,10 +517,17 @@ impl MoveData {
500517
path: MovePathIndex,
501518
kill_id: ast::NodeId,
502519
dfcx_moves: &mut MoveDataFlow) {
503-
self.each_applicable_move(path, |move_index| {
504-
dfcx_moves.add_kill(kill_id, move_index.get());
505-
true
506-
});
520+
// We can only perform kills for paths that refer to a unique location,
521+
// since otherwise we may kill a move from one location with an
522+
// assignment referring to another location.
523+
524+
let loan_path = self.path_loan_path(path);
525+
if loan_path_is_precise(&*loan_path) {
526+
self.each_applicable_move(path, |move_index| {
527+
dfcx_moves.add_kill(kill_id, move_index.get());
528+
true
529+
});
530+
}
507531
}
508532
}
509533

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 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+
fn f() {
12+
let mut a = [box 0, box 1];
13+
drop(a[0]);
14+
a[1] = box 2;
15+
drop(a[0]); //~ ERROR use of moved value: `a[..]`
16+
}
17+
18+
fn main() {
19+
f();
20+
}
21+

0 commit comments

Comments
 (0)