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-22 17:38:51 -05:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2017-11-06 05:21:48 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-11-22 17:38:51 -05:00
|
|
|
use rustc::ty::{self, ClosureSubsts, Ty, TypeFoldable};
|
2017-11-07 04:30:43 -05:00
|
|
|
use rustc::mir::{BasicBlock, Local, Location, Mir, Statement, StatementKind};
|
2017-11-22 17:38:51 -05:00
|
|
|
use rustc::mir::RETURN_PLACE;
|
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-11-21 13:12:24 -05:00
|
|
|
use super::universal_regions::UniversalRegions;
|
2017-10-30 04:51:10 -04:00
|
|
|
|
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>,
|
2017-11-21 13:12:24 -05:00
|
|
|
universal_regions: &UniversalRegions<'tcx>,
|
2017-10-30 04:51:10 -04:00
|
|
|
mir: &mut Mir<'tcx>,
|
2017-11-06 04:33:15 -05:00
|
|
|
) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!("renumber_mir()");
|
|
|
|
debug!("renumber_mir: mir.arg_count={:?}", mir.arg_count);
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
// Update the return type and types of the arguments based on the
|
|
|
|
// `universal_regions` computation.
|
|
|
|
debug!("renumber_mir: output_ty={:?}", universal_regions.output_ty);
|
|
|
|
mir.local_decls[RETURN_PLACE].ty = universal_regions.output_ty;
|
|
|
|
for (&input_ty, local) in universal_regions
|
|
|
|
.input_tys
|
|
|
|
.iter()
|
|
|
|
.zip((1..).map(Local::new))
|
|
|
|
{
|
|
|
|
debug!("renumber_mir: input_ty={:?} local={:?}", input_ty, local);
|
|
|
|
mir.local_decls[local].ty = input_ty;
|
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
let mut visitor = NLLVisitor {
|
|
|
|
infcx,
|
|
|
|
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
|
|
|
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>,
|
|
|
|
{
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!("renumber_regions(value={:?})", value);
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
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-11-22 17:38:51 -05:00
|
|
|
/// Checks that all the regions appearing in `value` have already
|
|
|
|
/// been renumbered. `FreeRegions` code should have done this.
|
|
|
|
fn assert_free_regions_are_renumbered<T>(&self, value: &T)
|
2017-10-30 04:51:10 -04:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
|
|
|
{
|
2017-11-22 17:38:51 -05:00
|
|
|
debug!("assert_free_regions_are_renumbered(value={:?})", value);
|
2017-11-07 04:30:43 -05:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
self.infcx.tcx.for_each_free_region(value, |region| {
|
|
|
|
region.to_region_vid(); // will panic if `region` is not renumbered
|
|
|
|
});
|
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-11-22 17:38:51 -05:00
|
|
|
if is_arg {
|
|
|
|
self.assert_free_regions_are_renumbered(ty);
|
2017-10-30 04:51:10 -04:00
|
|
|
} else {
|
2017-11-22 17:38:51 -05:00
|
|
|
*ty = self.renumber_regions(ty_context, ty);
|
|
|
|
}
|
|
|
|
|
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-11-07 04:30:43 -05:00
|
|
|
debug!("visit_substs(substs={:?}, location={:?})", substs, 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-11-07 04:30:43 -05:00
|
|
|
|
|
|
|
debug!("visit_substs: substs={:?}", substs);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 04:30:43 -05:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) {
|
|
|
|
debug!("visit_region(region={:?}, location={:?})", region, location);
|
|
|
|
|
|
|
|
let old_region = *region;
|
|
|
|
let ty_context = TyContext::Location(location);
|
|
|
|
*region = self.renumber_regions(ty_context, &old_region);
|
|
|
|
|
|
|
|
debug!("visit_region: region={:?}", region);
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:29:55 -05:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, location: Location) {
|
|
|
|
let ty_context = TyContext::Location(location);
|
|
|
|
*constant = self.renumber_regions(ty_context, &*constant);
|
|
|
|
}
|
|
|
|
|
2017-10-30 04:51:10 -04:00
|
|
|
fn visit_closure_substs(&mut self, substs: &mut ClosureSubsts<'tcx>, location: Location) {
|
2017-11-07 04:30:43 -05:00
|
|
|
debug!(
|
|
|
|
"visit_closure_substs(substs={:?}, location={:?})",
|
|
|
|
substs,
|
|
|
|
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-11-07 04:30:43 -05:00
|
|
|
|
|
|
|
debug!("visit_closure_substs: substs={:?}", 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);
|
|
|
|
}
|
|
|
|
}
|