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.
|
|
|
|
|
2017-10-25 15:10:48 -04:00
|
|
|
use rustc::hir;
|
2017-11-20 16:43:09 -05:00
|
|
|
use rustc::mir::{BasicBlock, BasicBlockData, Location, Place, Mir, Rvalue};
|
2017-10-24 18:28:39 -04:00
|
|
|
use rustc::mir::visit::Visitor;
|
2017-12-01 14:31:47 +02:00
|
|
|
use rustc::mir::Place::Projection;
|
2017-12-03 13:14:36 -05:00
|
|
|
use rustc::mir::{PlaceProjection, ProjectionElem};
|
2017-11-20 16:43:09 -05:00
|
|
|
use rustc::mir::visit::TyContext;
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
2017-12-03 13:14:36 -05:00
|
|
|
use rustc::ty::{self, ClosureSubsts};
|
2017-11-20 16:43:09 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-10-24 17:14:39 -04:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
2017-10-24 14:20:47 -04:00
|
|
|
|
2017-11-06 04:15:38 -05:00
|
|
|
use super::ToRegionVid;
|
2017-10-24 14:20:47 -04:00
|
|
|
use super::region_infer::RegionInferenceContext;
|
|
|
|
|
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>,
|
2017-10-24 16:20:47 -04:00
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
) {
|
2017-11-20 16:43:09 -05:00
|
|
|
let mut cg = ConstraintGeneration {
|
2017-10-24 16:20:47 -04:00
|
|
|
infcx,
|
|
|
|
regioncx,
|
|
|
|
mir,
|
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>,
|
|
|
|
regioncx: &'cg mut RegionInferenceContext<'tcx>,
|
|
|
|
mir: &'cg Mir<'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) {
|
|
|
|
self.add_regular_live_constraint(*substs, location);
|
|
|
|
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) {
|
|
|
|
self.add_regular_live_constraint(*region, location);
|
|
|
|
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 {
|
|
|
|
TyContext::ReturnTy(source_info) |
|
|
|
|
TyContext::LocalDecl { source_info, .. } => {
|
|
|
|
span_bug!(source_info.span,
|
|
|
|
"should not be visiting outside of the CFG: {:?}",
|
|
|
|
ty_context);
|
|
|
|
}
|
|
|
|
TyContext::Location(location) => {
|
|
|
|
self.add_regular_live_constraint(*ty, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_ty(ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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) {
|
|
|
|
self.add_regular_live_constraint(*substs, location);
|
|
|
|
self.super_closure_substs(substs);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
|
|
|
debug!("visit_rvalue(rvalue={:?}, location={:?})", rvalue, location);
|
|
|
|
|
|
|
|
// Look for an rvalue like:
|
|
|
|
//
|
|
|
|
// & L
|
|
|
|
//
|
|
|
|
// where L is the path that is borrowed. In that case, we have
|
|
|
|
// to add the reborrow constraints (which don't fall out
|
|
|
|
// naturally from the type-checker).
|
|
|
|
if let Rvalue::Ref(region, _bk, ref borrowed_lv) = *rvalue {
|
|
|
|
self.add_reborrow_constraint(location, region, borrowed_lv);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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`.
|
|
|
|
fn add_regular_live_constraint<T>(&mut self, live_ty: T, location: Location)
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
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();
|
2017-10-24 17:14:39 -04:00
|
|
|
self.regioncx.add_live_point(vid, location);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-25 15:10:48 -04:00
|
|
|
fn add_reborrow_constraint(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
borrow_region: ty::Region<'tcx>,
|
2017-12-01 14:39:51 +02:00
|
|
|
borrowed_place: &Place<'tcx>,
|
2017-10-25 15:10:48 -04:00
|
|
|
) {
|
2017-12-01 14:39:51 +02:00
|
|
|
if let Projection(ref proj) = *borrowed_place {
|
2017-12-01 14:31:47 +02:00
|
|
|
let PlaceProjection { ref base, ref elem } = **proj;
|
2017-10-25 15:10:48 -04:00
|
|
|
|
|
|
|
if let ProjectionElem::Deref = *elem {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let base_ty = base.ty(self.mir, tcx).to_ty(tcx);
|
|
|
|
let base_sty = &base_ty.sty;
|
|
|
|
|
2017-11-20 16:43:09 -05:00
|
|
|
if let ty::TyRef(base_region, ty::TypeAndMut { ty: _, mutbl }) = *base_sty {
|
2017-10-25 15:10:48 -04:00
|
|
|
match mutbl {
|
2017-11-20 16:43:09 -05:00
|
|
|
hir::Mutability::MutImmutable => {}
|
2017-10-25 15:10:48 -04:00
|
|
|
|
|
|
|
hir::Mutability::MutMutable => {
|
|
|
|
self.add_reborrow_constraint(location, borrow_region, base);
|
2017-11-20 16:43:09 -05:00
|
|
|
}
|
2017-10-25 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
2017-10-31 13:21:58 -04:00
|
|
|
let span = self.mir.source_info(location).span;
|
2017-11-20 16:43:09 -05:00
|
|
|
self.regioncx.add_outlives(
|
|
|
|
span,
|
|
|
|
base_region.to_region_vid(),
|
|
|
|
borrow_region.to_region_vid(),
|
|
|
|
location.successor_within_block(),
|
|
|
|
);
|
2017-10-25 15:10:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 18:28:39 -04:00
|
|
|
}
|