Skip to content

Commit d3a90e7

Browse files
committed
Move find_use stuff to it's own file
1 parent a7052f4 commit d3a90e7

File tree

2 files changed

+148
-133
lines changed

2 files changed

+148
-133
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright 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 borrow_check::borrow_set::BorrowData;
12+
use borrow_check::nll::region_infer::RegionInferenceContext;
13+
use rustc::mir::visit::{MirVisitable, PlaceContext, Visitor};
14+
use rustc::mir::{Local, Location, Mir};
15+
use rustc_data_structures::fx::FxHashSet;
16+
use util::liveness::{self, DefUse, LivenessMode};
17+
18+
crate fn regular_use<'gcx, 'tcx>(
19+
mir: &'gcx Mir,
20+
regioncx: &'tcx RegionInferenceContext,
21+
borrow: &'tcx BorrowData,
22+
start_point: Location,
23+
local: Local,
24+
) -> Option<Location> {
25+
let mut uf = UseFinder {
26+
mir,
27+
regioncx,
28+
borrow,
29+
start_point,
30+
local,
31+
liveness_mode: LivenessMode {
32+
include_regular_use: true,
33+
include_drops: false,
34+
},
35+
};
36+
37+
uf.find()
38+
}
39+
40+
crate fn drop_use<'gcx, 'tcx>(
41+
mir: &'gcx Mir,
42+
regioncx: &'tcx RegionInferenceContext,
43+
borrow: &'tcx BorrowData,
44+
start_point: Location,
45+
local: Local,
46+
) -> Option<Location> {
47+
let mut uf = UseFinder {
48+
mir,
49+
regioncx,
50+
borrow,
51+
start_point,
52+
local,
53+
liveness_mode: LivenessMode {
54+
include_regular_use: false,
55+
include_drops: true,
56+
},
57+
};
58+
59+
uf.find()
60+
}
61+
62+
struct UseFinder<'gcx, 'tcx> {
63+
mir: &'gcx Mir<'gcx>,
64+
regioncx: &'tcx RegionInferenceContext<'tcx>,
65+
borrow: &'tcx BorrowData<'tcx>,
66+
start_point: Location,
67+
local: Local,
68+
liveness_mode: LivenessMode,
69+
}
70+
71+
impl<'gcx, 'tcx> UseFinder<'gcx, 'tcx> {
72+
fn find(&mut self) -> Option<Location> {
73+
let mut stack = vec![];
74+
let mut visited = FxHashSet();
75+
76+
stack.push(self.start_point);
77+
while let Some(p) = stack.pop() {
78+
if !self.regioncx.region_contains_point(self.borrow.region, p) {
79+
continue;
80+
}
81+
82+
if !visited.insert(p) {
83+
continue;
84+
}
85+
86+
let block_data = &self.mir[p.block];
87+
let (defined, used) = self.def_use(p, block_data.visitable(p.statement_index));
88+
89+
if used {
90+
return Some(p);
91+
} else if !defined {
92+
if p.statement_index < block_data.statements.len() {
93+
stack.push(Location {
94+
statement_index: p.statement_index + 1,
95+
..p
96+
});
97+
} else {
98+
stack.extend(block_data.terminator().successors().map(|&basic_block| {
99+
Location {
100+
statement_index: 0,
101+
block: basic_block,
102+
}
103+
}));
104+
}
105+
}
106+
}
107+
108+
None
109+
}
110+
111+
fn def_use(&self, location: Location, thing: &dyn MirVisitable<'tcx>) -> (bool, bool) {
112+
let mut visitor = DefUseVisitor {
113+
defined: false,
114+
used: false,
115+
local: self.local,
116+
liveness_mode: self.liveness_mode,
117+
};
118+
119+
thing.apply(location, &mut visitor);
120+
121+
(visitor.defined, visitor.used)
122+
}
123+
}
124+
125+
struct DefUseVisitor {
126+
defined: bool,
127+
used: bool,
128+
local: Local,
129+
liveness_mode: LivenessMode,
130+
}
131+
132+
impl<'tcx> Visitor<'tcx> for DefUseVisitor {
133+
fn visit_local(&mut self, &local: &Local, context: PlaceContext<'tcx>, _: Location) {
134+
if local == self.local {
135+
match liveness::categorize(context, self.liveness_mode) {
136+
Some(DefUse::Def) => self.defined = true,
137+
Some(DefUse::Use) => self.used = true,
138+
None => (),
139+
}
140+
}
141+
}
142+
}

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

Lines changed: 6 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
// except according to those terms.
1010

1111
use borrow_check::borrow_set::BorrowData;
12-
use borrow_check::nll::region_infer::{Cause, RegionInferenceContext};
12+
use borrow_check::nll::region_infer::Cause;
1313
use borrow_check::{Context, MirBorrowckCtxt, WriteKind};
14-
use rustc::mir::visit::{MirVisitable, PlaceContext, Visitor};
15-
use rustc::mir::{Local, Location, Mir, Place};
16-
use rustc_data_structures::fx::FxHashSet;
14+
use rustc::mir::Place;
1715
use rustc_errors::DiagnosticBuilder;
18-
use util::liveness::{self, DefUse, LivenessMode};
16+
17+
mod find_use;
1918

2019
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
2120
/// Adds annotations to `err` explaining *why* the borrow contains the
@@ -45,7 +44,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
4544
let borrow_region_vid = regioncx.to_region_vid(borrow.region);
4645
if let Some(cause) = regioncx.why_region_contains_point(borrow_region_vid, context.loc) {
4746
match cause {
48-
Cause::LiveVar(local, location) => match find_regular_use(
47+
Cause::LiveVar(local, location) => match find_use::regular_use(
4948
mir, regioncx, borrow, location, local,
5049
) {
5150
Some(p) => {
@@ -60,7 +59,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
6059
}
6160
},
6261

63-
Cause::DropVar(local, location) => match find_drop_use(
62+
Cause::DropVar(local, location) => match find_use::drop_use(
6463
mir, regioncx, borrow, location, local,
6564
) {
6665
Some(p) => match &mir.local_decls[local].name {
@@ -124,129 +123,3 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
124123
}
125124
}
126125
}
127-
128-
fn find_regular_use<'gcx, 'tcx>(
129-
mir: &'gcx Mir,
130-
regioncx: &'tcx RegionInferenceContext,
131-
borrow: &'tcx BorrowData,
132-
start_point: Location,
133-
local: Local,
134-
) -> Option<Location> {
135-
let mut uf = UseFinder {
136-
mir,
137-
regioncx,
138-
borrow,
139-
start_point,
140-
local,
141-
liveness_mode: LivenessMode {
142-
include_regular_use: true,
143-
include_drops: false,
144-
},
145-
};
146-
147-
uf.find()
148-
}
149-
150-
fn find_drop_use<'gcx, 'tcx>(
151-
mir: &'gcx Mir,
152-
regioncx: &'tcx RegionInferenceContext,
153-
borrow: &'tcx BorrowData,
154-
start_point: Location,
155-
local: Local,
156-
) -> Option<Location> {
157-
let mut uf = UseFinder {
158-
mir,
159-
regioncx,
160-
borrow,
161-
start_point,
162-
local,
163-
liveness_mode: LivenessMode {
164-
include_regular_use: false,
165-
include_drops: true,
166-
},
167-
};
168-
169-
uf.find()
170-
}
171-
172-
struct UseFinder<'gcx, 'tcx> {
173-
mir: &'gcx Mir<'gcx>,
174-
regioncx: &'tcx RegionInferenceContext<'tcx>,
175-
borrow: &'tcx BorrowData<'tcx>,
176-
start_point: Location,
177-
local: Local,
178-
liveness_mode: LivenessMode,
179-
}
180-
181-
impl<'gcx, 'tcx> UseFinder<'gcx, 'tcx> {
182-
fn find(&mut self) -> Option<Location> {
183-
let mut stack = vec![];
184-
let mut visited = FxHashSet();
185-
186-
stack.push(self.start_point);
187-
while let Some(p) = stack.pop() {
188-
if !self.regioncx.region_contains_point(self.borrow.region, p) {
189-
continue;
190-
}
191-
192-
if !visited.insert(p) {
193-
continue;
194-
}
195-
196-
let block_data = &self.mir[p.block];
197-
let (defined, used) = self.def_use(p, block_data.visitable(p.statement_index));
198-
199-
if used {
200-
return Some(p);
201-
} else if !defined {
202-
if p.statement_index < block_data.statements.len() {
203-
stack.push(Location {
204-
statement_index: p.statement_index + 1,
205-
..p
206-
});
207-
} else {
208-
stack.extend(block_data.terminator().successors().map(|&basic_block| {
209-
Location {
210-
statement_index: 0,
211-
block: basic_block,
212-
}
213-
}));
214-
}
215-
}
216-
}
217-
218-
None
219-
}
220-
221-
fn def_use(&self, location: Location, thing: &dyn MirVisitable<'tcx>) -> (bool, bool) {
222-
let mut visitor = DefUseVisitor {
223-
defined: false,
224-
used: false,
225-
local: self.local,
226-
liveness_mode: self.liveness_mode,
227-
};
228-
229-
thing.apply(location, &mut visitor);
230-
231-
(visitor.defined, visitor.used)
232-
}
233-
}
234-
235-
struct DefUseVisitor {
236-
defined: bool,
237-
used: bool,
238-
local: Local,
239-
liveness_mode: LivenessMode,
240-
}
241-
242-
impl<'tcx> Visitor<'tcx> for DefUseVisitor {
243-
fn visit_local(&mut self, &local: &Local, context: PlaceContext<'tcx>, _: Location) {
244-
if local == self.local {
245-
match liveness::categorize(context, self.liveness_mode) {
246-
Some(DefUse::Def) => self.defined = true,
247-
Some(DefUse::Use) => self.used = true,
248-
None => (),
249-
}
250-
}
251-
}
252-
}

0 commit comments

Comments
 (0)