Skip to content

Commit fc6b58d

Browse files
committed
Simplify BodyCache impl and fix all remaining type errors in librustc_mir (lifetime errors still exist)
1 parent 38c0887 commit fc6b58d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+588
-612
lines changed

src/librustc/arena.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ macro_rules! arena_types {
2323
[] generics: rustc::ty::Generics,
2424
[] trait_def: rustc::ty::TraitDef,
2525
[] adt_def: rustc::ty::AdtDef,
26-
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::Body<$tcx>>,
27-
[] mir: rustc::mir::Body<$tcx>,
26+
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::BodyCache<$tcx>>,
27+
[] mir: rustc::mir::BodyCache<$tcx>,
2828
[] steal_promoted: rustc::ty::steal::Steal<
2929
rustc_index::vec::IndexVec<
3030
rustc::mir::Promoted,
31-
rustc::mir::Body<$tcx>
31+
rustc::mir::BodyCache<$tcx>
3232
>
3333
>,
3434
[] promoted: rustc_index::vec::IndexVec<
3535
rustc::mir::Promoted,
36-
rustc::mir::Body<$tcx>
36+
rustc::mir::BodyCache<$tcx>
3737
>,
3838
[] tables: rustc::ty::TypeckTables<$tcx>,
3939
[] const_allocs: rustc::mir::interpret::Allocation,

src/librustc/mir/cache.rs

Lines changed: 78 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_index::vec::IndexVec;
2-
//use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3-
//use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
4-
//use crate::ich::StableHashingContext;
2+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3+
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
4+
use crate::ich::StableHashingContext;
55
use crate::mir::{BasicBlock, BasicBlockData, Body, LocalDecls, Location, Successors};
66
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
77
use rustc_data_structures::graph::dominators::{dominators, Dominators};
@@ -14,23 +14,23 @@ pub struct Cache {
1414
predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>,
1515
}
1616

17-
//impl<'tcx, T> rustc_serialize::Encodable for Cache<'tcx, T> {
18-
// fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
19-
// Encodable::encode(&(), s)
20-
// }
21-
//}
22-
//
23-
//impl<'tcx, T> rustc_serialize::Decodable for Cache<'tcx, T> {
24-
// fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
25-
// Decodable::decode(d).map(|_v: ()| Self::new())
26-
// }
27-
//}
28-
//
29-
//impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for Cache<'tcx, T> {
30-
// fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
31-
// // Do nothing.
32-
// }
33-
//}
17+
impl rustc_serialize::Encodable for Cache {
18+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
19+
Encodable::encode(&(), s)
20+
}
21+
}
22+
23+
impl rustc_serialize::Decodable for Cache {
24+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
25+
Decodable::decode(d).map(|_v: ()| Self::new())
26+
}
27+
}
28+
29+
impl<'a> HashStable<StableHashingContext<'a>> for Cache {
30+
fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
31+
// Do nothing.
32+
}
33+
}
3434

3535
macro_rules! get_predecessors {
3636
(mut $self:ident, $block:expr, $body:expr) => {
@@ -98,13 +98,13 @@ impl Cache {
9898

9999
#[inline]
100100
/// This will recompute the predecessors cache if it is not available
101-
pub fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
101+
fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
102102
self.ensure_predecessors(body);
103103
self.predecessors.as_ref().unwrap()
104104
}
105105

106106
#[inline]
107-
pub fn predecessors_for(&mut self, bb: BasicBlock, body: &Body<'_>) -> &[BasicBlock] {
107+
fn predecessors_for(&mut self, bb: BasicBlock, body: &Body<'_>) -> &[BasicBlock] {
108108
&self.predecessors(body)[bb]
109109
}
110110

@@ -136,55 +136,58 @@ impl Cache {
136136
}
137137
}
138138

139-
pub struct BodyCache<T> {
139+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
140+
pub struct BodyCache<'tcx> {
140141
cache: Cache,
141-
body: T,
142+
body: Body<'tcx>,
142143
}
143144

144-
impl<T> BodyCache<T> {
145-
pub fn new(body: T) -> Self {
145+
impl BodyCache<'tcx> {
146+
pub fn new(body: Body<'tcx>) -> Self {
146147
Self {
147148
cache: Cache::new(),
148-
body
149+
body,
149150
}
150151
}
151152
}
152153

153-
impl<'a, 'tcx> BodyCache<&'a Body<'tcx>> {
154-
#[inline]
155-
pub fn predecessors_for(&mut self, bb: BasicBlock) -> &[BasicBlock] {
156-
self.cache.predecessors_for(bb, self.body)
154+
impl BodyCache<'tcx> {
155+
pub fn ensure_predecessors(&mut self) {
156+
self.cache.ensure_predecessors(&self.body);
157157
}
158158

159-
#[inline]
160-
pub fn body(&self) -> &'a Body<'tcx> {
161-
self.body
159+
pub fn predecessors(&mut self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
160+
self.cache.predecessors(&self.body)
162161
}
163162

164-
#[inline]
165-
pub fn read_only(mut self) -> ReadOnlyBodyCache<'a, 'tcx> {
166-
self.cache.ensure_predecessors(self.body);
163+
pub fn read_only(&self) -> ReadOnlyBodyCache<'_, '_> {
164+
assert!(self.cache.predecessors.is_some(), "");
167165
ReadOnlyBodyCache {
168-
cache: self.cache,
169-
body: self.body,
166+
cache: &self.cache,
167+
body: &self.body,
170168
}
171169
}
172170

173-
#[inline]
174-
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
175-
&self.body.basic_blocks
171+
pub fn body(&self) -> &Body<'tcx> {
172+
&self.body
176173
}
177-
}
178174

179-
impl<'a, 'tcx> Deref for BodyCache<&'a Body<'tcx>> {
180-
type Target = Body<'tcx>;
175+
pub fn body_mut(&mut self) -> &mut Body<'tcx> {
176+
&mut self.body
177+
}
181178

182-
fn deref(&self) -> &Self::Target {
183-
self.body
179+
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
180+
self.cache.basic_blocks_mut(&mut self.body)
181+
}
182+
183+
pub fn basic_blocks_and_local_decls_mut(
184+
&mut self
185+
) -> (&mut IndexVec<BasicBlock, BasicBlockData<'tcx>>, &mut LocalDecls<'tcx>) {
186+
self.cache.basic_blocks_and_local_decls_mut(&mut self.body)
184187
}
185188
}
186189

187-
impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
190+
impl<'tcx> Index<BasicBlock> for BodyCache<'tcx> {
188191
type Output = BasicBlockData<'tcx>;
189192

190193
#[inline]
@@ -193,69 +196,29 @@ impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
193196
}
194197
}
195198

196-
impl<'a, 'tcx> BodyCache<&'a mut Body<'tcx>> {
197-
#[inline]
198-
pub fn body(&self) -> &Body<'tcx> {
199-
self.body
200-
}
201-
202-
#[inline]
203-
pub fn body_mut(&mut self) -> &mut Body<'tcx> {
204-
self.body
205-
}
206-
207-
#[inline]
208-
pub fn read_only(mut self) -> ReadOnlyBodyCache<'a, 'tcx> {
209-
self.cache.ensure_predecessors(self.body);
210-
ReadOnlyBodyCache {
211-
cache: self.cache,
212-
body: self.body,
213-
}
214-
}
215-
216-
#[inline]
217-
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
218-
&self.body.basic_blocks
219-
}
220-
221-
#[inline]
222-
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
223-
self.cache.basic_blocks_mut(&mut self.body)
199+
impl<'tcx> IndexMut<BasicBlock> for BodyCache<'tcx> {
200+
fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
201+
&mut self.basic_blocks_mut()[index]
224202
}
225203
}
226204

227-
impl<'a, 'tcx> Deref for BodyCache<&'a mut Body<'tcx>> {
205+
impl<'tcx> Deref for BodyCache<'tcx> {
228206
type Target = Body<'tcx>;
229207

230208
fn deref(&self) -> &Self::Target {
231-
self.body
232-
}
233-
}
234-
235-
impl<'a, 'tcx> DerefMut for BodyCache<&'a mut Body<'tcx>> {
236-
fn deref_mut(&mut self) -> &mut Body<'tcx> {
237-
self.body
238-
}
239-
}
240-
241-
impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
242-
type Output = BasicBlockData<'tcx>;
243-
244-
#[inline]
245-
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
246-
&self.body[index]
209+
&self.body
247210
}
248211
}
249212

250-
impl<'a, 'tcx> IndexMut<BasicBlock> for BodyCache<&'a mut Body<'tcx>> {
251-
fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
252-
self.cache.invalidate_predecessors();
253-
&mut self.body.basic_blocks[index]
213+
impl<'tcx> DerefMut for BodyCache<'tcx> {
214+
fn deref_mut(&mut self) -> &mut Self::Target {
215+
&mut self.body
254216
}
255217
}
256218

219+
#[derive(Copy, Clone, Debug)]
257220
pub struct ReadOnlyBodyCache<'a, 'tcx> {
258-
cache: Cache,
221+
cache: &'a Cache,
259222
body: &'a Body<'tcx>,
260223
}
261224

@@ -289,13 +252,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
289252
pub fn dominators(&self) -> Dominators<BasicBlock> {
290253
dominators(self)
291254
}
292-
293-
pub fn to_owned(self) -> BodyCache<&'a Body<'tcx>> {
294-
BodyCache {
295-
cache: self.cache,
296-
body: self.body,
297-
}
298-
}
299255
}
300256

301257
impl graph::DirectedGraph for ReadOnlyBodyCache<'a, 'tcx> {
@@ -358,4 +314,20 @@ impl Index<BasicBlock> for ReadOnlyBodyCache<'a, 'tcx> {
358314
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
359315
&self.body[index]
360316
}
361-
}
317+
}
318+
319+
CloneTypeFoldableAndLiftImpls! {
320+
Cache,
321+
}
322+
323+
impl_stable_hash_for!(struct BodyCache<'tcx> {
324+
cache,
325+
body,
326+
});
327+
328+
BraceStructTypeFoldableImpl! {
329+
impl<'tcx> TypeFoldable<'tcx> for BodyCache<'tcx> {
330+
cache,
331+
body
332+
}
333+
}

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub struct Body<'tcx> {
107107
pub yield_ty: Option<Ty<'tcx>>,
108108

109109
/// Generator drop glue.
110-
pub generator_drop: Option<Box<Body<'tcx>>>,
110+
pub generator_drop: Option<Box<BodyCache<'tcx>>>,
111111

112112
/// The layout of a generator. Produced by the state transformation.
113113
pub generator_layout: Option<GeneratorLayout<'tcx>>,
@@ -2600,7 +2600,7 @@ impl Location {
26002600
pub fn is_predecessor_of<'tcx>(
26012601
&self,
26022602
other: Location,
2603-
body_cache: &ReadOnlyBodyCache<'_, 'tcx>
2603+
body_cache: ReadOnlyBodyCache<'_, 'tcx>
26042604
) -> bool {
26052605
// If we are in the same block as the other location and are an earlier statement
26062606
// then we are a predecessor of `other`.

src/librustc/mir/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ use syntax_pos::Span;
6767

6868
macro_rules! body_cache_type {
6969
(mut $a:lifetime, $tcx:lifetime) => {
70-
&mut BodyCache<& $a mut Body<$tcx>>
70+
&mut BodyCache<$tcx>
7171
};
7272
($a:lifetime, $tcx:lifetime) => {
73-
&ReadOnlyBodyCache<$a, $tcx>
73+
ReadOnlyBodyCache<$a, $tcx>
7474
};
7575
}
7676

src/librustc/query/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,42 +106,46 @@ rustc_queries! {
106106

107107
/// Fetch the MIR for a given `DefId` right after it's built - this includes
108108
/// unreachable code.
109-
query mir_built(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {}
109+
query mir_built(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {}
110110

111111
/// Fetch the MIR for a given `DefId` up till the point where it is
112112
/// ready for const evaluation.
113113
///
114114
/// See the README for the `mir` module for details.
115-
query mir_const(_: DefId) -> &'tcx Steal<mir::Body<'tcx>> {
115+
query mir_const(_: DefId) -> &'tcx Steal<mir::BodyCache<'tcx>> {
116116
no_hash
117117
}
118118

119119
query mir_validated(_: DefId) ->
120120
(
121-
&'tcx Steal<mir::Body<'tcx>>,
122-
&'tcx Steal<IndexVec<mir::Promoted, mir::Body<'tcx>>>
121+
&'tcx Steal<mir::BodyCache<'tcx>>,
122+
&'tcx Steal<IndexVec<mir::Promoted, mir::BodyCache<'tcx>>>
123123
) {
124124
no_hash
125125
}
126126

127127
/// MIR after our optimization passes have run. This is MIR that is ready
128128
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
129-
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
129+
query optimized_mir(key: DefId) -> &'tcx mir::BodyCache<'tcx> {
130130
cache_on_disk_if { key.is_local() }
131131
load_cached(tcx, id) {
132-
let mir: Option<crate::mir::Body<'tcx>> = tcx.queries.on_disk_cache
133-
.try_load_query_result(tcx, id);
134-
mir.map(|x| &*tcx.arena.alloc(x))
132+
let mir: Option<crate::mir::BodyCache<'tcx>>
133+
= tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
134+
mir.map(|x| {
135+
let cache = tcx.arena.alloc(x);
136+
cache.ensure_predecessors();
137+
&*cache
138+
})
135139
}
136140
}
137141

138-
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> {
142+
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyCache<'tcx>> {
139143
cache_on_disk_if { key.is_local() }
140144
load_cached(tcx, id) {
141145
let promoted: Option<
142146
rustc_index::vec::IndexVec<
143147
crate::mir::Promoted,
144-
crate::mir::Body<'tcx>
148+
crate::mir::BodyCache<'tcx>
145149
>> = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
146150
promoted.map(|p| &*tcx.arena.alloc(p))
147151
}
@@ -502,7 +506,7 @@ rustc_queries! {
502506
/// in the case of closures, this will be redirected to the enclosing function.
503507
query region_scope_tree(_: DefId) -> &'tcx region::ScopeTree {}
504508

505-
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::Body<'tcx> {
509+
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyCache<'tcx> {
506510
no_force
507511
desc { |tcx| "generating MIR shim for `{}`", tcx.def_path_str(key.def_id()) }
508512
}

0 commit comments

Comments
 (0)