2017-08-11 20:34:14 +02:00
|
|
|
//! This pass erases all early-bound regions from the types occurring in the MIR.
|
2018-05-08 16:10:16 +03:00
|
|
|
//! We want to do this once just before codegen, so codegen does not have to take
|
2015-11-16 18:41:16 +01:00
|
|
|
//! care erasing regions all over the place.
|
2019-02-08 14:53:55 +01:00
|
|
|
//! N.B., we do _not_ erase regions of statements that are relevant for
|
|
|
|
//! "types-as-contracts"-validation, namely, `AcquireValid` and `ReleaseValid`.
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2019-02-09 22:11:53 +08:00
|
|
|
use rustc::ty::subst::SubstsRef;
|
2017-08-04 11:25:13 +03:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2017-10-26 19:53:31 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, TyContext};
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::transform::{MirPass, MirSource};
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2016-02-05 09:35:00 +01:00
|
|
|
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
|
|
|
|
2016-02-05 09:35:00 +01:00
|
|
|
impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
|
2016-05-03 05:23:22 +03:00
|
|
|
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
|
2016-02-05 09:35:00 +01:00
|
|
|
EraseRegionsVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 20:25:17 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 09:35:00 +01:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
|
2017-10-26 19:53:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
|
2018-10-24 11:47:17 +02:00
|
|
|
*ty = self.tcx.erase_regions(ty);
|
2017-07-11 14:30:30 -07:00
|
|
|
self.super_ty(ty);
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
|
|
|
|
2017-08-04 11:25:13 +03:00
|
|
|
fn visit_region(&mut self, region: &mut ty::Region<'tcx>, _: Location) {
|
|
|
|
*region = self.tcx.types.re_erased;
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
2017-03-20 15:22:24 +01:00
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _: Location) {
|
2017-08-04 11:25:13 +03:00
|
|
|
*constant = self.tcx.erase_regions(constant);
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:11:53 +08:00
|
|
|
fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, _: Location) {
|
2017-08-04 11:25:13 +03:00
|
|
|
*substs = self.tcx.erase_regions(substs);
|
2017-03-20 15:22:24 +01:00
|
|
|
}
|
|
|
|
|
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 13:38:42 +01:00
|
|
|
fn visit_statement(&mut self,
|
2017-06-27 18:34:07 +03: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 13:38:42 +01:00
|
|
|
statement: &mut Statement<'tcx>,
|
2017-06-27 18:34:07 +03:00
|
|
|
location: Location) {
|
|
|
|
self.super_statement(block, statement, location);
|
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 13:38:42 +01:00
|
|
|
}
|
2015-11-16 18:41:16 +01:00
|
|
|
}
|
2016-02-26 18:05:50 +02:00
|
|
|
|
|
|
|
pub struct EraseRegions;
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
impl MirPass for EraseRegions {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2019-02-09 12:19:04 +01:00
|
|
|
_: MirSource<'tcx>,
|
2017-04-25 18:23:33 -04:00
|
|
|
mir: &mut Mir<'tcx>) {
|
2016-02-26 18:05:50 +02:00
|
|
|
EraseRegionsVisitor::new(tcx).visit_mir(mir);
|
|
|
|
}
|
|
|
|
}
|