2019-02-08 06:28:15 +09:00
|
|
|
use crate::borrow_check::borrow_set::BorrowSet;
|
|
|
|
use crate::borrow_check::location::LocationTable;
|
|
|
|
use crate::borrow_check::nll::ToRegionVid;
|
|
|
|
use crate::borrow_check::nll::facts::AllFacts;
|
|
|
|
use crate::borrow_check::nll::region_infer::values::LivenessValues;
|
2018-05-01 10:49:11 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
|
|
|
use rustc::mir::visit::TyContext;
|
2017-10-24 18:28:39 -04:00
|
|
|
use rustc::mir::visit::Visitor;
|
2019-02-22 05:24:03 +01:00
|
|
|
use rustc::mir::{BasicBlock, BasicBlockData, Location, Mir, Place, PlaceBase, Rvalue};
|
2019-01-12 14:55:23 +00:00
|
|
|
use rustc::mir::{SourceInfo, Statement, Terminator};
|
2018-10-22 11:58:06 +02:00
|
|
|
use rustc::mir::UserTypeProjection;
|
2017-10-24 17:14:39 -04:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
2018-10-10 17:07:10 -04:00
|
|
|
use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, RegionVid};
|
2019-02-09 22:11:53 +08:00
|
|
|
use rustc::ty::subst::SubstsRef;
|
2017-10-24 14:20:47 -04:00
|
|
|
|
2017-11-17 04:34:02 -05:00
|
|
|
pub(super) fn generate_constraints<'cx, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
|
2018-07-23 22:28:28 +03:00
|
|
|
liveness_constraints: &mut LivenessValues<RegionVid>,
|
2018-05-01 10:49:11 -04:00
|
|
|
all_facts: &mut Option<AllFacts>,
|
|
|
|
location_table: &LocationTable,
|
2017-10-24 16:20:47 -04:00
|
|
|
mir: &Mir<'tcx>,
|
2018-05-01 10:49:11 -04:00
|
|
|
borrow_set: &BorrowSet<'tcx>,
|
2017-10-24 16:20:47 -04:00
|
|
|
) {
|
2017-11-20 16:43:09 -05:00
|
|
|
let mut cg = ConstraintGeneration {
|
2018-05-01 10:49:11 -04:00
|
|
|
borrow_set,
|
2017-10-24 16:20:47 -04:00
|
|
|
infcx,
|
2018-07-23 22:10:18 +03:00
|
|
|
liveness_constraints,
|
2018-05-01 10:49:11 -04:00
|
|
|
location_table,
|
|
|
|
all_facts,
|
2017-11-20 16:43:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for (bb, data) in mir.basic_blocks().iter_enumerated() {
|
|
|
|
cg.visit_basic_block_data(bb, data);
|
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-17 04:34:02 -05:00
|
|
|
/// 'cg = the duration of the constraint generation process itself.
|
|
|
|
struct ConstraintGeneration<'cg, 'cx: 'cg, 'gcx: 'tcx, 'tcx: 'cx> {
|
|
|
|
infcx: &'cg InferCtxt<'cx, 'gcx, 'tcx>,
|
2018-05-01 10:49:11 -04:00
|
|
|
all_facts: &'cg mut Option<AllFacts>,
|
|
|
|
location_table: &'cg LocationTable,
|
2018-07-23 22:28:28 +03:00
|
|
|
liveness_constraints: &'cg mut LivenessValues<RegionVid>,
|
2018-05-01 10:49:11 -04:00
|
|
|
borrow_set: &'cg BorrowSet<'tcx>,
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 16:43:09 -05:00
|
|
|
impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx, 'tcx> {
|
|
|
|
fn visit_basic_block_data(&mut self, bb: BasicBlock, data: &BasicBlockData<'tcx>) {
|
|
|
|
self.super_basic_block_data(bb, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We sometimes have `substs` within an rvalue, or within a
|
|
|
|
/// call. Make them live at the location where they appear.
|
2019-02-09 22:11:53 +08:00
|
|
|
fn visit_substs(&mut self, substs: &SubstsRef<'tcx>, location: Location) {
|
2018-07-01 19:43:01 -03:00
|
|
|
self.add_regular_live_constraint(*substs, location);
|
2017-11-20 16:43:09 -05:00
|
|
|
self.super_substs(substs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We sometimes have `region` within an rvalue, or within a
|
|
|
|
/// call. Make them live at the location where they appear.
|
|
|
|
fn visit_region(&mut self, region: &ty::Region<'tcx>, location: Location) {
|
2018-07-01 19:43:01 -03:00
|
|
|
self.add_regular_live_constraint(*region, location);
|
2017-11-20 16:43:09 -05:00
|
|
|
self.super_region(region);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 16:43:09 -05:00
|
|
|
/// We sometimes have `ty` within an rvalue, or within a
|
|
|
|
/// call. Make them live at the location where they appear.
|
|
|
|
fn visit_ty(&mut self, ty: &ty::Ty<'tcx>, ty_context: TyContext) {
|
|
|
|
match ty_context {
|
2019-01-12 14:55:23 +00:00
|
|
|
TyContext::ReturnTy(SourceInfo { span, .. })
|
|
|
|
| TyContext::YieldTy(SourceInfo { span, .. })
|
|
|
|
| TyContext::UserTy(span)
|
|
|
|
| TyContext::LocalDecl { source_info: SourceInfo { span, .. }, .. } => {
|
2018-05-01 10:49:11 -04:00
|
|
|
span_bug!(
|
2019-01-12 14:55:23 +00:00
|
|
|
span,
|
2018-05-01 10:49:11 -04:00
|
|
|
"should not be visiting outside of the CFG: {:?}",
|
|
|
|
ty_context
|
|
|
|
);
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
TyContext::Location(location) => {
|
2018-07-01 19:43:01 -03:00
|
|
|
self.add_regular_live_constraint(*ty, location);
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_ty(ty);
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
/// We sometimes have `generator_substs` within an rvalue, or within a
|
|
|
|
/// call. Make them live at the location where they appear.
|
|
|
|
fn visit_generator_substs(&mut self, substs: &GeneratorSubsts<'tcx>, location: Location) {
|
2018-07-01 19:43:01 -03:00
|
|
|
self.add_regular_live_constraint(*substs, location);
|
2018-05-02 13:14:30 +02:00
|
|
|
self.super_generator_substs(substs);
|
|
|
|
}
|
|
|
|
|
2017-11-20 16:43:09 -05:00
|
|
|
/// We sometimes have `closure_substs` within an rvalue, or within a
|
|
|
|
/// call. Make them live at the location where they appear.
|
|
|
|
fn visit_closure_substs(&mut self, substs: &ClosureSubsts<'tcx>, location: Location) {
|
2018-07-01 19:43:01 -03:00
|
|
|
self.add_regular_live_constraint(*substs, location);
|
2017-11-20 16:43:09 -05:00
|
|
|
self.super_closure_substs(substs);
|
|
|
|
}
|
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
fn visit_statement(
|
|
|
|
&mut self,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
if let Some(all_facts) = self.all_facts {
|
|
|
|
all_facts.cfg_edge.push((
|
|
|
|
self.location_table.start_index(location),
|
|
|
|
self.location_table.mid_index(location),
|
|
|
|
));
|
|
|
|
|
|
|
|
all_facts.cfg_edge.push((
|
|
|
|
self.location_table.mid_index(location),
|
|
|
|
self.location_table
|
|
|
|
.start_index(location.successor_within_block()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:07:14 +01:00
|
|
|
self.super_statement(statement, location);
|
2018-05-01 10:49:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_assign(
|
|
|
|
&mut self,
|
|
|
|
place: &Place<'tcx>,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
// When we see `X = ...`, then kill borrows of
|
|
|
|
// `(*X).foo` and so forth.
|
|
|
|
if let Some(all_facts) = self.all_facts {
|
2019-02-22 05:24:03 +01:00
|
|
|
if let Place::Base(PlaceBase::Local(temp)) = place {
|
2018-05-01 10:49:11 -04:00
|
|
|
if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) {
|
2018-10-17 16:52:35 +02:00
|
|
|
all_facts.killed.reserve(borrow_indices.len());
|
2018-05-01 10:49:11 -04:00
|
|
|
for &borrow_index in borrow_indices {
|
|
|
|
let location_index = self.location_table.mid_index(location);
|
|
|
|
all_facts.killed.push((borrow_index, location_index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:07:14 +01:00
|
|
|
self.super_assign(place, rvalue, location);
|
2018-05-01 10:49:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator(
|
|
|
|
&mut self,
|
|
|
|
terminator: &Terminator<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
if let Some(all_facts) = self.all_facts {
|
|
|
|
all_facts.cfg_edge.push((
|
|
|
|
self.location_table.start_index(location),
|
|
|
|
self.location_table.mid_index(location),
|
|
|
|
));
|
|
|
|
|
2018-10-17 16:52:35 +02:00
|
|
|
let successor_blocks = terminator.successors();
|
|
|
|
all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
|
|
|
|
for successor_block in successor_blocks {
|
2018-05-01 10:49:11 -04:00
|
|
|
all_facts.cfg_edge.push((
|
|
|
|
self.location_table.mid_index(location),
|
|
|
|
self.location_table
|
|
|
|
.start_index(successor_block.start_location()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:07:14 +01:00
|
|
|
self.super_terminator(terminator, location);
|
2018-05-01 10:49:11 -04:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:59:35 -04:00
|
|
|
fn visit_ascribe_user_ty(
|
2018-05-01 10:49:11 -04:00
|
|
|
&mut self,
|
2018-08-31 18:59:35 -04:00
|
|
|
_place: &Place<'tcx>,
|
2018-09-05 15:52:01 -04:00
|
|
|
_variance: &ty::Variance,
|
2019-03-28 18:00:17 -07:00
|
|
|
_user_ty: &UserTypeProjection,
|
2018-05-01 10:49:11 -04:00
|
|
|
_location: Location,
|
|
|
|
) {
|
|
|
|
}
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'cg, 'gcx, 'tcx> ConstraintGeneration<'cx, 'cg, 'gcx, 'tcx> {
|
2017-10-24 17:14:39 -04:00
|
|
|
/// Some variable with type `live_ty` is "regular live" at
|
|
|
|
/// `location` -- i.e., it may be used later. This means that all
|
|
|
|
/// regions appearing in the type `live_ty` must be live at
|
|
|
|
/// `location`.
|
2018-07-01 19:43:01 -03:00
|
|
|
fn add_regular_live_constraint<T>(&mut self, live_ty: T, location: Location)
|
2017-10-24 17:14:39 -04:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
debug!(
|
|
|
|
"add_regular_live_constraint(live_ty={:?}, location={:?})",
|
2018-05-01 10:49:11 -04:00
|
|
|
live_ty, location
|
2017-10-24 17:14:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
self.infcx
|
|
|
|
.tcx
|
|
|
|
.for_each_free_region(&live_ty, |live_region| {
|
2017-11-06 04:15:38 -05:00
|
|
|
let vid = live_region.to_region_vid();
|
2018-07-23 22:14:56 +03:00
|
|
|
self.liveness_constraints.add_element(vid, location);
|
2017-10-24 17:14:39 -04:00
|
|
|
});
|
|
|
|
}
|
2017-10-24 18:28:39 -04:00
|
|
|
}
|