2015-11-16 11:41:16 -06:00
|
|
|
// Copyright 2015 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-08-11 13:34:14 -05:00
|
|
|
//! This pass erases all early-bound regions from the types occurring in the MIR.
|
2015-11-16 11:41:16 -06:00
|
|
|
//! We want to do this once just before trans, so trans does not have to take
|
|
|
|
//! care erasing regions all over the place.
|
2017-07-22 03:04:16 -05:00
|
|
|
//! NOTE: We do NOT erase regions of statements that are relevant for
|
|
|
|
//! "types-as-contracts"-validation, namely, AcquireValid, ReleaseValid, and EndRegion.
|
2015-11-16 11:41:16 -06:00
|
|
|
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-08-04 03:25:13 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2016-09-19 15:50:00 -05:00
|
|
|
use rustc::mir::*;
|
2017-10-26 18:53:31 -05:00
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
2017-11-10 11:20:35 -06:00
|
|
|
use transform::{MirPass, MirSource};
|
2015-11-16 11:41:16 -06:00
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-07-11 16:30:30 -05:00
|
|
|
in_validation_statement: bool,
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
|
2016-02-05 02:35:00 -06:00
|
|
|
EraseRegionsVisitor {
|
2017-08-07 00:54:09 -05:00
|
|
|
tcx,
|
2017-07-11 16:30:30 -05:00
|
|
|
in_validation_statement: false,
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 12:25:17 -06:00
|
|
|
}
|
|
|
|
|
2016-02-05 02:35:00 -06:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
|
2017-10-26 18:53:31 -05:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
|
2017-07-11 16:30:30 -05:00
|
|
|
if !self.in_validation_statement {
|
2017-08-04 03:25:13 -05:00
|
|
|
*ty = self.tcx.erase_regions(ty);
|
2017-07-11 16:30:30 -05:00
|
|
|
}
|
|
|
|
self.super_ty(ty);
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
|
|
|
|
2017-08-04 03:25:13 -05:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
|
|
|
|
*region = self.tcx.types.re_erased;
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
2017-03-20 09:22:24 -05:00
|
|
|
|
2017-08-04 03:25:13 -05:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
|
|
|
|
*constant = self.tcx.erase_regions(constant);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) {
|
|
|
|
*substs = self.tcx.erase_regions(substs);
|
2017-03-20 09:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_closure_substs(&mut self,
|
2017-08-04 03:25:13 -05:00
|
|
|
substs: &mut ty::ClosureSubsts<'tcx>,
|
2017-07-18 21:31:38 -05:00
|
|
|
_: Location) {
|
2017-03-20 09:22:24 -05:00
|
|
|
*substs = self.tcx.erase_regions(substs);
|
|
|
|
}
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 06:38:42 -06:00
|
|
|
|
|
|
|
fn visit_statement(&mut self,
|
2017-06-27 10:34:07 -05:00
|
|
|
block: BasicBlock,
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 06:38:42 -06:00
|
|
|
statement: &mut Statement<'tcx>,
|
2017-06-27 10:34:07 -05:00
|
|
|
location: Location) {
|
2017-07-31 17:46:36 -05:00
|
|
|
// Do NOT delete EndRegion if validation statements are emitted.
|
|
|
|
// Validation needs EndRegion.
|
|
|
|
if self.tcx.sess.opts.debugging_opts.mir_emit_validate == 0 {
|
2017-07-22 01:18:34 -05:00
|
|
|
if let StatementKind::EndRegion(_) = statement.kind {
|
|
|
|
statement.kind = StatementKind::Nop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:30:30 -05:00
|
|
|
self.in_validation_statement = match statement.kind {
|
|
|
|
StatementKind::Validate(..) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2017-06-27 10:34:07 -05:00
|
|
|
self.super_statement(block, statement, location);
|
2017-07-11 16:30:30 -05:00
|
|
|
self.in_validation_statement = false;
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 06:38:42 -06:00
|
|
|
}
|
2015-11-16 11:41:16 -06:00
|
|
|
}
|
2016-02-26 10:05:50 -06:00
|
|
|
|
|
|
|
pub struct EraseRegions;
|
|
|
|
|
2017-04-25 17:23:33 -05:00
|
|
|
impl MirPass for EraseRegions {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
_: MirSource,
|
|
|
|
mir: &mut Mir<'tcx>) {
|
2016-02-26 10:05:50 -06:00
|
|
|
EraseRegionsVisitor::new(tcx).visit_mir(mir);
|
|
|
|
}
|
|
|
|
}
|