Skip to content

Commit 595d161

Browse files
committed
Remove BodyCache.body and rely on Deref as much as possible for ReadOnlyBodyCache
1 parent c42bdb8 commit 595d161

30 files changed

+89
-87
lines changed

src/librustc/mir/cache.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_index::vec::IndexVec;
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
44
use crate::ich::StableHashingContext;
5-
use crate::mir::{BasicBlock, BasicBlockData, Body, LocalDecls, Location, Successors};
5+
use crate::mir::{BasicBlock, BasicBlockData, Body, HasLocalDecls, LocalDecls, Location, Successors};
66
use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
77
use rustc_data_structures::graph::dominators::{dominators, Dominators};
88
use std::iter;
@@ -181,14 +181,6 @@ impl BodyCache<'tcx> {
181181
ReadOnlyBodyCache::new(&self.cache, &self.body)
182182
}
183183

184-
pub fn body(&self) -> &Body<'tcx> {
185-
&self.body
186-
}
187-
188-
pub fn body_mut(&mut self) -> &mut Body<'tcx> {
189-
&mut self.body
190-
}
191-
192184
pub fn cache(&self) -> &Cache { &self.cache }
193185

194186
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
@@ -231,6 +223,12 @@ impl<'tcx> DerefMut for BodyCache<'tcx> {
231223
}
232224
}
233225

226+
impl<'tcx> HasLocalDecls<'tcx> for BodyCache<'tcx> {
227+
fn local_decls(&self) -> &LocalDecls<'tcx> {
228+
&self.body.local_decls
229+
}
230+
}
231+
234232
#[derive(Copy, Clone, Debug)]
235233
pub struct ReadOnlyBodyCache<'a, 'tcx> {
236234
cache: &'a Cache,
@@ -349,6 +347,12 @@ impl Index<BasicBlock> for ReadOnlyBodyCache<'a, 'tcx> {
349347
}
350348
}
351349

350+
impl<'a, 'tcx> HasLocalDecls<'tcx> for ReadOnlyBodyCache<'a, 'tcx> {
351+
fn local_decls(&self) -> &LocalDecls<'tcx> {
352+
&self.body.local_decls
353+
}
354+
}
355+
352356
CloneTypeFoldableAndLiftImpls! {
353357
Cache,
354358
}

src/librustc/mir/visit.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,10 @@ macro_rules! make_mir_visitor {
254254

255255
fn super_body(
256256
&mut self,
257-
body_cache: body_cache_type!($($mutability)? '_, 'tcx)
257+
$($mutability)? body_cache: body_cache_type!($($mutability)? '_, 'tcx)
258258
) {
259-
macro_rules! body {
260-
(mut) => (body_cache.body_mut());
261-
() => (body_cache.body());
262-
}
263-
let span = body_cache.body().span;
264-
if let Some(yield_ty) = &$($mutability)? body!($($mutability)?).yield_ty {
259+
let span = body_cache.span;
260+
if let Some(yield_ty) = &$($mutability)? body_cache.yield_ty {
265261
self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
266262
span,
267263
scope: OUTERMOST_SOURCE_SCOPE,
@@ -279,7 +275,7 @@ macro_rules! make_mir_visitor {
279275
self.visit_basic_block_data(bb, data);
280276
}
281277

282-
let body = body!($($mutability)?);
278+
let body: & $($mutability)? Body<'_> = & $($mutability)? body_cache;
283279
for scope in &$($mutability)? body.source_scopes {
284280
self.visit_source_scope_data(scope);
285281
}

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ impl<'tcx> TyCtxt<'tcx> {
30233023
}
30243024

30253025
pub fn generator_layout(self, def_id: DefId) -> &'tcx GeneratorLayout<'tcx> {
3026-
self.optimized_mir(def_id).body().generator_layout.as_ref().unwrap()
3026+
self.optimized_mir(def_id).generator_layout.as_ref().unwrap()
30273027
}
30283028

30293029
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.

src/librustc_codegen_ssa/mir/analyze.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
131131
};
132132
if is_consume {
133133
let base_ty =
134-
mir::Place::ty_from(place_ref.base, proj_base, self.fx.mir.body(), cx.tcx());
134+
mir::Place::ty_from(place_ref.base, proj_base, &self.fx.mir, cx.tcx());
135135
let base_ty = self.fx.monomorphize(&base_ty);
136136

137137
// ZSTs don't require any actual memory access.

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
324324
target: mir::BasicBlock,
325325
unwind: Option<mir::BasicBlock>,
326326
) {
327-
let ty = location.ty(self.mir.body(), bx.tcx()).ty;
327+
let ty = location.ty(&self.mir, bx.tcx()).ty;
328328
let ty = self.monomorphize(&ty);
329329
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
330330

@@ -510,7 +510,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
510510

511511
let extra_args = &args[sig.inputs().len()..];
512512
let extra_args = extra_args.iter().map(|op_arg| {
513-
let op_ty = op_arg.ty(self.mir.body(), bx.tcx());
513+
let op_ty = op_arg.ty(&self.mir, bx.tcx());
514514
self.monomorphize(&op_ty)
515515
}).collect::<Vec<_>>();
516516

@@ -569,7 +569,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
569569
// a NOP
570570
let target = destination.as_ref().unwrap().1;
571571
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
572-
helper.funclet_br(self, &mut bx, destination.as_ref().unwrap().1)
572+
helper.funclet_br(self, &mut bx, target)
573573
}
574574
return;
575575
}
@@ -791,7 +791,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
791791
bb: mir::BasicBlock,
792792
) {
793793
let mut bx = self.build_block(bb);
794-
let data = &self.mir.body()[bb];
794+
let mir = self.mir;
795+
let data = &mir[bb];
795796

796797
debug!("codegen_block({:?}={:?})", bb, data);
797798

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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();
159+
let mir_body: &Body<'_> = &mir;
160160
let mut fx = FunctionCx {
161161
instance,
162162
mir,

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
594594
let place_ty = mir::Place::ty_from(
595595
place_ref.base,
596596
place_ref.projection,
597-
self.mir.body(),
598-
tcx);
597+
&self.mir,
598+
tcx,
599+
);
599600
self.monomorphize(&place_ty.ty)
600601
}
601602
}

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
460460
}
461461

462462
mir::Rvalue::Discriminant(ref place) => {
463-
let discr_ty = rvalue.ty(self.mir.body(), bx.tcx());
463+
let discr_ty = rvalue.ty(&self.mir, bx.tcx());
464464
let discr = self.codegen_place(&mut bx, &place.as_ref())
465465
.codegen_get_discr(&mut bx, discr_ty);
466466
(bx, OperandRef {
@@ -513,7 +513,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
513513
mir::Rvalue::Aggregate(..) => {
514514
// According to `rvalue_creates_operand`, only ZST
515515
// aggregate rvalues are allowed to be operands.
516-
let ty = rvalue.ty(self.mir.body(), self.cx.tcx());
516+
let ty = rvalue.ty(&self.mir, self.cx.tcx());
517517
let operand = OperandRef::new_zst(
518518
&mut bx,
519519
self.cx.layout_of(self.monomorphize(&ty)),
@@ -710,7 +710,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
710710
true,
711711
mir::Rvalue::Repeat(..) |
712712
mir::Rvalue::Aggregate(..) => {
713-
let ty = rvalue.ty(self.mir.body(), self.cx.tcx());
713+
let ty = rvalue.ty(&self.mir, self.cx.tcx());
714714
let ty = self.monomorphize(&ty);
715715
self.cx.spanned_layout_of(ty, span).is_zst()
716716
}

src/librustc_mir/borrow_check/borrow_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> BorrowSet<'tcx> {
130130
) -> Self {
131131
let mut visitor = GatherBorrows {
132132
tcx,
133-
body: body_cache.body(),
133+
body: &body_cache,
134134
idx_vec: IndexVec::new(),
135135
location_map: Default::default(),
136136
activation_map: Default::default(),

src/librustc_mir/borrow_check/conflict_errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
208208
let ty = Place::ty_from(
209209
used_place.base,
210210
used_place.projection,
211-
self.body_cache.body(),
211+
&self.body_cache,
212212
self.infcx.tcx
213213
).ty;
214214
let needs_note = match ty.kind {
@@ -225,7 +225,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
225225
let mpi = self.move_data.moves[move_out_indices[0]].path;
226226
let place = &self.move_data.move_paths[mpi].place;
227227

228-
let ty = place.ty(self.body_cache.body(), self.infcx.tcx).ty;
228+
let ty = place.ty(&self.body_cache, self.infcx.tcx).ty;
229229
let opt_name =
230230
self.describe_place_with_options(place.as_ref(), IncludingDowncast(true));
231231
let note_msg = match opt_name {
@@ -625,7 +625,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
625625
let ty = Place::ty_from(
626626
place_base,
627627
place_projection,
628-
self.body_cache.body(),
628+
&self.body_cache,
629629
self.infcx.tcx
630630
).ty;
631631
ty.ty_adt_def().filter(|adt| adt.is_union()).map(|_| ty)
@@ -1635,7 +1635,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16351635
Place::ty_from(
16361636
&place.base,
16371637
proj_base,
1638-
self.body_cache.body(),
1638+
&self.body_cache,
16391639
tcx
16401640
).ty.is_box(),
16411641
"Drop of value behind a reference or raw pointer"
@@ -1648,7 +1648,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16481648
let base_ty = Place::ty_from(
16491649
&place.base,
16501650
proj_base,
1651-
self.body_cache.body(),
1651+
&self.body_cache,
16521652
tcx
16531653
).ty;
16541654
match base_ty.kind {

0 commit comments

Comments
 (0)