Skip to content

Commit 6a9b008

Browse files
committed
Simplify accessing locals in a MirPatch
1 parent 38d4b9e commit 6a9b008

File tree

10 files changed

+33
-16
lines changed

10 files changed

+33
-16
lines changed

compiler/rustc_middle/src/mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,20 @@ pub use self::pretty::{
7575
pub type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
7676

7777
pub trait HasLocalDecls<'tcx> {
78-
fn local_decls(&self) -> &LocalDecls<'tcx>;
78+
fn local_decl(&self, local: Local) -> &LocalDecl<'tcx>;
7979
}
8080

8181
impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
8282
#[inline]
83-
fn local_decls(&self) -> &LocalDecls<'tcx> {
84-
self
83+
fn local_decl(&self, local: Local) -> &LocalDecl<'tcx> {
84+
&self[local]
8585
}
8686
}
8787

8888
impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
8989
#[inline]
90-
fn local_decls(&self) -> &LocalDecls<'tcx> {
91-
&self.local_decls
90+
fn local_decl(&self, local: Local) -> &LocalDecl<'tcx> {
91+
&self.local_decls[local]
9292
}
9393
}
9494

compiler/rustc_middle/src/mir/patch.rs

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub struct MirPatch<'tcx> {
1717
}
1818

1919
impl<'tcx> MirPatch<'tcx> {
20+
pub fn local_decl<'a>(&'a self, body: &'a Body<'tcx>, local: Local) -> &'a LocalDecl<'tcx> {
21+
body.local_decls
22+
.get(local)
23+
.unwrap_or_else(|| &self.new_locals[local.as_usize() - body.local_decls.len()])
24+
}
25+
2026
pub fn new(body: &Body<'tcx>) -> Self {
2127
let mut result = MirPatch {
2228
patch_map: IndexVec::from_elem(None, &body.basic_blocks),

compiler/rustc_middle/src/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'tcx> Place<'tcx> {
127127
{
128128
projection
129129
.iter()
130-
.fold(PlaceTy::from_ty(local_decls.local_decls()[local].ty), |place_ty, &elem| {
130+
.fold(PlaceTy::from_ty(local_decls.local_decl(local).ty), |place_ty, &elem| {
131131
place_ty.projection_ty(tcx, elem)
132132
})
133133
}

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Unwind {
9595
}
9696
}
9797

98-
pub trait DropElaborator<'a, 'tcx>: fmt::Debug {
98+
pub trait DropElaborator<'a, 'tcx>: fmt::Debug + HasLocalDecls<'tcx> {
9999
/// The type representing paths that can be moved out of.
100100
///
101101
/// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
@@ -195,7 +195,7 @@ where
195195
'tcx: 'b,
196196
{
197197
fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> {
198-
place.ty(self.elaborator.body(), self.tcx()).ty
198+
place.ty(&*self.elaborator, self.tcx()).ty
199199
}
200200

201201
fn tcx(&self) -> TyCtxt<'tcx> {

compiler/rustc_mir_dataflow/src/impls/borrowed_locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeBorrowedLocals {
2424

2525
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
2626
// bottom = unborrowed
27-
BitSet::new_empty(body.local_decls().len())
27+
BitSet::new_empty(body.local_decls.len())
2828
}
2929

3030
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {

compiler/rustc_mir_transform/src/const_goto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
8484
let target_bb_terminator = target_bb.terminator();
8585
let (discr, targets) = target_bb_terminator.kind.as_switch()?;
8686
if discr.place() == Some(*place) {
87-
let switch_ty = place.ty(self.body.local_decls(), self.tcx).ty;
87+
let switch_ty = place.ty(self.body, self.tcx).ty;
8888
// We now know that the Switch matches on the const place, and it is statementless
8989
// Now find which value in the Switch matches the const value.
9090
let const_value =

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
131131
Operand::Copy(x) => Operand::Copy(*x),
132132
Operand::Constant(x) => Operand::Constant(x.clone()),
133133
};
134-
let parent_ty = parent_op.ty(body.local_decls(), tcx);
134+
let parent_ty = parent_op.ty(body, tcx);
135135
let statements_before = bbs[parent].statements.len();
136136
let parent_end = Location { block: parent, statement_index: statements_before };
137137

@@ -268,7 +268,7 @@ fn may_hoist<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, place: Place<'tcx>) ->
268268
// `otherwise` branch in `BBC, BBD` in the input to our transformation, which would
269269
// have invalidated the data when computing `discriminant(P)`
270270
// So dereferencing here is correct.
271-
ProjectionElem::Deref => match place.ty(body.local_decls(), tcx).ty.kind() {
271+
ProjectionElem::Deref => match place.ty(body, tcx).ty.kind() {
272272
ty::Ref(..) => {}
273273
_ => return false,
274274
},
@@ -317,7 +317,7 @@ fn evaluate_candidate<'tcx>(
317317
} = &bbs[parent].terminator().kind else {
318318
return None
319319
};
320-
let parent_ty = parent_discr.ty(body.local_decls(), tcx);
320+
let parent_ty = parent_discr.ty(body, tcx);
321321
let parent_dest = {
322322
let poss = targets.otherwise();
323323
// If the fallthrough on the parent is trivially unreachable, we can let the
@@ -338,7 +338,7 @@ fn evaluate_candidate<'tcx>(
338338
} = &child_terminator.kind else {
339339
return None
340340
};
341-
let child_ty = child_discr.ty(body.local_decls(), tcx);
341+
let child_ty = child_discr.ty(body, tcx);
342342
if child_ty != parent_ty {
343343
return None;
344344
}

compiler/rustc_mir_transform/src/elaborate_drops.rs

+6
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ impl fmt::Debug for Elaborator<'_, '_, '_> {
156156
}
157157
}
158158

159+
impl<'tcx> HasLocalDecls<'tcx> for Elaborator<'_, '_, 'tcx> {
160+
fn local_decl(&self, local: Local) -> &LocalDecl<'tcx> {
161+
self.ctxt.patch.local_decl(self.ctxt.body, local)
162+
}
163+
}
164+
159165
impl<'a, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, '_, 'tcx> {
160166
type Path = MovePathIndex;
161167

compiler/rustc_mir_transform/src/shim.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
190190
BorrowKind::Mut { allow_two_phase_borrow: false },
191191
tcx.mk_place_deref(dropee_ptr),
192192
);
193-
let ref_ty = reborrow.ty(body.local_decls(), tcx);
193+
let ref_ty = reborrow.ty(&body, tcx);
194194
dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into();
195195
let new_statements = [
196196
StatementKind::Assign(Box::new((dropee_ptr, reborrow))),
@@ -270,6 +270,11 @@ impl fmt::Debug for DropShimElaborator<'_, '_> {
270270
Ok(())
271271
}
272272
}
273+
impl<'tcx> HasLocalDecls<'tcx> for DropShimElaborator<'_, 'tcx> {
274+
fn local_decl(&self, local: Local) -> &LocalDecl<'tcx> {
275+
self.patch.local_decl(self.body, local)
276+
}
277+
}
273278

274279
impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
275280
type Path = ();

compiler/rustc_mir_transform/src/sroa.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'tcx> MirPass<'tcx> for ScalarReplacementOfAggregates {
3232
fn escaping_locals(body: &Body<'_>) -> BitSet<Local> {
3333
let mut set = BitSet::new_empty(body.local_decls.len());
3434
set.insert_range(RETURN_PLACE..=Local::from_usize(body.arg_count));
35-
for (local, decl) in body.local_decls().iter_enumerated() {
35+
for (local, decl) in body.local_decls.iter_enumerated() {
3636
if decl.ty.is_union() || decl.ty.is_enum() {
3737
set.insert(local);
3838
}

0 commit comments

Comments
 (0)