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-11-06 04:04:45 -05:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
2017-11-06 05:21:48 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
|
|
|
use rustc::ty::{self, ClosureSubsts, RegionVid, Ty, TypeFoldable};
|
2017-10-30 04:51:10 -04:00
|
|
|
use rustc::mir::{BasicBlock, Local, Location, Mir, Rvalue, Statement, StatementKind};
|
2017-10-26 19:53:31 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
2017-11-06 05:21:48 -05:00
|
|
|
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
|
2017-10-24 14:20:47 -04:00
|
|
|
|
2017-11-06 05:21:48 -05:00
|
|
|
use super::ToRegionVid;
|
2017-10-30 04:51:10 -04:00
|
|
|
use super::free_regions::FreeRegions;
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
/// Replaces all free regions appearing in the MIR with fresh
|
|
|
|
/// inference variables, returning the number of variables created.
|
2017-10-30 04:51:10 -04:00
|
|
|
pub fn renumber_mir<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
free_regions: &FreeRegions<'tcx>,
|
|
|
|
mir: &mut Mir<'tcx>,
|
2017-11-06 04:33:15 -05:00
|
|
|
) {
|
2017-10-30 04:51:10 -04:00
|
|
|
// Create inference variables for each of the free regions
|
|
|
|
// declared on the function signature.
|
|
|
|
let free_region_inference_vars = (0..free_regions.indices.len())
|
2017-11-06 05:21:48 -05:00
|
|
|
.map(RegionVid::new)
|
|
|
|
.map(|vid_expected| {
|
|
|
|
let r = infcx.next_nll_region_var(NLLRegionVariableOrigin::FreeRegion);
|
|
|
|
assert_eq!(vid_expected, r.to_region_vid());
|
|
|
|
r
|
2017-10-30 04:51:10 -04:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut visitor = NLLVisitor {
|
|
|
|
infcx,
|
|
|
|
free_regions,
|
|
|
|
free_region_inference_vars,
|
|
|
|
arg_count: mir.arg_count,
|
|
|
|
};
|
2017-10-24 14:20:47 -04:00
|
|
|
visitor.visit_mir(mir);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
2017-10-30 04:51:10 -04:00
|
|
|
free_regions: &'a FreeRegions<'tcx>,
|
2017-11-06 04:15:38 -05:00
|
|
|
free_region_inference_vars: IndexVec<RegionVid, ty::Region<'tcx>>,
|
2017-10-30 04:51:10 -04:00
|
|
|
arg_count: usize,
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
|
2017-10-30 04:51:10 -04:00
|
|
|
/// Replaces all regions appearing in `value` with fresh inference
|
|
|
|
/// variables. This is what we do for almost the entire MIR, with
|
|
|
|
/// the exception of the declared types of our arguments.
|
2017-11-06 05:21:48 -05:00
|
|
|
fn renumber_regions<T>(&mut self, ty_context: TyContext, value: &T) -> T
|
2017-10-30 04:51:10 -04:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
self.infcx
|
|
|
|
.tcx
|
|
|
|
.fold_regions(value, &mut false, |_region, _depth| {
|
2017-11-06 05:21:48 -05:00
|
|
|
let origin = NLLRegionVariableOrigin::Inferred(ty_context);
|
|
|
|
self.infcx.next_nll_region_var(origin)
|
2017-10-30 04:51:10 -04:00
|
|
|
})
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
/// Renumbers the regions appearing in `value`, but those regions
|
|
|
|
/// are expected to be free regions from the function signature.
|
|
|
|
fn renumber_free_regions<T>(&mut self, value: &T) -> T
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
|
|
|
self.infcx
|
|
|
|
.tcx
|
|
|
|
.fold_regions(value, &mut false, |region, _depth| {
|
|
|
|
let index = self.free_regions.indices[®ion];
|
|
|
|
self.free_region_inference_vars[index]
|
|
|
|
})
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
fn is_argument_or_return_slot(&self, local: Local) -> bool {
|
|
|
|
// The first argument is return slot, next N are arguments.
|
|
|
|
local.index() <= self.arg_count
|
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
|
2017-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
|
2017-10-30 04:51:10 -04:00
|
|
|
let is_arg = match ty_context {
|
|
|
|
TyContext::LocalDecl { local, .. } => self.is_argument_or_return_slot(local),
|
2017-11-07 04:30:06 -05:00
|
|
|
TyContext::ReturnTy(..) => true,
|
|
|
|
TyContext::Location(..) => false,
|
2017-10-30 04:51:10 -04:00
|
|
|
};
|
2017-11-07 04:30:06 -05:00
|
|
|
debug!(
|
|
|
|
"visit_ty(ty={:?}, is_arg={:?}, ty_context={:?})",
|
|
|
|
ty,
|
|
|
|
is_arg,
|
|
|
|
ty_context
|
|
|
|
);
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
let old_ty = *ty;
|
2017-10-30 04:51:10 -04:00
|
|
|
*ty = if is_arg {
|
|
|
|
self.renumber_free_regions(&old_ty)
|
|
|
|
} else {
|
2017-11-06 05:21:48 -05:00
|
|
|
self.renumber_regions(ty_context, &old_ty)
|
2017-10-30 04:51:10 -04:00
|
|
|
};
|
2017-11-07 04:30:06 -05:00
|
|
|
debug!("visit_ty: ty={:?}", ty);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) {
|
2017-10-26 19:53:31 -04:00
|
|
|
let ty_context = TyContext::Location(location);
|
2017-11-06 05:21:48 -05:00
|
|
|
*substs = self.renumber_regions(ty_context, &{ *substs });
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
|
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(ref mut r, _, _) => {
|
|
|
|
let old_r = *r;
|
2017-10-26 19:53:31 -04:00
|
|
|
let ty_context = TyContext::Location(location);
|
2017-11-06 05:21:48 -05:00
|
|
|
*r = self.renumber_regions(ty_context, &old_r);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
Rvalue::Use(..) |
|
|
|
|
Rvalue::Repeat(..) |
|
|
|
|
Rvalue::Len(..) |
|
|
|
|
Rvalue::Cast(..) |
|
|
|
|
Rvalue::BinaryOp(..) |
|
|
|
|
Rvalue::CheckedBinaryOp(..) |
|
|
|
|
Rvalue::UnaryOp(..) |
|
|
|
|
Rvalue::Discriminant(..) |
|
|
|
|
Rvalue::NullaryOp(..) |
|
|
|
|
Rvalue::Aggregate(..) => {
|
|
|
|
// These variants don't contain regions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
fn visit_closure_substs(&mut self, substs: &mut ClosureSubsts<'tcx>, location: Location) {
|
2017-10-26 19:53:31 -04:00
|
|
|
let ty_context = TyContext::Location(location);
|
2017-11-06 05:21:48 -05:00
|
|
|
*substs = self.renumber_regions(ty_context, substs);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
fn visit_statement(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
statement: &mut Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
2017-10-24 14:20:47 -04:00
|
|
|
if let StatementKind::EndRegion(_) = statement.kind {
|
|
|
|
statement.kind = StatementKind::Nop;
|
|
|
|
}
|
|
|
|
self.super_statement(block, statement, location);
|
|
|
|
}
|
|
|
|
}
|