about summary refs log tree commit diff
path: root/src/librustc
diff options
context:
space:
mode:
authorPaul Daniel Faria <Nashenas88@users.noreply.github.com>2019-10-26 01:41:17 -0400
committerPaul Daniel Faria <Nashenas88@users.noreply.github.com>2019-12-02 08:35:08 -0500
commitfc6b58d0a809b4994cd1a633ccb04e89234ff0b8 (patch)
tree61c0f5dd280467c5f8e3e16c527711732bf682d4 /src/librustc
parent38c0887c769b227c827c76d80fa76fc5e4493516 (diff)
downloadrust-fc6b58d0a809b4994cd1a633ccb04e89234ff0b8.tar.gz
rust-fc6b58d0a809b4994cd1a633ccb04e89234ff0b8.zip
Simplify BodyCache impl and fix all remaining type errors in librustc_mir (lifetime errors still exist)
Diffstat (limited to 'src/librustc')
-rw-r--r--src/librustc/arena.rs8
-rw-r--r--src/librustc/mir/cache.rs184
-rw-r--r--src/librustc/mir/mod.rs4
-rw-r--r--src/librustc/mir/visit.rs4
-rw-r--r--src/librustc/query/mod.rs26
-rw-r--r--src/librustc/ty/context.rs14
-rw-r--r--src/librustc/ty/mod.rs10
7 files changed, 113 insertions, 137 deletions
diff --git a/src/librustc/arena.rs b/src/librustc/arena.rs
index 1d6a3420ed9..364a35f1b6f 100644
--- a/src/librustc/arena.rs
+++ b/src/librustc/arena.rs
@@ -23,17 +23,17 @@ macro_rules! arena_types {
             [] generics: rustc::ty::Generics,
             [] trait_def: rustc::ty::TraitDef,
             [] adt_def: rustc::ty::AdtDef,
-            [] steal_mir: rustc::ty::steal::Steal<rustc::mir::Body<$tcx>>,
-            [] mir: rustc::mir::Body<$tcx>,
+            [] steal_mir: rustc::ty::steal::Steal<rustc::mir::BodyCache<$tcx>>,
+            [] mir: rustc::mir::BodyCache<$tcx>,
             [] steal_promoted: rustc::ty::steal::Steal<
                 rustc_index::vec::IndexVec<
                     rustc::mir::Promoted,
-                    rustc::mir::Body<$tcx>
+                    rustc::mir::BodyCache<$tcx>
                 >
             >,
             [] promoted: rustc_index::vec::IndexVec<
                 rustc::mir::Promoted,
-                rustc::mir::Body<$tcx>
+                rustc::mir::BodyCache<$tcx>
             >,
             [] tables: rustc::ty::TypeckTables<$tcx>,
             [] const_allocs: rustc::mir::interpret::Allocation,
diff --git a/src/librustc/mir/cache.rs b/src/librustc/mir/cache.rs
index b6682470606..8958a31b51c 100644
--- a/src/librustc/mir/cache.rs
+++ b/src/librustc/mir/cache.rs
@@ -1,7 +1,7 @@
 use rustc_index::vec::IndexVec;
-//use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
-//use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
-//use crate::ich::StableHashingContext;
+use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
+use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
+use crate::ich::StableHashingContext;
 use crate::mir::{BasicBlock, BasicBlockData, Body, LocalDecls, Location, Successors};
 use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
 use rustc_data_structures::graph::dominators::{dominators, Dominators};
@@ -14,23 +14,23 @@ pub struct Cache {
     predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>,
 }
 
-//impl<'tcx, T> rustc_serialize::Encodable for Cache<'tcx, T> {
-//    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
-//        Encodable::encode(&(), s)
-//    }
-//}
-//
-//impl<'tcx, T> rustc_serialize::Decodable for Cache<'tcx, T> {
-//    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
-//        Decodable::decode(d).map(|_v: ()| Self::new())
-//    }
-//}
-//
-//impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for Cache<'tcx, T> {
-//    fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
-//        // Do nothing.
-//    }
-//}
+impl rustc_serialize::Encodable for Cache {
+    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+        Encodable::encode(&(), s)
+    }
+}
+
+impl rustc_serialize::Decodable for Cache {
+    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
+        Decodable::decode(d).map(|_v: ()| Self::new())
+    }
+}
+
+impl<'a> HashStable<StableHashingContext<'a>> for Cache {
+    fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
+        // Do nothing.
+    }
+}
 
 macro_rules! get_predecessors {
     (mut $self:ident, $block:expr, $body:expr) => {
@@ -98,13 +98,13 @@ impl Cache {
 
     #[inline]
     /// This will recompute the predecessors cache if it is not available
-    pub fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
+    fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
         self.ensure_predecessors(body);
         self.predecessors.as_ref().unwrap()
     }
 
     #[inline]
-    pub fn predecessors_for(&mut self, bb: BasicBlock, body: &Body<'_>) -> &[BasicBlock] {
+    fn predecessors_for(&mut self, bb: BasicBlock, body: &Body<'_>) -> &[BasicBlock] {
         &self.predecessors(body)[bb]
     }
 
@@ -136,55 +136,58 @@ impl Cache {
     }
 }
 
-pub struct BodyCache<T> {
+#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
+pub struct BodyCache<'tcx> {
     cache: Cache,
-    body: T,
+    body: Body<'tcx>,
 }
 
-impl<T> BodyCache<T> {
-    pub fn new(body: T) -> Self {
+impl BodyCache<'tcx> {
+    pub fn new(body: Body<'tcx>) -> Self {
         Self {
             cache: Cache::new(),
-            body
+            body,
         }
     }
 }
 
-impl<'a, 'tcx> BodyCache<&'a Body<'tcx>> {
-    #[inline]
-    pub fn predecessors_for(&mut self, bb: BasicBlock) -> &[BasicBlock] {
-        self.cache.predecessors_for(bb, self.body)
+impl BodyCache<'tcx> {
+    pub fn ensure_predecessors(&mut self) {
+        self.cache.ensure_predecessors(&self.body);
     }
 
-    #[inline]
-    pub fn body(&self) -> &'a Body<'tcx> {
-        self.body
+    pub fn predecessors(&mut self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
+        self.cache.predecessors(&self.body)
     }
 
-    #[inline]
-    pub fn read_only(mut self) -> ReadOnlyBodyCache<'a, 'tcx> {
-        self.cache.ensure_predecessors(self.body);
+    pub fn read_only(&self) -> ReadOnlyBodyCache<'_, '_> {
+        assert!(self.cache.predecessors.is_some(), "");
         ReadOnlyBodyCache {
-            cache: self.cache,
-            body: self.body,
+            cache: &self.cache,
+            body: &self.body,
         }
     }
 
-    #[inline]
-    pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
-        &self.body.basic_blocks
+    pub fn body(&self) -> &Body<'tcx> {
+        &self.body
     }
-}
 
-impl<'a, 'tcx> Deref for BodyCache<&'a Body<'tcx>> {
-    type Target = Body<'tcx>;
+    pub fn body_mut(&mut self) -> &mut Body<'tcx> {
+        &mut self.body
+    }
 
-    fn deref(&self) -> &Self::Target {
-        self.body
+    pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
+        self.cache.basic_blocks_mut(&mut self.body)
+    }
+
+    pub fn basic_blocks_and_local_decls_mut(
+        &mut self
+    ) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
+        self.cache.basic_blocks_and_local_decls_mut(&mut self.body)
     }
 }
 
-impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
+impl<'tcx> Index<BasicBlock> for BodyCache<'tcx> {
     type Output = BasicBlockData<'tcx>;
 
     #[inline]
@@ -193,69 +196,29 @@ impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
     }
 }
 
-impl<'a, 'tcx> BodyCache<&'a mut Body<'tcx>> {
-    #[inline]
-    pub fn body(&self) -> &Body<'tcx> {
-        self.body
-    }
-
-    #[inline]
-    pub fn body_mut(&mut self) -> &mut Body<'tcx> {
-        self.body
-    }
-
-    #[inline]
-    pub fn read_only(mut self) -> ReadOnlyBodyCache<'a, 'tcx> {
-        self.cache.ensure_predecessors(self.body);
-        ReadOnlyBodyCache {
-            cache: self.cache,
-            body: self.body,
-        }
-    }
-
-    #[inline]
-    pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
-        &self.body.basic_blocks
-    }
-
-    #[inline]
-    pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
-        self.cache.basic_blocks_mut(&mut self.body)
+impl<'tcx> IndexMut<BasicBlock> for BodyCache<'tcx> {
+    fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
+        &mut self.basic_blocks_mut()[index]
     }
 }
 
-impl<'a, 'tcx> Deref for BodyCache<&'a mut Body<'tcx>> {
+impl<'tcx> Deref for BodyCache<'tcx> {
     type Target = Body<'tcx>;
 
     fn deref(&self) -> &Self::Target {
-        self.body
-    }
-}
-
-impl<'a, 'tcx> DerefMut for BodyCache<&'a mut Body<'tcx>> {
-    fn deref_mut(&mut self) -> &mut Body<'tcx> {
-        self.body
-    }
-}
-
-impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
-    type Output = BasicBlockData<'tcx>;
-
-    #[inline]
-    fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
-        &self.body[index]
+        &self.body
     }
 }
 
-impl<'a, 'tcx> IndexMut<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
-    fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
-        self.cache.invalidate_predecessors();
-        &mut self.body.basic_blocks[index]
+impl<'tcx> DerefMut for BodyCache<'tcx> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.body
     }
 }
 
+#[derive(Copy, Clone, Debug)]
 pub struct ReadOnlyBodyCache<'a, 'tcx> {
-    cache: Cache,
+    cache: &'a Cache,
     body: &'a Body<'tcx>,
 }
 
@@ -289,13 +252,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
     pub fn dominators(&self) -> Dominators<BasicBlock> {
         dominators(self)
     }
-
-    pub fn to_owned(self) -> BodyCache<&'a Body<'tcx>> {
-        BodyCache {
-            cache: self.cache,
-            body: self.body,
-        }
-    }
 }
 
 impl graph::DirectedGraph for ReadOnlyBodyCache<'a, 'tcx> {
@@ -358,4 +314,20 @@ impl Index<BasicBlock> for ReadOnlyBodyCache<'a, 'tcx> {
     fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
         &self.body[index]
     }
-}
\ No newline at end of file
+}
+
+CloneTypeFoldableAndLiftImpls! {
+    Cache,
+}
+
+impl_stable_hash_for!(struct BodyCache<'tcx> {
+    cache,
+    body,
+});
+
+BraceStructTypeFoldableImpl! {
+    impl<'tcx> TypeFoldable<'tcx> for BodyCache<'tcx> {
+        cache,
+        body
+    }
+}
diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs
index 57d396ae933..b6d1c78cc4f 100644
--- a/src/librustc/mir/mod.rs
+++ b/src/librustc/mir/mod.rs
@@ -107,7 +107,7 @@ pub struct Body<'tcx> {
     pub yield_ty: Option<Ty<'tcx>>,
 
     /// Generator drop glue.
-    pub generator_drop: Option<Box<Body<'tcx>>>,
+    pub generator_drop: Option<Box<BodyCache<'tcx>>>,
 
     /// The layout of a generator. Produced by the state transformation.
     pub generator_layout: Option<GeneratorLayout<'tcx>>,
@@ -2600,7 +2600,7 @@ impl Location {
     pub fn is_predecessor_of<'tcx>(
         &self,
         other: Location,
-        body_cache: &ReadOnlyBodyCache<'_, 'tcx>
+        body_cache: ReadOnlyBodyCache<'_, 'tcx>
     ) -> bool {
         // If we are in the same block as the other location and are an earlier statement
         // then we are a predecessor of `other`.
diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs
index c464247c4b4..68694f1b717 100644
--- a/src/librustc/mir/visit.rs
+++ b/src/librustc/mir/visit.rs
@@ -67,10 +67,10 @@ use syntax_pos::Span;
 
 macro_rules! body_cache_type {
     (mut $a:lifetime, $tcx:lifetime) => {
-        &mut BodyCache<& $a mut Body<$tcx>>
+        &mut BodyCache<$tcx>
     };
     ($a:lifetime, $tcx:lifetime) => {
-        &ReadOnlyBodyCache<$a, $tcx>
+        ReadOnlyBodyCache<$a, $tcx>
     };
 }
 
diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs
index d715ddb1b81..cdfdcee5823 100644
--- a/src/librustc/query/mod.rs
+++ b/src/librustc/query/mod.rs
@@ -106,42 +106,46 @@ rustc_queries! {
 
         /// Fetch the MIR for a given `DefId` right after it's built - this includes
         /// unreachable code.
-        query mir_built(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {}
+        query mir_built(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {}
 
         /// Fetch the MIR for a given `DefId` up till the point where it is
         /// ready for const evaluation.
         ///
         /// See the README for the `mir` module for details.
-        query mir_const(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {
+        query mir_const(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {
             no_hash
         }
 
         query mir_validated(_: DefId) ->
             (
-                &'tcx Steal<mir::Body<'tcx>>,
-                &'tcx Steal<IndexVec<mir::Promoted, mir::Body<'tcx>>>
+                &'tcx Steal<mir::BodyCache<'tcx>>,
+                &'tcx Steal<IndexVec<mir::Promoted, mir::BodyCache<'tcx>>>
             ) {
             no_hash
         }
 
         /// MIR after our optimization passes have run. This is MIR that is ready
         /// for codegen. This is also the only query that can fetch non-local MIR, at present.
-        query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
+        query optimized_mir(key: DefId) -> &'tcx mir::BodyCache<'tcx> {
             cache_on_disk_if { key.is_local() }
             load_cached(tcx, id) {
-                let mir: Option<crate::mir::Body<'tcx>> = tcx.queries.on_disk_cache
-                                                            .try_load_query_result(tcx, id);
-                mir.map(|x| &*tcx.arena.alloc(x))
+                let mir: Option<crate::mir::BodyCache<'tcx>>
+                    = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
+                mir.map(|x| {
+                    let cache = tcx.arena.alloc(x);
+                    cache.ensure_predecessors();
+                    &*cache
+                })
             }
         }
 
-        query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> {
+        query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyCache<'tcx>> {
             cache_on_disk_if { key.is_local() }
             load_cached(tcx, id) {
                 let promoted: Option<
                     rustc_index::vec::IndexVec<
                         crate::mir::Promoted,
-                        crate::mir::Body<'tcx>
+                        crate::mir::BodyCache<'tcx>
                     >> = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
                 promoted.map(|p| &*tcx.arena.alloc(p))
             }
@@ -502,7 +506,7 @@ rustc_queries! {
         /// in the case of closures, this will be redirected to the enclosing function.
         query region_scope_tree(_: DefId) -> &'tcx region::ScopeTree {}
 
-        query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::Body<'tcx> {
+        query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyCache<'tcx> {
             no_force
             desc { |tcx| "generating MIR shim for `{}`", tcx.def_path_str(key.def_id()) }
         }
diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs
index 776ae7dc141..07d86a5f86a 100644
--- a/src/librustc/ty/context.rs
+++ b/src/librustc/ty/context.rs
@@ -22,7 +22,7 @@ use crate::middle::cstore::EncodedMetadata;
 use crate::middle::lang_items;
 use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
 use crate::middle::stability;
-use crate::mir::{Body, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
+use crate::mir::{BodyCache, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
 use crate::mir::interpret::{ConstValue, Allocation, Scalar};
 use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef, Subst};
 use crate::ty::ReprOptions;
@@ -1083,17 +1083,17 @@ impl<'tcx> TyCtxt<'tcx> {
         &self.hir_map
     }
 
-    pub fn alloc_steal_mir(self, mir: Body<'tcx>) -> &'tcx Steal<Body<'tcx>> {
-        self.arena.alloc(Steal::new(mir))
+    pub fn alloc_steal_mir(self, mir_cache: BodyCache<'tcx>) -> &'tcx Steal<BodyCache<'tcx>> {
+        self.arena.alloc(Steal::new(mir_cache))
     }
 
-    pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, Body<'tcx>>) ->
-        &'tcx Steal<IndexVec<Promoted, Body<'tcx>>> {
+    pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
+        &'tcx Steal<IndexVec<Promoted, BodyCache<'tcx>>> {
         self.arena.alloc(Steal::new(promoted))
     }
 
-    pub fn intern_promoted(self, promoted: IndexVec<Promoted, Body<'tcx>>) ->
-        &'tcx IndexVec<Promoted, Body<'tcx>> {
+    pub fn intern_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
+        &'tcx IndexVec<Promoted, BodyCache<'tcx>> {
         self.arena.alloc(promoted)
     }
 
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index 8ccfc467f4a..f8feff0def9 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -18,7 +18,7 @@ use crate::infer::canonical::Canonical;
 use crate::middle::cstore::CrateStoreDyn;
 use crate::middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
 use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
-use crate::mir::Body;
+use crate::mir::ReadOnlyBodyCache;
 use crate::mir::interpret::{GlobalId, ErrorHandled};
 use crate::mir::GeneratorLayout;
 use crate::session::CrateDisambiguator;
@@ -2985,10 +2985,10 @@ impl<'tcx> TyCtxt<'tcx> {
     }
 
     /// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
-    pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
+    pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> ReadOnlyBodyCache<'tcx, 'tcx> {
         match instance {
             ty::InstanceDef::Item(did) => {
-                self.optimized_mir(did)
+                self.optimized_mir(did).read_only()
             }
             ty::InstanceDef::VtableShim(..) |
             ty::InstanceDef::ReifyShim(..) |
@@ -2998,7 +2998,7 @@ impl<'tcx> TyCtxt<'tcx> {
             ty::InstanceDef::ClosureOnceShim { .. } |
             ty::InstanceDef::DropGlue(..) |
             ty::InstanceDef::CloneShim(..) => {
-                self.mir_shims(instance)
+                self.mir_shims(instance).read_only()
             }
         }
     }
@@ -3023,7 +3023,7 @@ impl<'tcx> TyCtxt<'tcx> {
     }
 
     pub fn generator_layout(self, def_id: DefId) -> &'tcx GeneratorLayout<'tcx> {
-        self.optimized_mir(def_id).generator_layout.as_ref().unwrap()
+        self.optimized_mir(def_id).body().generator_layout.as_ref().unwrap()
     }
 
     /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.