Skip to content

Commit cf30759

Browse files
committed
Auto merge of #46268 - arielb1:union-borrow, r=nikomatsakis
MIR borrowck: implement union-and-array-compatible semantics Fixes #44831. Fixes #44834. Fixes #45537. Fixes #45696 (by implementing DerefPure semantics, which is what we want going forward). r? @nikomatsakis
2 parents 833785b + 9d35587 commit cf30759

17 files changed

+608
-176
lines changed

src/libcore/cell.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,11 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
10841084
pub fn map<U: ?Sized, F>(orig: RefMut<'b, T>, f: F) -> RefMut<'b, U>
10851085
where F: FnOnce(&mut T) -> &mut U
10861086
{
1087+
// FIXME(nll-rfc#40): fix borrow-check
1088+
let RefMut { value, borrow } = orig;
10871089
RefMut {
1088-
value: f(orig.value),
1089-
borrow: orig.borrow,
1090+
value: f(value),
1091+
borrow: borrow,
10901092
}
10911093
}
10921094
}

src/libcore/iter/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1776,12 +1776,18 @@ impl<I: Iterator> Iterator for Peekable<I> {
17761776

17771777
#[inline]
17781778
fn nth(&mut self, n: usize) -> Option<I::Item> {
1779-
match self.peeked.take() {
1780-
// the .take() below is just to avoid "move into pattern guard"
1781-
Some(ref mut v) if n == 0 => v.take(),
1782-
Some(None) => None,
1783-
Some(Some(_)) => self.iter.nth(n - 1),
1784-
None => self.iter.nth(n),
1779+
// FIXME(#6393): merge these when borrow-checking gets better.
1780+
if n == 0 {
1781+
match self.peeked.take() {
1782+
Some(v) => v,
1783+
None => self.iter.nth(n),
1784+
}
1785+
} else {
1786+
match self.peeked.take() {
1787+
Some(None) => None,
1788+
Some(Some(_)) => self.iter.nth(n - 1),
1789+
None => self.iter.nth(n),
1790+
}
17851791
}
17861792
}
17871793

src/librustc_mir/borrow_check/mod.rs

+428-85
Large diffs are not rendered by default.

src/librustc_mir/build/cfg.rs

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ impl<'tcx> CFG<'tcx> {
5151
source_info: SourceInfo,
5252
region_scope: region::Scope) {
5353
if tcx.sess.emit_end_regions() {
54+
if let region::ScopeData::CallSite(_) = region_scope.data() {
55+
// The CallSite scope (aka the root scope) is sort of weird, in that it is
56+
// supposed to "separate" the "interior" and "exterior" of a closure. Being
57+
// that, it is not really a part of the region hierarchy, but for some
58+
// reason it *is* considered a part of it.
59+
//
60+
// It should die a hopefully painful death with NLL, so let's leave this hack
61+
// for now so that nobody can complain about soundness.
62+
return
63+
}
64+
5465
self.push(block, Statement {
5566
source_info,
5667
kind: StatementKind::EndRegion(region_scope),

src/librustc_mir/dataflow/impls/borrows.rs

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

11+
use rustc::hir;
12+
use rustc::hir::def_id::DefId;
13+
use rustc::middle::region;
1114
use rustc::mir::{self, Location, Mir};
1215
use rustc::mir::visit::Visitor;
1316
use rustc::ty::{self, Region, TyCtxt};
@@ -27,16 +30,20 @@ use borrow_check::nll::ToRegionVid;
2730
use syntax_pos::Span;
2831

2932
use std::fmt;
33+
use std::rc::Rc;
3034

3135
// `Borrows` maps each dataflow bit to an `Rvalue::Ref`, which can be
3236
// uniquely identified in the MIR by the `Location` of the assigment
3337
// statement in which it appears on the right hand side.
3438
pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
3539
tcx: TyCtxt<'a, 'gcx, 'tcx>,
3640
mir: &'a Mir<'tcx>,
41+
scope_tree: Rc<region::ScopeTree>,
42+
root_scope: Option<region::Scope>,
3743
borrows: IndexVec<BorrowIndex, BorrowData<'tcx>>,
3844
location_map: FxHashMap<Location, BorrowIndex>,
3945
region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
46+
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
4047
region_span_map: FxHashMap<RegionKind, Span>,
4148
nonlexical_regioncx: Option<RegionInferenceContext<'tcx>>,
4249
}
@@ -69,22 +76,32 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
6976
impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
7077
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
7178
mir: &'a Mir<'tcx>,
72-
nonlexical_regioncx: Option<RegionInferenceContext<'tcx>>)
79+
nonlexical_regioncx: Option<RegionInferenceContext<'tcx>>,
80+
def_id: DefId,
81+
body_id: Option<hir::BodyId>)
7382
-> Self {
83+
let scope_tree = tcx.region_scope_tree(def_id);
84+
let root_scope = body_id.map(|body_id| {
85+
region::Scope::CallSite(tcx.hir.body(body_id).value.hir_id.local_id)
86+
});
7487
let mut visitor = GatherBorrows {
7588
tcx,
7689
mir,
7790
idx_vec: IndexVec::new(),
7891
location_map: FxHashMap(),
7992
region_map: FxHashMap(),
93+
local_map: FxHashMap(),
8094
region_span_map: FxHashMap()
8195
};
8296
visitor.visit_mir(mir);
8397
return Borrows { tcx: tcx,
8498
mir: mir,
8599
borrows: visitor.idx_vec,
100+
scope_tree,
101+
root_scope,
86102
location_map: visitor.location_map,
87103
region_map: visitor.region_map,
104+
local_map: visitor.local_map,
88105
region_span_map: visitor.region_span_map,
89106
nonlexical_regioncx };
90107

@@ -94,13 +111,22 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
94111
idx_vec: IndexVec<BorrowIndex, BorrowData<'tcx>>,
95112
location_map: FxHashMap<Location, BorrowIndex>,
96113
region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
114+
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
97115
region_span_map: FxHashMap<RegionKind, Span>,
98116
}
99117

100118
impl<'a, 'gcx, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'gcx, 'tcx> {
101119
fn visit_rvalue(&mut self,
102120
rvalue: &mir::Rvalue<'tcx>,
103121
location: mir::Location) {
122+
fn root_local(mut p: &mir::Place<'_>) -> Option<mir::Local> {
123+
loop { match p {
124+
mir::Place::Projection(pi) => p = &pi.base,
125+
mir::Place::Static(_) => return None,
126+
mir::Place::Local(l) => return Some(*l)
127+
}}
128+
}
129+
104130
if let mir::Rvalue::Ref(region, kind, ref place) = *rvalue {
105131
if is_unsafe_place(self.tcx, self.mir, place) { return; }
106132

@@ -109,8 +135,14 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
109135
};
110136
let idx = self.idx_vec.push(borrow);
111137
self.location_map.insert(location, idx);
138+
112139
let borrows = self.region_map.entry(region).or_insert(FxHashSet());
113140
borrows.insert(idx);
141+
142+
if let Some(local) = root_local(place) {
143+
let borrows = self.local_map.entry(local).or_insert(FxHashSet());
144+
borrows.insert(idx);
145+
}
114146
}
115147
}
116148

@@ -199,7 +231,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
199231
mir::StatementKind::EndRegion(region_scope) => {
200232
if let Some(borrow_indexes) = self.region_map.get(&ReScope(region_scope)) {
201233
assert!(self.nonlexical_regioncx.is_none());
202-
for idx in borrow_indexes { sets.kill(&idx); }
234+
sets.kill_all(borrow_indexes);
203235
} else {
204236
// (if there is no entry, then there are no borrows to be tracked)
205237
}
@@ -224,10 +256,19 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
224256
}
225257
}
226258

259+
mir::StatementKind::StorageDead(local) => {
260+
// Make sure there are no remaining borrows for locals that
261+
// are gone out of scope.
262+
//
263+
// FIXME: expand this to variables that are assigned over.
264+
if let Some(borrow_indexes) = self.local_map.get(&local) {
265+
sets.kill_all(borrow_indexes);
266+
}
267+
}
268+
227269
mir::StatementKind::InlineAsm { .. } |
228270
mir::StatementKind::SetDiscriminant { .. } |
229271
mir::StatementKind::StorageLive(..) |
230-
mir::StatementKind::StorageDead(..) |
231272
mir::StatementKind::Validate(..) |
232273
mir::StatementKind::Nop => {}
233274

@@ -253,8 +294,17 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
253294
// like unwind paths, we do not always emit `EndRegion` statements, so we
254295
// add some kills here as a "backup" and to avoid spurious error messages.
255296
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() {
256-
if let ReScope(..) = borrow_data.region {
257-
sets.kill(&borrow_index);
297+
if let ReScope(scope) = borrow_data.region {
298+
// Check that the scope is not actually a scope from a function that is
299+
// a parent of our closure. Note that the CallSite scope itself is
300+
// *outside* of the closure, for some weird reason.
301+
if let Some(root_scope) = self.root_scope {
302+
if *scope != root_scope &&
303+
self.scope_tree.is_subscope_of(*scope, root_scope)
304+
{
305+
sets.kill(&borrow_index);
306+
}
307+
}
258308
}
259309
}
260310
}

src/librustc_mir/dataflow/impls/mod.rs

+39-53
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
1515
use rustc::ty::TyCtxt;
1616
use rustc::mir::{self, Mir, Location};
17-
use rustc_data_structures::bitslice::BitSlice; // adds set_bit/get_bit to &[usize] bitvector rep.
1817
use rustc_data_structures::bitslice::{BitwiseOperator};
1918
use rustc_data_structures::indexed_set::{IdxSet};
2019
use rustc_data_structures::indexed_vec::Idx;
@@ -504,7 +503,6 @@ impl<'a, 'gcx, 'tcx> BitDenotation for MovingOutStatements<'a, 'gcx, 'tcx> {
504503
let stmt = &mir[location.block].statements[location.statement_index];
505504
let loc_map = &move_data.loc_map;
506505
let path_map = &move_data.path_map;
507-
let bits_per_block = self.bits_per_block();
508506

509507
match stmt.kind {
510508
// this analysis only tries to find moves explicitly
@@ -515,21 +513,15 @@ impl<'a, 'gcx, 'tcx> BitDenotation for MovingOutStatements<'a, 'gcx, 'tcx> {
515513
_ => {
516514
debug!("stmt {:?} at loc {:?} moves out of move_indexes {:?}",
517515
stmt, location, &loc_map[location]);
518-
for move_index in &loc_map[location] {
519-
// Every path deinitialized by a *particular move*
520-
// has corresponding bit, "gen'ed" (i.e. set)
521-
// here, in dataflow vector
522-
zero_to_one(sets.gen_set.words_mut(), *move_index);
523-
}
516+
// Every path deinitialized by a *particular move*
517+
// has corresponding bit, "gen'ed" (i.e. set)
518+
// here, in dataflow vector
519+
sets.gen_all_and_assert_dead(&loc_map[location]);
524520
}
525521
}
526522

527523
for_location_inits(tcx, mir, move_data, location,
528-
|mpi| for moi in &path_map[mpi] {
529-
assert!(moi.index() < bits_per_block);
530-
sets.kill_set.add(&moi);
531-
}
532-
);
524+
|mpi| sets.kill_all(&path_map[mpi]));
533525
}
534526

535527
fn terminator_effect(&self,
@@ -543,18 +535,10 @@ impl<'a, 'gcx, 'tcx> BitDenotation for MovingOutStatements<'a, 'gcx, 'tcx> {
543535

544536
debug!("terminator {:?} at loc {:?} moves out of move_indexes {:?}",
545537
term, location, &loc_map[location]);
546-
let bits_per_block = self.bits_per_block();
547-
for move_index in &loc_map[location] {
548-
assert!(move_index.index() < bits_per_block);
549-
zero_to_one(sets.gen_set.words_mut(), *move_index);
550-
}
538+
sets.gen_all_and_assert_dead(&loc_map[location]);
551539

552540
for_location_inits(tcx, mir, move_data, location,
553-
|mpi| for moi in &path_map[mpi] {
554-
assert!(moi.index() < bits_per_block);
555-
sets.kill_set.add(&moi);
556-
}
557-
);
541+
|mpi| sets.kill_all(&path_map[mpi]));
558542
}
559543

560544
fn propagate_call_return(&self,
@@ -585,11 +569,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedLvals<'a, 'gcx, 'tcx> {
585569
}
586570

587571
fn start_block_effect(&self, sets: &mut BlockSets<InitIndex>) {
588-
let bits_per_block = self.bits_per_block();
589-
for init_index in (0..self.mir.arg_count).map(InitIndex::new) {
590-
assert!(init_index.index() < bits_per_block);
591-
sets.gen_set.add(&init_index);
592-
}
572+
sets.gen_all((0..self.mir.arg_count).map(InitIndex::new));
593573
}
594574
fn statement_effect(&self,
595575
sets: &mut BlockSets<InitIndex>,
@@ -599,26 +579,39 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedLvals<'a, 'gcx, 'tcx> {
599579
let init_path_map = &move_data.init_path_map;
600580
let init_loc_map = &move_data.init_loc_map;
601581
let rev_lookup = &move_data.rev_lookup;
602-
let bits_per_block = self.bits_per_block();
603582

604583
debug!("statement {:?} at loc {:?} initializes move_indexes {:?}",
605584
stmt, location, &init_loc_map[location]);
606-
for init_index in &init_loc_map[location] {
607-
assert!(init_index.index() < bits_per_block);
608-
sets.gen_set.add(init_index);
609-
}
585+
sets.gen_all(&init_loc_map[location]);
610586

611587
match stmt.kind {
612-
mir::StatementKind::StorageDead(local) => {
613-
// End inits for StorageDead, so that an immutable variable can
614-
// be reinitialized on the next iteration of the loop.
588+
mir::StatementKind::StorageDead(local) |
589+
mir::StatementKind::StorageLive(local) => {
590+
// End inits for StorageDead and StorageLive, so that an immutable
591+
// variable can be reinitialized on the next iteration of the loop.
592+
//
593+
// FIXME(#46525): We *need* to do this for StorageLive as well as
594+
// StorageDead, because lifetimes of match bindings with guards are
595+
// weird - i.e. this code
596+
//
597+
// ```
598+
// fn main() {
599+
// match 0 {
600+
// a | a
601+
// if { println!("a={}", a); false } => {}
602+
// _ => {}
603+
// }
604+
// }
605+
// ```
606+
//
607+
// runs the guard twice, using the same binding for `a`, and only
608+
// storagedeads after everything ends, so if we don't regard the
609+
// storagelive as killing storage, we would have a multiple assignment
610+
// to immutable data error.
615611
if let LookupResult::Exact(mpi) = rev_lookup.find(&mir::Place::Local(local)) {
616612
debug!("stmt {:?} at loc {:?} clears the ever initialized status of {:?}",
617-
stmt, location, &init_path_map[mpi]);
618-
for ii in &init_path_map[mpi] {
619-
assert!(ii.index() < bits_per_block);
620-
sets.kill_set.add(&ii);
621-
}
613+
stmt, location, &init_path_map[mpi]);
614+
sets.kill_all(&init_path_map[mpi]);
622615
}
623616
}
624617
_ => {}
@@ -634,13 +627,11 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedLvals<'a, 'gcx, 'tcx> {
634627
let init_loc_map = &move_data.init_loc_map;
635628
debug!("terminator {:?} at loc {:?} initializes move_indexes {:?}",
636629
term, location, &init_loc_map[location]);
637-
let bits_per_block = self.bits_per_block();
638-
for init_index in &init_loc_map[location] {
639-
if move_data.inits[*init_index].kind != InitKind::NonPanicPathOnly {
640-
assert!(init_index.index() < bits_per_block);
641-
sets.gen_set.add(init_index);
642-
}
643-
}
630+
sets.gen_all(
631+
init_loc_map[location].iter().filter(|init_index| {
632+
move_data.inits[**init_index].kind != InitKind::NonPanicPathOnly
633+
})
634+
);
644635
}
645636

646637
fn propagate_call_return(&self,
@@ -663,11 +654,6 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedLvals<'a, 'gcx, 'tcx> {
663654
}
664655
}
665656

666-
fn zero_to_one(bitvec: &mut [usize], move_index: MoveOutIndex) {
667-
let retval = bitvec.set_bit(move_index.index());
668-
assert!(retval);
669-
}
670-
671657
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeInitializedLvals<'a, 'gcx, 'tcx> {
672658
#[inline]
673659
fn join(&self, pred1: usize, pred2: usize) -> usize {

0 commit comments

Comments
 (0)