1 use crate::borrow_check::borrow_set::BorrowSet;
2 use crate::borrow_check::location::LocationTable;
3 use crate::borrow_check::nll::ToRegionVid;
4 use crate::borrow_check::nll::facts::AllFacts;
5 use crate::borrow_check::nll::region_infer::values::LivenessValues;
6 use rustc::infer::InferCtxt;
7 use rustc::mir::visit::TyContext;
8 use rustc::mir::visit::Visitor;
9 use rustc::mir::{BasicBlock, BasicBlockData, Location, Mir, Place, Rvalue};
10 use rustc::mir::{SourceInfo, Statement, Terminator};
11 use rustc::mir::UserTypeProjection;
12 use rustc::ty::fold::TypeFoldable;
13 use rustc::ty::subst::Substs;
14 use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, RegionVid};
16 pub(super) fn generate_constraints<'cx, 'gcx, 'tcx>(
17 infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
18 liveness_constraints: &mut LivenessValues<RegionVid>,
19 all_facts: &mut Option<AllFacts>,
20 location_table: &LocationTable,
22 borrow_set: &BorrowSet<'tcx>,
24 let mut cg = ConstraintGeneration {
32 for (bb, data) in mir.basic_blocks().iter_enumerated() {
33 cg.visit_basic_block_data(bb, data);
37 /// 'cg = the duration of the constraint generation process itself.
38 struct ConstraintGeneration<'cg, 'cx: 'cg, 'gcx: 'tcx, 'tcx: 'cx> {
39 infcx: &'cg InferCtxt<'cx, 'gcx, 'tcx>,
40 all_facts: &'cg mut Option<AllFacts>,
41 location_table: &'cg LocationTable,
42 liveness_constraints: &'cg mut LivenessValues<RegionVid>,
43 borrow_set: &'cg BorrowSet<'tcx>,
46 impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx, 'tcx> {
47 fn visit_basic_block_data(&mut self, bb: BasicBlock, data: &BasicBlockData<'tcx>) {
48 self.super_basic_block_data(bb, data);
51 /// We sometimes have `substs` within an rvalue, or within a
52 /// call. Make them live at the location where they appear.
53 fn visit_substs(&mut self, substs: &&'tcx Substs<'tcx>, location: Location) {
54 self.add_regular_live_constraint(*substs, location);
55 self.super_substs(substs);
58 /// We sometimes have `region` within an rvalue, or within a
59 /// call. Make them live at the location where they appear.
60 fn visit_region(&mut self, region: &ty::Region<'tcx>, location: Location) {
61 self.add_regular_live_constraint(*region, location);
62 self.super_region(region);
65 /// We sometimes have `ty` within an rvalue, or within a
66 /// call. Make them live at the location where they appear.
67 fn visit_ty(&mut self, ty: &ty::Ty<'tcx>, ty_context: TyContext) {
69 TyContext::ReturnTy(SourceInfo { span, .. })
70 | TyContext::YieldTy(SourceInfo { span, .. })
71 | TyContext::UserTy(span)
72 | TyContext::LocalDecl { source_info: SourceInfo { span, .. }, .. } => {
75 "should not be visiting outside of the CFG: {:?}",
79 TyContext::Location(location) => {
80 self.add_regular_live_constraint(*ty, location);
87 /// We sometimes have `generator_substs` within an rvalue, or within a
88 /// call. Make them live at the location where they appear.
89 fn visit_generator_substs(&mut self, substs: &GeneratorSubsts<'tcx>, location: Location) {
90 self.add_regular_live_constraint(*substs, location);
91 self.super_generator_substs(substs);
94 /// We sometimes have `closure_substs` within an rvalue, or within a
95 /// call. Make them live at the location where they appear.
96 fn visit_closure_substs(&mut self, substs: &ClosureSubsts<'tcx>, location: Location) {
97 self.add_regular_live_constraint(*substs, location);
98 self.super_closure_substs(substs);
104 statement: &Statement<'tcx>,
107 if let Some(all_facts) = self.all_facts {
108 all_facts.cfg_edge.push((
109 self.location_table.start_index(location),
110 self.location_table.mid_index(location),
113 all_facts.cfg_edge.push((
114 self.location_table.mid_index(location),
116 .start_index(location.successor_within_block()),
120 self.super_statement(block, statement, location);
127 rvalue: &Rvalue<'tcx>,
130 // When we see `X = ...`, then kill borrows of
131 // `(*X).foo` and so forth.
132 if let Some(all_facts) = self.all_facts {
133 if let Place::Local(temp) = place {
134 if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) {
135 all_facts.killed.reserve(borrow_indices.len());
136 for &borrow_index in borrow_indices {
137 let location_index = self.location_table.mid_index(location);
138 all_facts.killed.push((borrow_index, location_index));
144 self.super_assign(block, place, rvalue, location);
150 terminator: &Terminator<'tcx>,
153 if let Some(all_facts) = self.all_facts {
154 all_facts.cfg_edge.push((
155 self.location_table.start_index(location),
156 self.location_table.mid_index(location),
159 let successor_blocks = terminator.successors();
160 all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
161 for successor_block in successor_blocks {
162 all_facts.cfg_edge.push((
163 self.location_table.mid_index(location),
165 .start_index(successor_block.start_location()),
170 self.super_terminator(block, terminator, location);
173 fn visit_ascribe_user_ty(
175 _place: &Place<'tcx>,
176 _variance: &ty::Variance,
177 _user_ty: &UserTypeProjection<'tcx>,
183 impl<'cx, 'cg, 'gcx, 'tcx> ConstraintGeneration<'cx, 'cg, 'gcx, 'tcx> {
184 /// Some variable with type `live_ty` is "regular live" at
185 /// `location` -- i.e., it may be used later. This means that all
186 /// regions appearing in the type `live_ty` must be live at
188 fn add_regular_live_constraint<T>(&mut self, live_ty: T, location: Location)
190 T: TypeFoldable<'tcx>,
193 "add_regular_live_constraint(live_ty={:?}, location={:?})",
199 .for_each_free_region(&live_ty, |live_region| {
200 let vid = live_region.to_region_vid();
201 self.liveness_constraints.add_element(vid, location);