Skip to content

Commit 40e367f

Browse files
authored
Rollup merge of rust-lang#66991 - Nashenas88:body_cache_cleanup, r=eddyb
Cleanup BodyCache After this PR: - `BodyCache` is renamed to `BodyAndCache` - `ReadOnlyBodyCache` is renamed to `ReadOnlyBodyAndCache` - `ReadOnlyBodyAndCache::body` fn is removed and all calls to it are replaced by a deref (possible due to fix of its `Deref` imp in rust-lang#65947) cc @eddyb @oli-obk
2 parents 1f029e7 + a5e144b commit 40e367f

Some content is hidden

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

58 files changed

+226
-224
lines changed

src/librustc/arena.rs

+4-4
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::BodyCache<$tcx>>,
27-
[] mir: rustc::mir::BodyCache<$tcx>,
26+
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::BodyAndCache<$tcx>>,
27+
[] mir: rustc::mir::BodyAndCache<$tcx>,
2828
[] steal_promoted: rustc::ty::steal::Steal<
2929
rustc_index::vec::IndexVec<
3030
rustc::mir::Promoted,
31-
rustc::mir::BodyCache<$tcx>
31+
rustc::mir::BodyAndCache<$tcx>
3232
>
3333
>,
3434
[] promoted: rustc_index::vec::IndexVec<
3535
rustc::mir::Promoted,
36-
rustc::mir::BodyCache<$tcx>
36+
rustc::mir::BodyAndCache<$tcx>
3737
>,
3838
[] tables: rustc::ty::TypeckTables<$tcx>,
3939
[] const_allocs: rustc::mir::interpret::Allocation,

src/librustc/mir/cache.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ impl Cache {
115115
}
116116

117117
#[derive(Clone, Debug, HashStable, RustcEncodable, RustcDecodable, TypeFoldable)]
118-
pub struct BodyCache<'tcx> {
119-
cache: Cache,
118+
pub struct BodyAndCache<'tcx> {
120119
body: Body<'tcx>,
120+
cache: Cache,
121121
}
122122

123-
impl BodyCache<'tcx> {
123+
impl BodyAndCache<'tcx> {
124124
pub fn new(body: Body<'tcx>) -> Self {
125125
Self {
126-
cache: Cache::new(),
127126
body,
127+
cache: Cache::new(),
128128
}
129129
}
130130
}
@@ -139,7 +139,7 @@ macro_rules! read_only {
139139
};
140140
}
141141

142-
impl BodyCache<'tcx> {
142+
impl BodyAndCache<'tcx> {
143143
pub fn ensure_predecessors(&mut self) {
144144
self.cache.ensure_predecessors(&self.body);
145145
}
@@ -148,8 +148,8 @@ impl BodyCache<'tcx> {
148148
self.cache.predecessors(&self.body)
149149
}
150150

151-
pub fn unwrap_read_only(&self) -> ReadOnlyBodyCache<'_, 'tcx> {
152-
ReadOnlyBodyCache::new(&self.cache, &self.body)
151+
pub fn unwrap_read_only(&self) -> ReadOnlyBodyAndCache<'_, 'tcx> {
152+
ReadOnlyBodyAndCache::new(&self.body, &self.cache)
153153
}
154154

155155
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
@@ -163,48 +163,48 @@ impl BodyCache<'tcx> {
163163
}
164164
}
165165

166-
impl<'tcx> Index<BasicBlock> for BodyCache<'tcx> {
166+
impl<'tcx> Index<BasicBlock> for BodyAndCache<'tcx> {
167167
type Output = BasicBlockData<'tcx>;
168168

169169
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
170170
&self.body[index]
171171
}
172172
}
173173

174-
impl<'tcx> IndexMut<BasicBlock> for BodyCache<'tcx> {
174+
impl<'tcx> IndexMut<BasicBlock> for BodyAndCache<'tcx> {
175175
fn index_mut(&mut self, index: BasicBlock) -> &mut Self::Output {
176176
&mut self.basic_blocks_mut()[index]
177177
}
178178
}
179179

180-
impl<'tcx> Deref for BodyCache<'tcx> {
180+
impl<'tcx> Deref for BodyAndCache<'tcx> {
181181
type Target = Body<'tcx>;
182182

183183
fn deref(&self) -> &Self::Target {
184184
&self.body
185185
}
186186
}
187187

188-
impl<'tcx> DerefMut for BodyCache<'tcx> {
188+
impl<'tcx> DerefMut for BodyAndCache<'tcx> {
189189
fn deref_mut(&mut self) -> &mut Self::Target {
190190
&mut self.body
191191
}
192192
}
193193

194194
#[derive(Copy, Clone, Debug)]
195-
pub struct ReadOnlyBodyCache<'a, 'tcx> {
196-
cache: &'a Cache,
195+
pub struct ReadOnlyBodyAndCache<'a, 'tcx> {
197196
body: &'a Body<'tcx>,
197+
cache: &'a Cache,
198198
}
199199

200-
impl ReadOnlyBodyCache<'a, 'tcx> {
201-
fn new(cache: &'a Cache, body: &'a Body<'tcx>) -> Self {
200+
impl ReadOnlyBodyAndCache<'a, 'tcx> {
201+
fn new(body: &'a Body<'tcx>, cache: &'a Cache) -> Self {
202202
assert!(
203203
cache.predecessors.is_some(),
204-
"Cannot construct ReadOnlyBodyCache without computed predecessors");
204+
"Cannot construct ReadOnlyBodyAndCache without computed predecessors");
205205
Self {
206-
cache,
207206
body,
207+
cache,
208208
}
209209
}
210210

@@ -220,10 +220,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
220220
self.cache.unwrap_predecessor_locations(loc, self.body)
221221
}
222222

223-
pub fn body(&self) -> &'a Body<'tcx> {
224-
self.body
225-
}
226-
227223
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
228224
&self.body.basic_blocks
229225
}
@@ -233,16 +229,16 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
233229
}
234230
}
235231

236-
impl graph::DirectedGraph for ReadOnlyBodyCache<'a, 'tcx> {
232+
impl graph::DirectedGraph for ReadOnlyBodyAndCache<'a, 'tcx> {
237233
type Node = BasicBlock;
238234
}
239235

240-
impl graph::GraphPredecessors<'graph> for ReadOnlyBodyCache<'a, 'tcx> {
236+
impl graph::GraphPredecessors<'graph> for ReadOnlyBodyAndCache<'a, 'tcx> {
241237
type Item = BasicBlock;
242238
type Iter = IntoIter<BasicBlock>;
243239
}
244240

245-
impl graph::WithPredecessors for ReadOnlyBodyCache<'a, 'tcx> {
241+
impl graph::WithPredecessors for ReadOnlyBodyAndCache<'a, 'tcx> {
246242
fn predecessors(
247243
&self,
248244
node: Self::Node,
@@ -251,19 +247,19 @@ impl graph::WithPredecessors for ReadOnlyBodyCache<'a, 'tcx> {
251247
}
252248
}
253249

254-
impl graph::WithNumNodes for ReadOnlyBodyCache<'a, 'tcx> {
250+
impl graph::WithNumNodes for ReadOnlyBodyAndCache<'a, 'tcx> {
255251
fn num_nodes(&self) -> usize {
256252
self.body.num_nodes()
257253
}
258254
}
259255

260-
impl graph::WithStartNode for ReadOnlyBodyCache<'a, 'tcx> {
256+
impl graph::WithStartNode for ReadOnlyBodyAndCache<'a, 'tcx> {
261257
fn start_node(&self) -> Self::Node {
262258
self.body.start_node()
263259
}
264260
}
265261

266-
impl graph::WithSuccessors for ReadOnlyBodyCache<'a, 'tcx> {
262+
impl graph::WithSuccessors for ReadOnlyBodyAndCache<'a, 'tcx> {
267263
fn successors(
268264
&self,
269265
node: Self::Node,
@@ -272,13 +268,13 @@ impl graph::WithSuccessors for ReadOnlyBodyCache<'a, 'tcx> {
272268
}
273269
}
274270

275-
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for ReadOnlyBodyCache<'a, 'tcx> {
271+
impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for ReadOnlyBodyAndCache<'a, 'tcx> {
276272
type Item = BasicBlock;
277273
type Iter = iter::Cloned<Successors<'b>>;
278274
}
279275

280276

281-
impl Deref for ReadOnlyBodyCache<'a, 'tcx> {
277+
impl Deref for ReadOnlyBodyAndCache<'a, 'tcx> {
282278
type Target = &'a Body<'tcx>;
283279

284280
fn deref(&self) -> &Self::Target {

src/librustc/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use syntax::symbol::Symbol;
3838
use syntax_pos::{Span, DUMMY_SP};
3939

4040
pub use crate::mir::interpret::AssertMessage;
41-
pub use crate::mir::cache::{BodyCache, ReadOnlyBodyCache};
41+
pub use crate::mir::cache::{BodyAndCache, ReadOnlyBodyAndCache};
4242
pub use crate::read_only;
4343

4444
mod cache;
@@ -108,7 +108,7 @@ pub struct Body<'tcx> {
108108
pub yield_ty: Option<Ty<'tcx>>,
109109

110110
/// Generator drop glue.
111-
pub generator_drop: Option<Box<BodyCache<'tcx>>>,
111+
pub generator_drop: Option<Box<BodyAndCache<'tcx>>>,
112112

113113
/// The layout of a generator. Produced by the state transformation.
114114
pub generator_layout: Option<GeneratorLayout<'tcx>>,
@@ -2597,7 +2597,7 @@ impl Location {
25972597
pub fn is_predecessor_of<'tcx>(
25982598
&self,
25992599
other: Location,
2600-
body: ReadOnlyBodyCache<'_, 'tcx>
2600+
body: ReadOnlyBodyAndCache<'_, 'tcx>
26012601
) -> bool {
26022602
// If we are in the same block as the other location and are an earlier statement
26032603
// then we are a predecessor of `other`.

src/librustc/mir/visit.rs

+2-2
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<$tcx>
70+
&mut BodyAndCache<$tcx>
7171
};
7272
($a:lifetime, $tcx:lifetime) => {
73-
ReadOnlyBodyCache<$a, $tcx>
73+
ReadOnlyBodyAndCache<$a, $tcx>
7474
};
7575
}
7676

src/librustc/query/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,30 @@ 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::BodyCache<'tcx>> {}
109+
query mir_built(_: DefId) -> &'tcx Steal<mir::BodyAndCache<'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::BodyCache<'tcx>> {
115+
query mir_const(_: DefId) -> &'tcx Steal<mir::BodyAndCache<'tcx>> {
116116
no_hash
117117
}
118118

119119
query mir_validated(_: DefId) ->
120120
(
121-
&'tcx Steal<mir::BodyCache<'tcx>>,
122-
&'tcx Steal<IndexVec<mir::Promoted, mir::BodyCache<'tcx>>>
121+
&'tcx Steal<mir::BodyAndCache<'tcx>>,
122+
&'tcx Steal<IndexVec<mir::Promoted, mir::BodyAndCache<'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::BodyCache<'tcx> {
129+
query optimized_mir(key: DefId) -> &'tcx mir::BodyAndCache<'tcx> {
130130
cache_on_disk_if { key.is_local() }
131131
load_cached(tcx, id) {
132-
let mir: Option<crate::mir::BodyCache<'tcx>>
132+
let mir: Option<crate::mir::BodyAndCache<'tcx>>
133133
= tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
134134
mir.map(|x| {
135135
let cache = tcx.arena.alloc(x);
@@ -139,13 +139,13 @@ rustc_queries! {
139139
}
140140
}
141141

142-
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyCache<'tcx>> {
142+
query promoted_mir(key: DefId) -> &'tcx IndexVec<mir::Promoted, mir::BodyAndCache<'tcx>> {
143143
cache_on_disk_if { key.is_local() }
144144
load_cached(tcx, id) {
145145
let promoted: Option<
146146
rustc_index::vec::IndexVec<
147147
crate::mir::Promoted,
148-
crate::mir::BodyCache<'tcx>
148+
crate::mir::BodyAndCache<'tcx>
149149
>> = tcx.queries.on_disk_cache.try_load_query_result(tcx, id);
150150
promoted.map(|p| {
151151
let cache = tcx.arena.alloc(p);
@@ -512,7 +512,7 @@ rustc_queries! {
512512
/// in the case of closures, this will be redirected to the enclosing function.
513513
query region_scope_tree(_: DefId) -> &'tcx region::ScopeTree {}
514514

515-
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyCache<'tcx> {
515+
query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::BodyAndCache<'tcx> {
516516
no_force
517517
desc { |tcx| "generating MIR shim for `{}`", tcx.def_path_str(key.def_id()) }
518518
}

src/librustc/ty/context.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::middle::cstore::EncodedMetadata;
2222
use crate::middle::lang_items;
2323
use crate::middle::resolve_lifetime::{self, ObjectLifetimeDefault};
2424
use crate::middle::stability;
25-
use crate::mir::{BodyCache, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
25+
use crate::mir::{BodyAndCache, Field, interpret, Local, Place, PlaceElem, ProjectionKind, Promoted};
2626
use crate::mir::interpret::{ConstValue, Allocation, Scalar};
2727
use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef, Subst};
2828
use crate::ty::ReprOptions;
@@ -1083,17 +1083,17 @@ impl<'tcx> TyCtxt<'tcx> {
10831083
&self.hir_map
10841084
}
10851085

1086-
pub fn alloc_steal_mir(self, mir: BodyCache<'tcx>) -> &'tcx Steal<BodyCache<'tcx>> {
1086+
pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
10871087
self.arena.alloc(Steal::new(mir))
10881088
}
10891089

1090-
pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
1091-
&'tcx Steal<IndexVec<Promoted, BodyCache<'tcx>>> {
1090+
pub fn alloc_steal_promoted(self, promoted: IndexVec<Promoted, BodyAndCache<'tcx>>) ->
1091+
&'tcx Steal<IndexVec<Promoted, BodyAndCache<'tcx>>> {
10921092
self.arena.alloc(Steal::new(promoted))
10931093
}
10941094

1095-
pub fn intern_promoted(self, promoted: IndexVec<Promoted, BodyCache<'tcx>>) ->
1096-
&'tcx IndexVec<Promoted, BodyCache<'tcx>> {
1095+
pub fn intern_promoted(self, promoted: IndexVec<Promoted, BodyAndCache<'tcx>>) ->
1096+
&'tcx IndexVec<Promoted, BodyAndCache<'tcx>> {
10971097
self.arena.alloc(promoted)
10981098
}
10991099

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::infer::canonical::Canonical;
1818
use crate::middle::cstore::CrateStoreDyn;
1919
use crate::middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2020
use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
21-
use crate::mir::ReadOnlyBodyCache;
21+
use crate::mir::ReadOnlyBodyAndCache;
2222
use crate::mir::interpret::{GlobalId, ErrorHandled};
2323
use crate::mir::GeneratorLayout;
2424
use crate::session::CrateDisambiguator;
@@ -2981,7 +2981,7 @@ impl<'tcx> TyCtxt<'tcx> {
29812981
}
29822982

29832983
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
2984-
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> ReadOnlyBodyCache<'tcx, 'tcx> {
2984+
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> ReadOnlyBodyAndCache<'tcx, 'tcx> {
29852985
match instance {
29862986
ty::InstanceDef::Item(did) => {
29872987
self.optimized_mir(did).unwrap_read_only()

src/librustc_codegen_ssa/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
153153
// a loop.
154154
fn maybe_sideeffect<Bx: BuilderMethods<'a, 'tcx>>(
155155
&self,
156-
mir: mir::ReadOnlyBodyCache<'tcx, 'tcx>,
156+
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,
157157
bx: &mut Bx,
158158
targets: &[mir::BasicBlock],
159159
) {

src/librustc_codegen_ssa/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use self::operand::{OperandRef, OperandValue};
2121
pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
2222
instance: Instance<'tcx>,
2323

24-
mir: mir::ReadOnlyBodyCache<'tcx, 'tcx>,
24+
mir: mir::ReadOnlyBodyAndCache<'tcx, 'tcx>,
2525

2626
debug_context: Option<FunctionDebugContext<Bx::DIScope>>,
2727

@@ -156,7 +156,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
156156
}).collect();
157157

158158
let (landing_pads, funclets) = create_funclets(&mir, &mut bx, &cleanup_kinds, &block_bxs);
159-
let mir_body: &mir::Body<'_> = mir.body();
159+
let mir_body: &mir::Body<'_> = *mir;
160160
let mut fx = FunctionCx {
161161
instance,
162162
mir,

src/librustc_metadata/rmeta/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::fx::FxHashMap;
1818
use rustc_data_structures::svh::Svh;
1919
use rustc::dep_graph::{self, DepNodeIndex};
2020
use rustc::middle::lang_items;
21-
use rustc::mir::{self, BodyCache, interpret, Promoted};
21+
use rustc::mir::{self, BodyAndCache, interpret, Promoted};
2222
use rustc::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2323
use rustc::session::Session;
2424
use rustc::ty::{self, Ty, TyCtxt};
@@ -1079,7 +1079,7 @@ impl<'a, 'tcx> CrateMetadata {
10791079
self.root.per_def.mir.get(self, id).is_some()
10801080
}
10811081

1082-
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> BodyCache<'tcx> {
1082+
fn get_optimized_mir(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> BodyAndCache<'tcx> {
10831083
let mut cache = self.root.per_def.mir.get(self, id)
10841084
.filter(|_| !self.is_proc_macro(id))
10851085
.unwrap_or_else(|| {
@@ -1094,7 +1094,7 @@ impl<'a, 'tcx> CrateMetadata {
10941094
&self,
10951095
tcx: TyCtxt<'tcx>,
10961096
id: DefIndex,
1097-
) -> IndexVec<Promoted, BodyCache<'tcx>> {
1097+
) -> IndexVec<Promoted, BodyAndCache<'tcx>> {
10981098
let mut cache = self.root.per_def.promoted_mir.get(self, id)
10991099
.filter(|_| !self.is_proc_macro(id))
11001100
.unwrap_or_else(|| {

0 commit comments

Comments
 (0)