2017-10-24 14:20:47 -04:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
use borrow_check::borrow_set::BorrowSet;
|
|
|
|
use borrow_check::location::LocationTable;
|
2018-06-04 12:25:12 -04:00
|
|
|
use borrow_check::nll::ToRegionVid;
|
2018-05-01 10:49:11 -04:00
|
|
|
use borrow_check::nll::facts::AllFacts;
|
2018-07-01 19:43:01 -03:00
|
|
|
use borrow_check::nll::region_infer::RegionInferenceContext;
|
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;
|
2018-05-01 10:49:11 -04:00
|
|
|
use rustc::mir::{BasicBlock, BasicBlockData, Location, Mir, Place, Rvalue};
|
2018-07-01 10:22:36 -04:00
|
|
|
use rustc::mir::{Local, Statement, Terminator};
|
2017-10-24 17:14:39 -04:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
2018-05-01 10:49:11 -04:00
|
|
|
use rustc::ty::subst::Substs;
|
2018-05-02 13:14:30 +02:00
|
|
|
use rustc::ty::{self, CanonicalTy, ClosureSubsts, GeneratorSubsts};
|
2018-06-05 11:06:38 -04:00
|
|
|
use std::iter;
|
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>,
|
2017-10-30 04:51:10 -04:00
|
|
|
regioncx: &mut RegionInferenceContext<'tcx>,
|
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>,
|
2018-07-01 19:43:01 -03:00
|
|
|
liveness_set_from_typeck: &[(ty::Region<'tcx>, Location)],
|
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,
|
|
|
|
regioncx,
|
2018-05-01 10:49:11 -04:00
|
|
|
location_table,
|
|
|
|
all_facts,
|
2017-11-20 16:43:09 -05:00
|
|
|
};
|
|
|
|
|
2018-06-05 11:06:38 -04:00
|
|
|
cg.add_region_liveness_constraints_from_type_check(liveness_set_from_typeck);
|
|
|
|
|
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,
|
2017-11-17 04:34:02 -05:00
|
|
|
regioncx: &'cg mut RegionInferenceContext<'tcx>,
|
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.
|
|
|
|
fn visit_substs(&mut self, substs: &&'tcx Substs<'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 {
|
2018-05-01 10:49:11 -04:00
|
|
|
TyContext::ReturnTy(source_info)
|
|
|
|
| TyContext::YieldTy(source_info)
|
|
|
|
| TyContext::LocalDecl { source_info, .. } => {
|
|
|
|
span_bug!(
|
|
|
|
source_info.span,
|
|
|
|
"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,
|
|
|
|
block: BasicBlock,
|
|
|
|
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()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_statement(block, statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_assign(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
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 {
|
|
|
|
if let Place::Local(temp) = place {
|
|
|
|
if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) {
|
|
|
|
for &borrow_index in borrow_indices {
|
|
|
|
let location_index = self.location_table.mid_index(location);
|
|
|
|
all_facts.killed.push((borrow_index, location_index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_assign(block, place, rvalue, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
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),
|
|
|
|
));
|
|
|
|
|
|
|
|
for successor_block in terminator.successors() {
|
|
|
|
all_facts.cfg_edge.push((
|
|
|
|
self.location_table.mid_index(location),
|
|
|
|
self.location_table
|
|
|
|
.start_index(successor_block.start_location()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_terminator(block, terminator, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_user_assert_ty(
|
|
|
|
&mut self,
|
|
|
|
_c_ty: &CanonicalTy<'tcx>,
|
|
|
|
_local: &Local,
|
|
|
|
_location: Location,
|
|
|
|
) {
|
|
|
|
}
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'cg, 'gcx, 'tcx> ConstraintGeneration<'cx, 'cg, 'gcx, 'tcx> {
|
2018-06-05 11:06:38 -04:00
|
|
|
/// The MIR type checker generates region liveness constraints
|
|
|
|
/// that we also have to respect.
|
|
|
|
fn add_region_liveness_constraints_from_type_check(
|
|
|
|
&mut self,
|
2018-07-01 19:43:01 -03:00
|
|
|
liveness_set: &[(ty::Region<'tcx>, Location)],
|
2018-06-05 11:06:38 -04:00
|
|
|
) {
|
|
|
|
debug!(
|
|
|
|
"add_region_liveness_constraints_from_type_check(liveness_set={} items)",
|
|
|
|
liveness_set.len(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let ConstraintGeneration {
|
|
|
|
regioncx,
|
|
|
|
location_table,
|
|
|
|
all_facts,
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
|
2018-07-01 19:43:01 -03:00
|
|
|
for (region, location) in liveness_set {
|
2018-06-05 11:06:38 -04:00
|
|
|
debug!("generate: {:#?} is live at {:#?}", region, location);
|
|
|
|
let region_vid = regioncx.to_region_vid(region);
|
2018-07-01 19:43:01 -03:00
|
|
|
regioncx.add_live_point(region_vid, *location);
|
2018-06-05 11:06:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(all_facts) = all_facts {
|
|
|
|
all_facts
|
|
|
|
.region_live_at
|
2018-07-01 19:43:01 -03:00
|
|
|
.extend(liveness_set.into_iter().flat_map(|(region, location)| {
|
2018-06-05 11:06:38 -04:00
|
|
|
let r = regioncx.to_region_vid(region);
|
|
|
|
let p1 = location_table.start_index(*location);
|
|
|
|
let p2 = location_table.mid_index(*location);
|
|
|
|
iter::once((r, p1)).chain(iter::once((r, p2)))
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01 19:43:01 -03:00
|
|
|
self.regioncx.add_live_point(vid, location);
|
2017-10-24 17:14:39 -04:00
|
|
|
});
|
|
|
|
}
|
2017-10-24 18:28:39 -04:00
|
|
|
}
|