use hir::map::DefPathData;
use mir::{Mir, Promoted};
use ty::TyCtxt;
-use std::cell::{Ref, RefCell};
+use std::cell::Ref;
use std::rc::Rc;
use syntax::ast::NodeId;
fn pass_num(&self) -> MirPassIndex;
fn source(&self) -> MirSource;
fn read_previous_mir(&self) -> Ref<'tcx, Mir<'tcx>>;
- fn steal_previous_mir(&self) -> &'tcx RefCell<Mir<'tcx>>;
+ fn steal_previous_mir(&self) -> Mir<'tcx>;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
default_name::<Self>()
}
- fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> &'tcx RefCell<Mir<'tcx>>;
+ fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx>;
}
/// A streamlined trait that you can implement to create a pass; the
MirPass::name(self)
}
- fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> &'tcx RefCell<Mir<'tcx>> {
+ fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx> {
let tcx = mir_cx.tcx();
let source = mir_cx.source();
- let mir = mir_cx.steal_previous_mir();
- MirPass::run_pass(self, tcx, source, &mut mir.borrow_mut());
+ let mut mir = mir_cx.steal_previous_mir();
+ MirPass::run_pass(self, tcx, source, &mut mir);
let item_id = source.item_id();
- for (promoted_index, promoted_mir) in mir.borrow_mut().promoted.iter_enumerated_mut() {
+ for (promoted_index, promoted_mir) in mir.promoted.iter_enumerated_mut() {
let promoted_source = MirSource::Promoted(item_id, promoted_index);
MirPass::run_pass(self, tcx, promoted_source, promoted_mir);
}
use ty::layout::{Layout, TargetDataLayout};
use ty::inhabitedness::DefIdForest;
use ty::maps;
+use ty::steal::Steal;
use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
use util::nodemap::{FxHashMap, FxHashSet};
use rustc_data_structures::accumulate_vec::AccumulateVec;
generics: TypedArena<ty::Generics>,
trait_def: TypedArena<ty::TraitDef>,
adt_def: TypedArena<ty::AdtDef>,
- mir: TypedArena<RefCell<Mir<'tcx>>>,
+ steal_mir: TypedArena<Steal<Mir<'tcx>>>,
+ mir: TypedArena<Mir<'tcx>>,
tables: TypedArena<ty::TypeckTables<'tcx>>,
}
generics: TypedArena::new(),
trait_def: TypedArena::new(),
adt_def: TypedArena::new(),
+ steal_mir: TypedArena::new(),
mir: TypedArena::new(),
tables: TypedArena::new(),
}
self.global_arenas.generics.alloc(generics)
}
- pub fn alloc_mir(self, mir: Mir<'gcx>) -> &'gcx RefCell<Mir<'gcx>> {
- self.global_arenas.mir.alloc(RefCell::new(mir))
+ pub fn alloc_steal_mir(self, mir: Mir<'gcx>) -> &'gcx Steal<Mir<'gcx>> {
+ self.global_arenas.steal_mir.alloc(Steal::new(mir))
+ }
+
+ pub fn alloc_mir(self, mir: Mir<'gcx>) -> &'gcx Mir<'gcx> {
+ self.global_arenas.mir.alloc(mir)
}
pub fn alloc_tables(self, tables: ty::TypeckTables<'gcx>) -> &'gcx ty::TypeckTables<'gcx> {
use session::CompileResult;
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
use ty::item_path;
+use ty::steal::Steal;
use ty::subst::Substs;
use util::nodemap::{DefIdSet, NodeSet};
use syntax_pos::{Span, DUMMY_SP};
use syntax::symbol::Symbol;
-trait Key {
+trait Key: Clone {
fn map_crate(&self) -> CrateNum;
fn default_span(&self, tcx: TyCtxt) -> Span;
}
impl<'tcx> QueryDescription for queries::mir_suite<'tcx> {
fn describe(_: TyCtxt, (suite, _): (MirSuite, DefId)) -> String {
- format!("MIR passes #{}.*", suite.0)
+ format!("MIR suite #{}.*", suite.0)
}
}
impl<'tcx> QueryDescription for queries::mir_pass<'tcx> {
- fn describe(_: TyCtxt, (pass_set, pass_num, _): (MirSuite, MirPassIndex, DefId)) -> String {
- format!("MIR pass #{}.{}", pass_set.0, pass_num.0)
+ fn describe(_: TyCtxt, (suite, pass_num, _): (MirSuite, MirPassIndex, DefId)) -> String {
+ format!("MIR pass #{}.{}", suite.0, pass_num.0)
}
}
/// Performs the initial MIR construction. You almost certainly do not
/// want to use this query, because its output is intended to be stolen
/// immediately by the MIR passes below. Consider `optimized_mir` instead.
- [] mir_build: Mir(DefId) -> &'tcx RefCell<mir::Mir<'tcx>>,
+ [] mir_build: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
/// Fetch the MIR for a given def-id after the given set of passes has ben
/// applied to it. This is mostly an "intermediate" query. Normally, you would
/// prefer to use `optimized_mir(def_id)`, which will fetch the MIR after all
/// optimizations and so forth.
- [] mir_suite: mir_suite((MirSuite, DefId)) -> &'tcx RefCell<mir::Mir<'tcx>>,
+ [] mir_suite: mir_suite((MirSuite, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
/// Fetch the MIR for a given def-id after a given pass has been executed. This is
/// **only** intended to be used by the `mir_suite` provider -- if you are using it
/// manually, you're doing it wrong.
- [] mir_pass: mir_pass((MirSuite, MirPassIndex, DefId)) -> &'tcx RefCell<mir::Mir<'tcx>>,
+ [] mir_pass: mir_pass((MirSuite, MirPassIndex, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
/// MIR after our optimization passes have run. This is MIR that is ready
/// for trans. This is also the only query that can fetch non-local MIR, at present.
- [] optimized_mir: Mir(DefId) -> &'tcx RefCell<mir::Mir<'tcx>>,
+ [] optimized_mir: Mir(DefId) -> &'tcx mir::Mir<'tcx>,
/// Records the type of each closure. The def ID is the ID of the
/// expression defining the closure.
/// fn item.
[] region_maps: RegionMaps(DefId) -> Rc<RegionMaps<'tcx>>,
- [] mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>,
+ [] mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx mir::Mir<'tcx>,
[] def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
[] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
use util::nodemap::{NodeSet, DefIdMap, FxHashMap, FxHashSet};
use serialize::{self, Encodable, Encoder};
-use std::cell::{Cell, RefCell, Ref};
+use std::cell::{Cell, RefCell};
use std::collections::BTreeMap;
use std::cmp;
use std::fmt;
pub mod maps;
pub mod outlives;
pub mod relate;
+pub mod steal;
pub mod subst;
pub mod trait_def;
pub mod walk;
}
/// Given the did of an item, returns its (optimized) MIR, borrowed immutably.
- pub fn item_mir(self, did: DefId) -> Ref<'gcx, Mir<'gcx>> {
- self.optimized_mir(did).borrow()
+ pub fn item_mir(self, did: DefId) -> &'gcx Mir<'gcx> {
+ self.optimized_mir(did)
}
/// Return the possibly-auto-generated MIR of a (DefId, Subst) pair.
pub fn instance_mir(self, instance: ty::InstanceDef<'gcx>)
- -> Ref<'gcx, Mir<'gcx>>
+ -> &'gcx Mir<'gcx>
{
match instance {
ty::InstanceDef::Item(did) => {
ty::InstanceDef::Virtual(..) |
ty::InstanceDef::ClosureOnceShim { .. } |
ty::InstanceDef::DropGlue(..) => {
- self.mir_shims(instance).borrow()
+ self.mir_shims(instance)
}
}
}
/// Given the DefId of an item, returns its MIR, borrowed immutably.
/// Returns None if there is no MIR for the DefId
- pub fn maybe_item_mir(self, did: DefId) -> Option<Ref<'gcx, Mir<'gcx>>> {
+ pub fn maybe_item_mir(self, did: DefId) -> Option<&'gcx Mir<'gcx>> {
if did.is_local() && !self.mir_keys(LOCAL_CRATE).contains(&did) {
return None;
}
--- /dev/null
+use std::cell::{Ref, RefCell};
+use std::mem;
+
+pub struct Steal<T> {
+ value: RefCell<Option<T>>
+}
+
+impl<T> Steal<T> {
+ pub fn new(value: T) -> Self {
+ Steal {
+ value: RefCell::new(Some(value))
+ }
+ }
+
+ pub fn borrow(&self) -> Ref<T> {
+ Ref::map(self.value.borrow(), |opt| match *opt {
+ None => panic!("attempted to read from stolen value"),
+ Some(ref v) => v
+ })
+ }
+
+ pub fn steal(&self) -> T {
+ let value_ref = &mut *self.value.borrow_mut();
+ let value = mem::replace(value_ref, None);
+ value.expect("attempt to read from stolen value")
+ }
+}
use rustc_back::PanicStrategy;
use std::any::Any;
-use std::mem;
use std::rc::Rc;
use syntax::ast;
let mir = tcx.alloc_mir(mir);
- // Perma-borrow MIR from extern crates to prevent mutation.
- mem::forget(mir.borrow());
-
mir
}
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
use rustc::traits::Reveal;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::maps::Providers;
+use rustc::ty::steal::Steal;
use rustc::ty::subst::Substs;
use rustc::hir;
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use syntax::ast;
use syntax_pos::Span;
-use std::cell::RefCell;
use std::mem;
use std::rc::Rc;
Rc::new(set)
}
-fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx RefCell<Mir<'tcx>> {
+fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
let id = tcx.hir.as_local_node_id(def_id).unwrap();
let unsupported = || {
span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id);
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
- tcx.alloc_mir(mir)
+ tcx.alloc_steal_mir(mir)
})
}
fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
ctor_id: ast::NodeId,
v: &'tcx hir::VariantData)
- -> &'tcx RefCell<Mir<'tcx>>
+ -> &'tcx Steal<Mir<'tcx>>
{
let span = tcx.hir.span(ctor_id);
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
- tcx.alloc_mir(mir)
+ tcx.alloc_steal_mir(mir)
})
} else {
span_bug!(span, "attempting to create MIR for non-tuple variant {:?}", v);
use syntax::ast;
use syntax_pos::Span;
-use std::cell::RefCell;
use std::fmt;
use std::iter;
-use std::mem;
use transform::{add_call_guards, no_landing_pads, simplify};
use util::elaborate_drops::{self, DropElaborator, DropStyle, DropFlagMode};
fn make_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
instance: ty::InstanceDef<'tcx>)
- -> &'tcx RefCell<Mir<'tcx>>
+ -> &'tcx Mir<'tcx>
{
debug!("make_shim({:?})", instance);
let did = instance.def_id();
debug!("make_shim({:?}) = {:?}", instance, result);
let result = tcx.alloc_mir(result);
- // Perma-borrow MIR from shims to prevent mutation.
- mem::forget(result.borrow());
result
}
//! This pass just dumps MIR at a specified point.
use std::borrow::Cow;
-use std::cell::RefCell;
use std::fmt;
use std::fs::File;
use std::io;
Cow::Borrowed(self.0)
}
- fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> &'tcx RefCell<Mir<'tcx>> {
+ fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx> {
mir_cx.steal_previous_mir()
}
}
mir: Option<&Mir<'tcx>>)
{
let tcx = mir_cx.tcx();
- let pass_set = mir_cx.pass_set();
+ let suite = mir_cx.suite();
let pass_num = mir_cx.pass_num();
- let pass = tcx.mir_passes.pass(pass_set, pass_num);
+ let pass = tcx.mir_passes.pass(suite, pass_num);
let name = &pass.name();
let source = mir_cx.source();
if mir_util::dump_enabled(tcx, name, source) {
}
};
mir_util::dump_mir(tcx,
- Some((pass_set, pass_num)),
+ Some((suite, pass_num)),
name,
&Disambiguator { is_after: mir.is_some() },
source,
use rustc::hir::def_id::DefId;
use rustc::mir::Mir;
use rustc::mir::transform::{MirCtxt, MirPassIndex, MirSuite, MirSource, MIR_OPTIMIZED};
+use rustc::ty::steal::Steal;
use rustc::ty::TyCtxt;
use rustc::ty::maps::Providers;
-use std::cell::{Ref, RefCell};
-use std::mem;
+use std::cell::Ref;
pub mod simplify_branches;
pub mod simplify;
};
}
-fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx RefCell<Mir<'tcx>> {
- let mir = tcx.mir_suite((MIR_OPTIMIZED, def_id));
-
- // "lock" the ref cell into read mode; after this point,
- // there ought to be no more changes to the MIR.
- mem::drop(mir.borrow());
-
- mir
+fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
+ let mir = tcx.mir_suite((MIR_OPTIMIZED, def_id)).steal();
+ tcx.alloc_mir(mir)
}
fn mir_suite<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
(suite, def_id): (MirSuite, DefId))
- -> &'tcx RefCell<Mir<'tcx>>
+ -> &'tcx Steal<Mir<'tcx>>
{
let passes = &tcx.mir_passes;
let len = passes.len_passes(suite);
fn mir_pass<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
(suite, pass_num, def_id): (MirSuite, MirPassIndex, DefId))
- -> &'tcx RefCell<Mir<'tcx>>
+ -> &'tcx Steal<Mir<'tcx>>
{
let passes = &tcx.mir_passes;
let pass = passes.pass(suite, pass_num);
let mir = pass.run_pass(&mir_ctxt);
for hook in passes.hooks() {
- hook.on_mir_pass(&mir_ctxt, Some(&mir.borrow()));
+ hook.on_mir_pass(&mir_ctxt, Some(&mir));
}
- mir
+ tcx.alloc_steal_mir(mir)
}
struct MirCtxtImpl<'a, 'tcx: 'a> {
}
fn read_previous_mir(&self) -> Ref<'tcx, Mir<'tcx>> {
- self.steal_previous_mir().borrow()
+ self.previous_mir().borrow()
}
- fn steal_previous_mir(&self) -> &'tcx RefCell<Mir<'tcx>> {
+ fn steal_previous_mir(&self) -> Mir<'tcx> {
+ self.previous_mir().steal()
+ }
+}
+
+impl<'a, 'tcx> MirCtxtImpl<'a, 'tcx> {
+ fn previous_mir(&self) -> &'tcx Steal<Mir<'tcx>> {
let MirSuite(suite) = self.suite;
let MirPassIndex(pass_num) = self.pass_num;
if pass_num > 0 {
use syntax::feature_gate::UnstableFeatures;
use syntax_pos::{Span, DUMMY_SP};
-use std::cell::RefCell;
use std::fmt;
use std::usize;
fn qualify_const_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId)
-> u8 {
- let mir = &tcx.mir_pass_set((MIR_CONST, def_id)).borrow();
+ let mir = &tcx.mir_suite((MIR_CONST, def_id)).borrow();
if mir.return_ty.references_error() {
return Qualif::NOT_CONST.bits();
}
pub struct QualifyAndPromoteConstants;
impl DefIdPass for QualifyAndPromoteConstants {
- fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> &'tcx RefCell<Mir<'tcx>> {
+ fn run_pass<'a, 'tcx: 'a>(&self, mir_cx: &MirCtxt<'a, 'tcx>) -> Mir<'tcx> {
let tcx = mir_cx.tcx();
match mir_cx.source() {
MirSource::Const(_) => {
}
src => {
- let mir = mir_cx.steal_previous_mir();
- self.run_pass(tcx, src, &mut mir.borrow_mut());
+ let mut mir = mir_cx.steal_previous_mir();
+ self.run_pass(tcx, src, &mut mir);
mir
}
}
} else {
match pass_num {
None => format!(".-------"),
- Some((pass_set, pass_num)) => format!(".{:03}-{:03}", pass_set.0, pass_num.0),
+ Some((suite, pass_num)) => format!(".{:03}-{:03}", suite.0, pass_num.0),
}
};