2022-09-14 23:07:19 +08:00
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_infer::infer::InferCtxt;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::visit::TyContext;
|
|
|
|
use rustc_middle::mir::visit::Visitor;
|
|
|
|
use rustc_middle::mir::{
|
2019-12-11 16:50:03 -03:00
|
|
|
BasicBlock, BasicBlockData, Body, Local, Location, Place, PlaceRef, ProjectionElem, Rvalue,
|
|
|
|
SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UserTypeProjection,
|
2019-07-16 17:30:41 +02:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
2022-06-17 13:10:07 +01:00
|
|
|
use rustc_middle::ty::visit::TypeVisitable;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, RegionVid, Ty};
|
2017-10-24 14:20:47 -04:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::{
|
2019-11-29 19:48:29 -06:00
|
|
|
borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, nll::ToRegionVid,
|
|
|
|
places_conflict, region_infer::values::LivenessValues,
|
|
|
|
};
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
pub(super) fn generate_constraints<'tcx>(
|
|
|
|
infcx: &InferCtxt<'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,
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &Body<'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,
|
2019-07-16 17:30:41 +02:00
|
|
|
body,
|
2017-11-20 16:43:09 -05:00
|
|
|
};
|
|
|
|
|
2022-07-05 00:00:00 +00:00
|
|
|
for (bb, data) in body.basic_blocks.iter_enumerated() {
|
2017-11-20 16:43:09 -05:00
|
|
|
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.
|
2022-09-09 13:01:06 -05:00
|
|
|
struct ConstraintGeneration<'cg, 'tcx> {
|
|
|
|
infcx: &'cg InferCtxt<'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>,
|
2019-07-16 17:30:41 +02:00
|
|
|
body: &'cg Body<'tcx>,
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
impl<'cg, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'tcx> {
|
2017-11-20 16:43:09 -05:00
|
|
|
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.
|
2022-01-28 11:25:15 +11:00
|
|
|
fn visit_region(&mut self, region: ty::Region<'tcx>, location: Location) {
|
|
|
|
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.
|
2019-04-26 14:26:49 +02:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>, ty_context: TyContext) {
|
2017-11-20 16:43:09 -05:00
|
|
|
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!(span, "should not be visiting outside of the CFG: {:?}", ty_context);
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
TyContext::Location(location) => {
|
2019-04-25 22:54:19 +02:00
|
|
|
self.add_regular_live_constraint(ty, location);
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_ty(ty);
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-09 19:16:48 +01:00
|
|
|
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
|
2018-05-01 10:49:11 -04:00
|
|
|
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-07-15 15:54:43 +02:00
|
|
|
|
|
|
|
// If there are borrows on this now dead local, we need to record them as `killed`.
|
2020-01-14 01:23:51 -03:00
|
|
|
if let StatementKind::StorageDead(local) = statement.kind {
|
2019-07-15 15:54:43 +02:00
|
|
|
record_killed_borrows_for_local(
|
|
|
|
all_facts,
|
|
|
|
self.borrow_set,
|
|
|
|
self.location_table,
|
|
|
|
local,
|
|
|
|
location,
|
|
|
|
);
|
|
|
|
}
|
2018-05-01 10:49:11 -04:00
|
|
|
}
|
|
|
|
|
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.
|
2020-03-30 17:49:33 -03:00
|
|
|
self.record_killed_borrows_for_place(*place, location);
|
2018-05-01 10:49:11 -04:00
|
|
|
|
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 {
|
2019-12-09 19:16:48 +01:00
|
|
|
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
|
2018-05-01 10:49:11 -04:00
|
|
|
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-07-15 15:54:43 +02:00
|
|
|
// A `Call` terminator's return value can be a local which has borrows,
|
|
|
|
// so we need to record those as `killed` as well.
|
2020-03-30 17:49:33 -03:00
|
|
|
if let TerminatorKind::Call { destination, .. } = terminator.kind {
|
2022-04-16 09:27:54 -04:00
|
|
|
self.record_killed_borrows_for_place(destination, location);
|
2019-07-15 15:54:43 +02:00
|
|
|
}
|
|
|
|
|
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>,
|
2022-07-04 09:40:58 +02: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
|
|
|
}
|
|
|
|
|
2022-09-09 13:01:06 -05:00
|
|
|
impl<'cx, 'tcx> ConstraintGeneration<'cx, '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
|
2022-06-17 13:10:07 +01:00
|
|
|
T: TypeVisitable<'tcx>,
|
2017-10-24 17:14:39 -04:00
|
|
|
{
|
|
|
|
debug!("add_regular_live_constraint(live_ty={:?}, location={:?})", live_ty, location);
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2019-07-15 15:54:43 +02:00
|
|
|
|
|
|
|
/// When recording facts for Polonius, records the borrows on the specified place
|
|
|
|
/// as `killed`. For example, when assigning to a local, or on a call's return destination.
|
2020-03-30 17:49:33 -03:00
|
|
|
fn record_killed_borrows_for_place(&mut self, place: Place<'tcx>, location: Location) {
|
2019-07-15 15:54:43 +02:00
|
|
|
if let Some(all_facts) = self.all_facts {
|
2019-12-09 19:16:48 +01:00
|
|
|
let _prof_timer = self.infcx.tcx.prof.generic_activity("polonius_fact_generation");
|
|
|
|
|
2019-07-16 17:30:41 +02:00
|
|
|
// Depending on the `Place` we're killing:
|
|
|
|
// - if it's a local, or a single deref of a local,
|
|
|
|
// we kill all the borrows on the local.
|
|
|
|
// - if it's a deeper projection, we have to filter which
|
|
|
|
// of the borrows are killed: the ones whose `borrowed_place`
|
|
|
|
// conflicts with the `place`.
|
2019-10-20 16:09:36 -04:00
|
|
|
match place.as_ref() {
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: &[] }
|
|
|
|
| PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
|
2019-07-16 17:30:41 +02:00
|
|
|
debug!(
|
|
|
|
"Recording `killed` facts for borrows of local={:?} at location={:?}",
|
|
|
|
local, location
|
|
|
|
);
|
|
|
|
|
|
|
|
record_killed_borrows_for_local(
|
|
|
|
all_facts,
|
|
|
|
self.borrow_set,
|
|
|
|
self.location_table,
|
2020-01-14 02:10:05 -03:00
|
|
|
local,
|
2019-07-16 17:30:41 +02:00
|
|
|
location,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
PlaceRef { local, projection: &[.., _] } => {
|
2019-07-16 17:30:41 +02:00
|
|
|
// Kill conflicting borrows of the innermost local.
|
|
|
|
debug!(
|
|
|
|
"Recording `killed` facts for borrows of \
|
|
|
|
innermost projected local={:?} at location={:?}",
|
|
|
|
local, location
|
|
|
|
);
|
|
|
|
|
2020-01-14 02:10:05 -03:00
|
|
|
if let Some(borrow_indices) = self.borrow_set.local_map.get(&local) {
|
2019-07-16 17:30:41 +02:00
|
|
|
for &borrow_index in borrow_indices {
|
|
|
|
let places_conflict = places_conflict::places_conflict(
|
|
|
|
self.infcx.tcx,
|
|
|
|
self.body,
|
2020-08-07 20:47:33 -07:00
|
|
|
self.borrow_set[borrow_index].borrowed_place,
|
2019-07-16 17:30:41 +02:00
|
|
|
place,
|
|
|
|
places_conflict::PlaceConflictBias::NoOverlap,
|
|
|
|
);
|
|
|
|
|
|
|
|
if places_conflict {
|
|
|
|
let location_index = self.location_table.mid_index(location);
|
2021-07-22 22:21:06 +02:00
|
|
|
all_facts.loan_killed_at.push((borrow_index, location_index));
|
2019-07-16 17:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 15:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// When recording facts for Polonius, records the borrows on the specified local as `killed`.
|
|
|
|
fn record_killed_borrows_for_local(
|
|
|
|
all_facts: &mut AllFacts,
|
|
|
|
borrow_set: &BorrowSet<'_>,
|
|
|
|
location_table: &LocationTable,
|
2020-01-14 01:23:51 -03:00
|
|
|
local: Local,
|
2019-07-15 15:54:43 +02:00
|
|
|
location: Location,
|
|
|
|
) {
|
2020-01-14 01:23:51 -03:00
|
|
|
if let Some(borrow_indices) = borrow_set.local_map.get(&local) {
|
2021-07-22 22:21:06 +02:00
|
|
|
all_facts.loan_killed_at.reserve(borrow_indices.len());
|
2019-07-15 15:54:43 +02:00
|
|
|
for &borrow_index in borrow_indices {
|
|
|
|
let location_index = location_table.mid_index(location);
|
2021-07-22 22:21:06 +02:00
|
|
|
all_facts.loan_killed_at.push((borrow_index, location_index));
|
2019-07-15 15:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 18:28:39 -04:00
|
|
|
}
|