Skip to content

Commit 7059266

Browse files
committed
relocate BorrowData etc into borrow_check::borrow_set
1 parent 6c528ce commit 7059266

File tree

8 files changed

+113
-81
lines changed

8 files changed

+113
-81
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2012-2017 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+
use dataflow::indexes::BorrowIndex;
12+
use rustc::mir::{self, Location};
13+
use rustc::ty::{Region, RegionKind};
14+
use rustc::util::nodemap::{FxHashMap, FxHashSet};
15+
use rustc_data_structures::indexed_vec::IndexVec;
16+
use std::fmt;
17+
use syntax_pos::Span;
18+
19+
crate struct BorrowSet<'tcx> {
20+
/// The fundamental map relating bitvector indexes to the borrows
21+
/// in the MIR.
22+
crate borrows: IndexVec<BorrowIndex, BorrowData<'tcx>>,
23+
24+
/// Each borrow is also uniquely identified in the MIR by the
25+
/// `Location` of the assignment statement in which it appears on
26+
/// the right hand side; we map each such location to the
27+
/// corresponding `BorrowIndex`.
28+
crate location_map: FxHashMap<Location, BorrowIndex>,
29+
30+
/// Locations which activate borrows.
31+
/// NOTE: A given location may activate more than one borrow in the future
32+
/// when more general two-phase borrow support is introduced, but for now we
33+
/// only need to store one borrow index
34+
crate activation_map: FxHashMap<Location, FxHashSet<BorrowIndex>>,
35+
36+
/// Every borrow has a region; this maps each such regions back to
37+
/// its borrow-indexes.
38+
crate region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
39+
40+
/// Map from local to all the borrows on that local
41+
crate local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
42+
43+
/// Maps regions to their corresponding source spans
44+
/// Only contains ReScope()s as keys
45+
crate region_span_map: FxHashMap<RegionKind, Span>,
46+
}
47+
48+
#[derive(Debug)]
49+
crate struct BorrowData<'tcx> {
50+
/// Location where the borrow reservation starts.
51+
/// In many cases, this will be equal to the activation location but not always.
52+
crate reserve_location: Location,
53+
/// Location where the borrow is activated. None if this is not a
54+
/// 2-phase borrow.
55+
crate activation_location: Option<Location>,
56+
/// What kind of borrow this is
57+
crate kind: mir::BorrowKind,
58+
/// The region for which this borrow is live
59+
crate region: Region<'tcx>,
60+
/// Place from which we are borrowing
61+
crate borrowed_place: mir::Place<'tcx>,
62+
/// Place to which the borrow was stored
63+
crate assigned_place: mir::Place<'tcx>,
64+
}
65+
66+
impl<'tcx> fmt::Display for BorrowData<'tcx> {
67+
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
68+
let kind = match self.kind {
69+
mir::BorrowKind::Shared => "",
70+
mir::BorrowKind::Unique => "uniq ",
71+
mir::BorrowKind::Mut { .. } => "mut ",
72+
};
73+
let region = format!("{}", self.region);
74+
let region = if region.len() > 0 { format!("{} ", region) } else { region };
75+
write!(w, "&{}{}{:?}", region, kind, self.borrowed_place)
76+
}
77+
}
78+

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_data_structures::sync::Lrc;
1818

1919
use super::{Context, MirBorrowckCtxt};
2020
use super::{InitializationRequiringAction, PrefixSet};
21-
use dataflow::{Borrows, BorrowData, FlowAtLocation, MovingOutStatements};
21+
use super::borrow_set::BorrowData;
22+
23+
use dataflow::{Borrows, FlowAtLocation, MovingOutStatements};
2224
use dataflow::move_paths::MovePathIndex;
2325
use util::borrowck_errors::{BorrowckErrors, Origin};
2426

@@ -834,7 +836,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
834836
}
835837

836838
// Retrieve span of given borrow from the current MIR representation
837-
pub fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
839+
crate fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
838840
self.mir.source_info(borrow.reserve_location).span
839841
}
840842

src/librustc_mir/borrow_check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use dataflow::MoveDataParamEnv;
3737
use dataflow::{DataflowResultsConsumer};
3838
use dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
3939
use dataflow::{EverInitializedPlaces, MovingOutStatements};
40-
use dataflow::{BorrowData, Borrows, ReserveOrActivateIndex};
40+
use dataflow::{Borrows, ReserveOrActivateIndex};
4141
use dataflow::indexes::BorrowIndex;
4242
use dataflow::move_paths::{IllegalMoveOriginKind, MoveError};
4343
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
@@ -46,10 +46,12 @@ use util::collect_writes::FindAssignments;
4646

4747
use std::iter;
4848

49+
use self::borrow_set::BorrowData;
4950
use self::flows::Flows;
5051
use self::prefixes::PrefixSet;
5152
use self::MutateMode::{JustWrite, WriteAndRead};
5253

54+
crate mod borrow_set;
5355
mod error_reporting;
5456
mod flows;
5557
mod prefixes;

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use borrow_check::nll::region_infer::{Cause, RegionInferenceContext};
1212
use borrow_check::{Context, MirBorrowckCtxt};
13-
use dataflow::BorrowData;
13+
use borrow_check::borrow_set::BorrowData;
1414
use rustc::mir::visit::{MirVisitable, PlaceContext, Visitor};
1515
use rustc::mir::{Local, Location, Mir};
1616
use rustc_data_structures::fx::FxHashSet;

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use borrow_check::borrow_set::{BorrowSet, BorrowData};
12+
1113
use rustc;
1214
use rustc::hir;
1315
use rustc::hir::def_id::DefId;
@@ -32,7 +34,6 @@ use borrow_check::nll::ToRegionVid;
3234

3335
use syntax_pos::Span;
3436

35-
use std::fmt;
3637
use std::hash::Hash;
3738
use std::rc::Rc;
3839

@@ -49,69 +50,12 @@ pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
4950
scope_tree: Lrc<region::ScopeTree>,
5051
root_scope: Option<region::Scope>,
5152

52-
/// The fundamental map relating bitvector indexes to the borrows
53-
/// in the MIR.
54-
borrows: IndexVec<BorrowIndex, BorrowData<'tcx>>,
55-
56-
/// Each borrow is also uniquely identified in the MIR by the
57-
/// `Location` of the assignment statement in which it appears on
58-
/// the right hand side; we map each such location to the
59-
/// corresponding `BorrowIndex`.
60-
location_map: FxHashMap<Location, BorrowIndex>,
61-
62-
/// Locations which activate borrows.
63-
activation_map: FxHashMap<Location, FxHashSet<BorrowIndex>>,
64-
65-
/// Every borrow has a region; this maps each such regions back to
66-
/// its borrow-indexes.
67-
region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
68-
69-
/// Map from local to all the borrows on that local
70-
local_map: FxHashMap<mir::Local, FxHashSet<BorrowIndex>>,
71-
72-
/// Maps regions to their corresponding source spans
73-
/// Only contains ReScope()s as keys
74-
region_span_map: FxHashMap<RegionKind, Span>,
53+
borrow_set: BorrowSet<'tcx>,
7554

7655
/// NLL region inference context with which NLL queries should be resolved
7756
nonlexical_regioncx: Option<Rc<RegionInferenceContext<'tcx>>>,
7857
}
7958

80-
// temporarily allow some dead fields: `kind` and `region` will be
81-
// needed by borrowck; `borrowed_place` will probably be a MovePathIndex when
82-
// that is extended to include borrowed data paths.
83-
#[allow(dead_code)]
84-
#[derive(Debug)]
85-
pub struct BorrowData<'tcx> {
86-
/// Location where the borrow reservation starts.
87-
/// In many cases, this will be equal to the activation location but not always.
88-
pub(crate) reserve_location: Location,
89-
/// Location where the borrow is activated. None if this is not a
90-
/// 2-phase borrow.
91-
pub(crate) activation_location: Option<Location>,
92-
/// What kind of borrow this is
93-
pub(crate) kind: mir::BorrowKind,
94-
/// The region for which this borrow is live
95-
pub(crate) region: Region<'tcx>,
96-
/// Place from which we are borrowing
97-
pub(crate) borrowed_place: mir::Place<'tcx>,
98-
/// Place to which the borrow was stored
99-
pub(crate) assigned_place: mir::Place<'tcx>,
100-
}
101-
102-
impl<'tcx> fmt::Display for BorrowData<'tcx> {
103-
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
104-
let kind = match self.kind {
105-
mir::BorrowKind::Shared => "",
106-
mir::BorrowKind::Unique => "uniq ",
107-
mir::BorrowKind::Mut { .. } => "mut ",
108-
};
109-
let region = format!("{}", self.region);
110-
let region = if region.len() > 0 { format!("{} ", region) } else { region };
111-
write!(w, "&{}{}{:?}", region, kind, self.borrowed_place)
112-
}
113-
}
114-
11559
impl ReserveOrActivateIndex {
11660
fn reserved(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new(i.index() * 2) }
11761
fn active(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new((i.index() * 2) + 1) }
@@ -169,14 +113,16 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
169113

170114
return Borrows { tcx: tcx,
171115
mir: mir,
172-
borrows: visitor.idx_vec,
116+
borrow_set: BorrowSet {
117+
borrows: visitor.idx_vec,
118+
location_map: visitor.location_map,
119+
activation_map: visitor.activation_map,
120+
region_map: visitor.region_map,
121+
local_map: visitor.local_map,
122+
region_span_map: visitor.region_span_map,
123+
},
173124
scope_tree,
174125
root_scope,
175-
location_map: visitor.location_map,
176-
activation_map: visitor.activation_map,
177-
region_map: visitor.region_map,
178-
local_map: visitor.local_map,
179-
region_span_map: visitor.region_span_map,
180126
nonlexical_regioncx };
181127

182128
struct GatherBorrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
@@ -514,20 +460,20 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
514460
match self.nonlexical_regioncx {
515461
Some(_) => None,
516462
None => {
517-
match self.region_span_map.get(region) {
463+
match self.borrow_set.region_span_map.get(region) {
518464
Some(span) => Some(self.tcx.sess.codemap().end_point(*span)),
519465
None => Some(self.tcx.sess.codemap().end_point(self.mir.span))
520466
}
521467
}
522468
}
523469
}
524470

525-
pub fn borrows(&self) -> &IndexVec<BorrowIndex, BorrowData<'tcx>> { &self.borrows }
471+
crate fn borrows(&self) -> &IndexVec<BorrowIndex, BorrowData<'tcx>> { &self.borrow_set.borrows }
526472

527473
pub fn scope_tree(&self) -> &Lrc<region::ScopeTree> { &self.scope_tree }
528474

529475
pub fn location(&self, idx: BorrowIndex) -> &Location {
530-
&self.borrows[idx].reserve_location
476+
&self.borrow_set.borrows[idx].reserve_location
531477
}
532478

533479
/// Add all borrows to the kill set, if those borrows are out of scope at `location`.
@@ -548,7 +494,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
548494
// terminator *does* introduce a new loan of the same
549495
// region, then setting that gen-bit will override any
550496
// potential kill introduced here.
551-
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() {
497+
for (borrow_index, borrow_data) in self.borrow_set.borrows.iter_enumerated() {
552498
let borrow_region = borrow_data.region.to_region_vid();
553499
if !regioncx.region_contains_point(borrow_region, location) {
554500
sets.kill(&ReserveOrActivateIndex::reserved(borrow_index));
@@ -562,7 +508,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
562508
sets: &mut BlockSets<ReserveOrActivateIndex>,
563509
local: &rustc::mir::Local)
564510
{
565-
if let Some(borrow_indexes) = self.local_map.get(local) {
511+
if let Some(borrow_indexes) = self.borrow_set.local_map.get(local) {
566512
sets.kill_all(borrow_indexes.iter()
567513
.map(|b| ReserveOrActivateIndex::reserved(*b)));
568514
sets.kill_all(borrow_indexes.iter()
@@ -575,7 +521,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
575521
sets: &mut BlockSets<ReserveOrActivateIndex>,
576522
location: Location) {
577523
// Handle activations
578-
match self.activation_map.get(&location) {
524+
match self.borrow_set.activation_map.get(&location) {
579525
Some(activations) => {
580526
for activated in activations {
581527
debug!("activating borrow {:?}", activated);
@@ -591,7 +537,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
591537
type Idx = ReserveOrActivateIndex;
592538
fn name() -> &'static str { "borrows" }
593539
fn bits_per_block(&self) -> usize {
594-
self.borrows.len() * 2
540+
self.borrow_set.borrows.len() * 2
595541
}
596542

597543
fn start_block_effect(&self, _entry_set: &mut IdxSet<ReserveOrActivateIndex>) {
@@ -623,7 +569,9 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
623569
match stmt.kind {
624570
// EndRegion kills any borrows (reservations and active borrows both)
625571
mir::StatementKind::EndRegion(region_scope) => {
626-
if let Some(borrow_indexes) = self.region_map.get(&ReScope(region_scope)) {
572+
if let Some(borrow_indexes) =
573+
self.borrow_set.region_map.get(&ReScope(region_scope))
574+
{
627575
assert!(self.nonlexical_regioncx.is_none());
628576
for idx in borrow_indexes {
629577
sets.kill(&ReserveOrActivateIndex::reserved(*idx));
@@ -650,7 +598,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
650598

651599
if let mir::Rvalue::Ref(region, _, ref place) = *rhs {
652600
if is_unsafe_place(self.tcx, self.mir, place) { return; }
653-
let index = self.location_map.get(&location).unwrap_or_else(|| {
601+
let index = self.borrow_set.location_map.get(&location).unwrap_or_else(|| {
654602
panic!("could not find BorrowIndex for location {:?}", location);
655603
});
656604

@@ -661,7 +609,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
661609
return
662610
}
663611

664-
assert!(self.region_map.get(region).unwrap_or_else(|| {
612+
assert!(self.borrow_set.region_map.get(region).unwrap_or_else(|| {
665613
panic!("could not find BorrowIndexs for region {:?}", region);
666614
}).contains(&index));
667615
sets.gen(&ReserveOrActivateIndex::reserved(*index));
@@ -739,7 +687,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
739687
// and hence most of these loans will already be dead -- but, in some cases
740688
// like unwind paths, we do not always emit `EndRegion` statements, so we
741689
// add some kills here as a "backup" and to avoid spurious error messages.
742-
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() {
690+
for (borrow_index, borrow_data) in self.borrow_set.borrows.iter_enumerated() {
743691
if let ReScope(scope) = borrow_data.region {
744692
// Check that the scope is not actually a scope from a function that is
745693
// a parent of our closure. Note that the CallSite scope itself is

src/librustc_mir/dataflow/impls/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_data_structures::indexed_set::{IdxSet};
1919
use rustc_data_structures::indexed_vec::Idx;
2020

2121
use super::MoveDataParamEnv;
22+
2223
use util::elaborate_drops::DropFlagState;
2324

2425
use super::move_paths::{HasMoveData, MoveData, MoveOutIndex, MovePathIndex, InitIndex};

src/librustc_mir/dataflow/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub use self::impls::{MaybeStorageLive};
2929
pub use self::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
3030
pub use self::impls::{DefinitelyInitializedPlaces, MovingOutStatements};
3131
pub use self::impls::EverInitializedPlaces;
32-
pub use self::impls::borrows::{Borrows, BorrowData};
32+
pub use self::impls::borrows::Borrows;
3333
pub use self::impls::HaveBeenBorrowedLocals;
3434
pub(crate) use self::impls::borrows::{ReserveOrActivateIndex};
3535
pub use self::at_location::{FlowAtLocation, FlowsAtLocation};

src/librustc_mir/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
2020
#![feature(box_patterns)]
2121
#![feature(box_syntax)]
2222
#![feature(catch_expr)]
23+
#![feature(crate_visibility_modifier)]
2324
#![feature(const_fn)]
2425
#![feature(core_intrinsics)]
2526
#![feature(decl_macro)]

0 commit comments

Comments
 (0)