Skip to content

Commit 62b2e54

Browse files
committed
Refactor: {Lvalue,Rvalue,Operand}::ty only need the locals' types, not the full &Mir
1 parent 9475ae4 commit 62b2e54

File tree

16 files changed

+72
-69
lines changed

16 files changed

+72
-69
lines changed

src/librustc/mir/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ macro_rules! newtype_index {
6666
)
6767
}
6868

69+
/// Types for locals
70+
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
71+
6972
/// Lowered representation of a single function.
7073
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
7174
pub struct Mir<'tcx> {
@@ -90,7 +93,7 @@ pub struct Mir<'tcx> {
9093
/// The first local is the return value pointer, followed by `arg_count`
9194
/// locals for the function arguments, followed by any user-declared
9295
/// variables and temporaries.
93-
pub local_decls: IndexVec<Local, LocalDecl<'tcx>>,
96+
pub local_decls: LocalDecls<'tcx>,
9497

9598
/// Number of arguments this function takes.
9699
///

src/librustc/mir/tcx.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,31 @@ impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
121121
}
122122

123123
impl<'tcx> Lvalue<'tcx> {
124-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx> {
124+
pub fn ty<'a, 'gcx>(&self, local_decls: &LocalDecls<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx> {
125125
match *self {
126126
Lvalue::Local(index) =>
127-
LvalueTy::Ty { ty: mir.local_decls[index].ty },
127+
LvalueTy::Ty { ty: local_decls[index].ty },
128128
Lvalue::Static(ref data) =>
129129
LvalueTy::Ty { ty: data.ty },
130130
Lvalue::Projection(ref proj) =>
131-
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
131+
proj.base.ty(local_decls, tcx).projection_ty(tcx, &proj.elem),
132132
}
133133
}
134134
}
135135

136136
impl<'tcx> Rvalue<'tcx> {
137-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
137+
pub fn ty<'a, 'gcx>(&self, local_decls: &LocalDecls<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
138138
{
139139
match *self {
140-
Rvalue::Use(ref operand) => operand.ty(mir, tcx),
140+
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
141141
Rvalue::Repeat(ref operand, ref count) => {
142-
let op_ty = operand.ty(mir, tcx);
142+
let op_ty = operand.ty(local_decls, tcx);
143143
let count = count.as_u64(tcx.sess.target.uint_type);
144144
assert_eq!(count as usize as u64, count);
145145
tcx.mk_array(op_ty, count as usize)
146146
}
147147
Rvalue::Ref(reg, bk, ref lv) => {
148-
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
148+
let lv_ty = lv.ty(local_decls, tcx).to_ty(tcx);
149149
tcx.mk_ref(reg,
150150
ty::TypeAndMut {
151151
ty: lv_ty,
@@ -156,22 +156,22 @@ impl<'tcx> Rvalue<'tcx> {
156156
Rvalue::Len(..) => tcx.types.usize,
157157
Rvalue::Cast(.., ty) => ty,
158158
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
159-
let lhs_ty = lhs.ty(mir, tcx);
160-
let rhs_ty = rhs.ty(mir, tcx);
159+
let lhs_ty = lhs.ty(local_decls, tcx);
160+
let rhs_ty = rhs.ty(local_decls, tcx);
161161
op.ty(tcx, lhs_ty, rhs_ty)
162162
}
163163
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
164-
let lhs_ty = lhs.ty(mir, tcx);
165-
let rhs_ty = rhs.ty(mir, tcx);
164+
let lhs_ty = lhs.ty(local_decls, tcx);
165+
let rhs_ty = rhs.ty(local_decls, tcx);
166166
let ty = op.ty(tcx, lhs_ty, rhs_ty);
167167
tcx.intern_tup(&[ty, tcx.types.bool], false)
168168
}
169169
Rvalue::UnaryOp(UnOp::Not, ref operand) |
170170
Rvalue::UnaryOp(UnOp::Neg, ref operand) => {
171-
operand.ty(mir, tcx)
171+
operand.ty(local_decls, tcx)
172172
}
173173
Rvalue::Discriminant(ref lval) => {
174-
let ty = lval.ty(mir, tcx).to_ty(tcx);
174+
let ty = lval.ty(local_decls, tcx).to_ty(tcx);
175175
if let ty::TyAdt(adt_def, _) = ty.sty {
176176
adt_def.repr.discr_type().to_ty(tcx)
177177
} else {
@@ -189,7 +189,7 @@ impl<'tcx> Rvalue<'tcx> {
189189
}
190190
AggregateKind::Tuple => {
191191
tcx.mk_tup(
192-
ops.iter().map(|op| op.ty(mir, tcx)),
192+
ops.iter().map(|op| op.ty(local_decls, tcx)),
193193
false
194194
)
195195
}
@@ -206,9 +206,9 @@ impl<'tcx> Rvalue<'tcx> {
206206
}
207207

208208
impl<'tcx> Operand<'tcx> {
209-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
209+
pub fn ty<'a, 'gcx>(&self, local_decls: &LocalDecls<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
210210
match self {
211-
&Operand::Consume(ref l) => l.ty(mir, tcx).to_ty(tcx),
211+
&Operand::Consume(ref l) => l.ty(local_decls, tcx).to_ty(tcx),
212212
&Operand::Constant(ref c) => c.ty,
213213
}
214214
}

src/librustc_mir/dataflow/drop_flag_effects.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn move_path_children_matching<'tcx, F>(move_data: &MoveData<'tcx>,
129129
fn lvalue_contents_drop_state_cannot_differ<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
130130
mir: &Mir<'tcx>,
131131
lv: &mir::Lvalue<'tcx>) -> bool {
132-
let ty = lv.ty(mir, tcx).to_ty(tcx);
132+
let ty = lv.ty(&mir.local_decls, tcx).to_ty(tcx);
133133
match ty.sty {
134134
ty::TyArray(..) | ty::TySlice(..) | ty::TyRef(..) | ty::TyRawPtr(..) => {
135135
debug!("lvalue_contents_drop_state_cannot_differ lv: {:?} ty: {:?} refd => true",
@@ -216,7 +216,7 @@ pub(crate) fn on_all_drop_children_bits<'a, 'tcx, F>(
216216
{
217217
on_all_children_bits(tcx, mir, &ctxt.move_data, path, |child| {
218218
let lvalue = &ctxt.move_data.move_paths[path].lvalue;
219-
let ty = lvalue.ty(mir, tcx).to_ty(tcx);
219+
let ty = lvalue.ty(&mir.local_decls, tcx).to_ty(tcx);
220220
debug!("on_all_drop_children_bits({:?}, {:?} : {:?})", path, lvalue, ty);
221221

222222
if ty.needs_drop(tcx, ctxt.param_env) {
@@ -263,7 +263,7 @@ pub(crate) fn drop_flag_effects_for_location<'a, 'tcx, F>(
263263

264264
// don't move out of non-Copy things
265265
let lvalue = &move_data.move_paths[path].lvalue;
266-
let ty = lvalue.ty(mir, tcx).to_ty(tcx);
266+
let ty = lvalue.ty(&mir.local_decls, tcx).to_ty(tcx);
267267
if !ty.moves_by_default(tcx, param_env, DUMMY_SP) {
268268
continue;
269269
}

src/librustc_mir/dataflow/move_paths/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
286286
-> Result<MovePathIndex, MovePathError>
287287
{
288288
let base = try!(self.move_path_for(&proj.base));
289-
let lv_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);
289+
let lv_ty = proj.base.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx);
290290
match lv_ty.sty {
291291
// error: can't move out of borrowed content
292292
ty::TyRef(..) | ty::TyRawPtr(..) => return Err(MovePathError::IllegalMove),
@@ -504,7 +504,7 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
504504
fn gather_move(&mut self, loc: Location, lval: &Lvalue<'tcx>) {
505505
debug!("gather_move({:?}, {:?})", loc, lval);
506506

507-
let lv_ty = lval.ty(self.mir, self.tcx).to_ty(self.tcx);
507+
let lv_ty = lval.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx);
508508
if !lv_ty.moves_by_default(self.tcx, self.param_env, DUMMY_SP) {
509509
debug!("gather_move({:?}, {:?}) - {:?} is Copy. skipping", loc, lval, lv_ty);
510510
return

src/librustc_mir/transform/inline.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
250250
work_list.push(target);
251251
// If the location doesn't actually need dropping, treat it like
252252
// a regular goto.
253-
let ty = location.ty(&callee_mir, tcx).subst(tcx, callsite.substs);
253+
let ty = location.ty(&callee_mir.local_decls, tcx).subst(tcx, callsite.substs);
254254
let ty = ty.to_ty(tcx);
255255
if ty.needs_drop(tcx, param_env) {
256256
cost += CALL_PENALTY;
@@ -390,7 +390,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
390390
BorrowKind::Mut,
391391
destination.0);
392392

393-
let ty = dest.ty(caller_mir, self.tcx);
393+
let ty = dest.ty(&caller_mir.local_decls, self.tcx);
394394

395395
let temp = LocalDecl::new_temp(ty, callsite.location.span);
396396

@@ -422,7 +422,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
422422
bug!("Constant arg to \"box_free\"");
423423
};
424424

425-
let ptr_ty = args[0].ty(caller_mir, self.tcx);
425+
let ptr_ty = args[0].ty(&caller_mir.local_decls, self.tcx);
426426
vec![self.cast_box_free_arg(arg, ptr_ty, &callsite, caller_mir)]
427427
} else {
428428
// Copy the arguments if needed.
@@ -475,7 +475,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
475475
BorrowKind::Mut,
476476
arg.deref());
477477

478-
let ty = arg.ty(caller_mir, self.tcx);
478+
let ty = arg.ty(&caller_mir.local_decls, self.tcx);
479479
let ref_tmp = LocalDecl::new_temp(ty, callsite.location.span);
480480
let ref_tmp = caller_mir.local_decls.push(ref_tmp);
481481
let ref_tmp = Lvalue::Local(ref_tmp);
@@ -529,7 +529,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
529529
// Otherwise, create a temporary for the arg
530530
let arg = Rvalue::Use(a);
531531

532-
let ty = arg.ty(caller_mir, tcx);
532+
let ty = arg.ty(&caller_mir.local_decls, tcx);
533533

534534
let arg_tmp = LocalDecl::new_temp(ty, callsite.location.span);
535535
let arg_tmp = caller_mir.local_decls.push(arg_tmp);

src/librustc_mir/transform/instcombine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for OptimizationFinder<'b, 'a, 'tcx> {
8787
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
8888
if let Rvalue::Ref(_, _, Lvalue::Projection(ref projection)) = *rvalue {
8989
if let ProjectionElem::Deref = projection.elem {
90-
if projection.base.ty(self.mir, self.tcx).to_ty(self.tcx).is_region_ptr() {
90+
if projection.base.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx).is_region_ptr() {
9191
self.optimizations.and_stars.insert(location);
9292
}
9393
}

src/librustc_mir/transform/promote_consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,13 @@ pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
362362
continue;
363363
}
364364
}
365-
(statement.source_info.span, dest.ty(mir, tcx).to_ty(tcx))
365+
(statement.source_info.span, dest.ty(&mir.local_decls, tcx).to_ty(tcx))
366366
}
367367
Candidate::ShuffleIndices(bb) => {
368368
let terminator = mir[bb].terminator();
369369
let ty = match terminator.kind {
370370
TerminatorKind::Call { ref args, .. } => {
371-
args[2].ty(mir, tcx)
371+
args[2].ty(&mir.local_decls, tcx)
372372
}
373373
_ => {
374374
span_bug!(terminator.source_info.span,

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
507507
this.add(Qualif::STATIC);
508508
}
509509

510-
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
510+
let base_ty = proj.base.ty(&this.mir.local_decls, this.tcx).to_ty(this.tcx);
511511
if let ty::TyRawPtr(_) = base_ty.sty {
512512
this.add(Qualif::NOT_CONST);
513513
if this.mode != Mode::Fn {
@@ -530,7 +530,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
530530
"cannot refer to the interior of another \
531531
static, use a constant instead");
532532
}
533-
let ty = lvalue.ty(this.mir, this.tcx).to_ty(this.tcx);
533+
let ty = lvalue.ty(&this.mir.local_decls, this.tcx).to_ty(this.tcx);
534534
this.qualif.restrict(ty, this.tcx, this.param_env);
535535
}
536536

@@ -606,7 +606,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
606606
self.add(Qualif::STATIC_REF);
607607
}
608608

609-
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
609+
let ty = lvalue.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx);
610610
if kind == BorrowKind::Mut {
611611
// In theory, any zero-sized value could be borrowed
612612
// mutably without consequences. However, only &mut []
@@ -671,7 +671,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
671671
}
672672

673673
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
674-
let operand_ty = operand.ty(self.mir, self.tcx);
674+
let operand_ty = operand.ty(&self.mir.local_decls, self.tcx);
675675
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
676676
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
677677
match (cast_in, cast_out) {
@@ -689,7 +689,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
689689
}
690690

691691
Rvalue::BinaryOp(op, ref lhs, _) => {
692-
if let ty::TyRawPtr(_) = lhs.ty(self.mir, self.tcx).sty {
692+
if let ty::TyRawPtr(_) = lhs.ty(&self.mir.local_decls, self.tcx).sty {
693693
assert!(op == BinOp::Eq || op == BinOp::Ne ||
694694
op == BinOp::Le || op == BinOp::Lt ||
695695
op == BinOp::Ge || op == BinOp::Gt ||
@@ -727,7 +727,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
727727
}
728728

729729
if Some(def.did) == self.tcx.lang_items.unsafe_cell_type() {
730-
let ty = rvalue.ty(self.mir, self.tcx);
730+
let ty = rvalue.ty(&self.mir.local_decls, self.tcx);
731731
self.add_type(ty);
732732
assert!(self.qualif.intersects(Qualif::MUTABLE_INTERIOR));
733733
// Even if the value inside may not need dropping,
@@ -748,7 +748,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
748748
if let TerminatorKind::Call { ref func, ref args, ref destination, .. } = *kind {
749749
self.visit_operand(func, location);
750750

751-
let fn_ty = func.ty(self.mir, self.tcx);
751+
let fn_ty = func.ty(&self.mir.local_decls, self.tcx);
752752
let (is_shuffle, is_const_fn) = match fn_ty.sty {
753753
ty::TyFnDef(def_id, _) => {
754754
(self.tcx.fn_sig(def_id).abi() == Abi::PlatformIntrinsic &&
@@ -828,7 +828,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
828828
} else {
829829
// Be conservative about the returned value of a const fn.
830830
let tcx = self.tcx;
831-
let ty = dest.ty(self.mir, tcx).to_ty(tcx);
831+
let ty = dest.ty(&self.mir.local_decls, tcx).to_ty(tcx);
832832
self.qualif = Qualif::empty();
833833
self.add_type(ty);
834834

0 commit comments

Comments
 (0)