Skip to content

Commit 23ecebd

Browse files
committed
Auto merge of rust-lang#43174 - RalfJung:refactor-ty, r=nikomatsakis
Refactor: {Lvalue,Rvalue,Operand}::ty only need the locals' types, not the full &Mir I am writing code that needs to call these `ty` methods while mutating MIR -- which is impossible with the current API. Even with the refactoring the situation is not great: I am cloning the `local_decls` and then passing the clone to the `ty` methods. I have to clone because `Mir::basic_blocks_mut` borrows the entire `Mir` including the `local_decls`. But even that is better than not being able to get these types at all... Cc @nikomatsakis
2 parents 6d9d82d + 0bbc315 commit 23ecebd

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

src/librustc/mir/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ macro_rules! newtype_index {
6666
)
6767
}
6868

69+
/// Types for locals
70+
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
71+
72+
pub trait HasLocalDecls<'tcx> {
73+
fn local_decls(&self) -> &LocalDecls<'tcx>;
74+
}
75+
76+
impl<'tcx> HasLocalDecls<'tcx> for LocalDecls<'tcx> {
77+
fn local_decls(&self) -> &LocalDecls<'tcx> {
78+
self
79+
}
80+
}
81+
82+
impl<'tcx> HasLocalDecls<'tcx> for Mir<'tcx> {
83+
fn local_decls(&self) -> &LocalDecls<'tcx> {
84+
&self.local_decls
85+
}
86+
}
87+
6988
/// Lowered representation of a single function.
7089
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
7190
pub struct Mir<'tcx> {
@@ -90,7 +109,7 @@ pub struct Mir<'tcx> {
90109
/// The first local is the return value pointer, followed by `arg_count`
91110
/// locals for the function arguments, followed by any user-declared
92111
/// variables and temporaries.
93-
pub local_decls: IndexVec<Local, LocalDecl<'tcx>>,
112+
pub local_decls: LocalDecls<'tcx>,
94113

95114
/// Number of arguments this function takes.
96115
///

src/librustc/mir/tcx.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,34 @@ 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, D>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx>
125+
where D: HasLocalDecls<'tcx>
126+
{
125127
match *self {
126128
Lvalue::Local(index) =>
127-
LvalueTy::Ty { ty: mir.local_decls[index].ty },
129+
LvalueTy::Ty { ty: local_decls.local_decls()[index].ty },
128130
Lvalue::Static(ref data) =>
129131
LvalueTy::Ty { ty: data.ty },
130132
Lvalue::Projection(ref proj) =>
131-
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
133+
proj.base.ty(local_decls, tcx).projection_ty(tcx, &proj.elem),
132134
}
133135
}
134136
}
135137

136138
impl<'tcx> Rvalue<'tcx> {
137-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
139+
pub fn ty<'a, 'gcx, D>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
140+
where D: HasLocalDecls<'tcx>
138141
{
139142
match *self {
140-
Rvalue::Use(ref operand) => operand.ty(mir, tcx),
143+
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
141144
Rvalue::Repeat(ref operand, ref count) => {
142-
let op_ty = operand.ty(mir, tcx);
145+
let op_ty = operand.ty(local_decls, tcx);
143146
let count = count.as_u64(tcx.sess.target.uint_type);
144147
assert_eq!(count as usize as u64, count);
145148
tcx.mk_array(op_ty, count as usize)
146149
}
147150
Rvalue::Ref(reg, bk, ref lv) => {
148-
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
151+
let lv_ty = lv.ty(local_decls, tcx).to_ty(tcx);
149152
tcx.mk_ref(reg,
150153
ty::TypeAndMut {
151154
ty: lv_ty,
@@ -156,22 +159,22 @@ impl<'tcx> Rvalue<'tcx> {
156159
Rvalue::Len(..) => tcx.types.usize,
157160
Rvalue::Cast(.., ty) => ty,
158161
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
159-
let lhs_ty = lhs.ty(mir, tcx);
160-
let rhs_ty = rhs.ty(mir, tcx);
162+
let lhs_ty = lhs.ty(local_decls, tcx);
163+
let rhs_ty = rhs.ty(local_decls, tcx);
161164
op.ty(tcx, lhs_ty, rhs_ty)
162165
}
163166
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
164-
let lhs_ty = lhs.ty(mir, tcx);
165-
let rhs_ty = rhs.ty(mir, tcx);
167+
let lhs_ty = lhs.ty(local_decls, tcx);
168+
let rhs_ty = rhs.ty(local_decls, tcx);
166169
let ty = op.ty(tcx, lhs_ty, rhs_ty);
167170
tcx.intern_tup(&[ty, tcx.types.bool], false)
168171
}
169172
Rvalue::UnaryOp(UnOp::Not, ref operand) |
170173
Rvalue::UnaryOp(UnOp::Neg, ref operand) => {
171-
operand.ty(mir, tcx)
174+
operand.ty(local_decls, tcx)
172175
}
173176
Rvalue::Discriminant(ref lval) => {
174-
let ty = lval.ty(mir, tcx).to_ty(tcx);
177+
let ty = lval.ty(local_decls, tcx).to_ty(tcx);
175178
if let ty::TyAdt(adt_def, _) = ty.sty {
176179
adt_def.repr.discr_type().to_ty(tcx)
177180
} else {
@@ -189,7 +192,7 @@ impl<'tcx> Rvalue<'tcx> {
189192
}
190193
AggregateKind::Tuple => {
191194
tcx.mk_tup(
192-
ops.iter().map(|op| op.ty(mir, tcx)),
195+
ops.iter().map(|op| op.ty(local_decls, tcx)),
193196
false
194197
)
195198
}
@@ -206,9 +209,11 @@ impl<'tcx> Rvalue<'tcx> {
206209
}
207210

208211
impl<'tcx> Operand<'tcx> {
209-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
212+
pub fn ty<'a, 'gcx, D>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
213+
where D: HasLocalDecls<'tcx>
214+
{
210215
match self {
211-
&Operand::Consume(ref l) => l.ty(mir, tcx).to_ty(tcx),
216+
&Operand::Consume(ref l) => l.ty(local_decls, tcx).to_ty(tcx),
212217
&Operand::Constant(ref c) => c.ty,
213218
}
214219
}

src/librustc_mir/transform/inline.rs

+1-1
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, tcx).subst(tcx, callsite.substs);
254254
let ty = ty.to_ty(tcx);
255255
if ty.needs_drop(tcx, param_env) {
256256
cost += CALL_PENALTY;

src/librustc_trans/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
263263
}
264264

265265
mir::TerminatorKind::Drop { ref location, target, unwind } => {
266-
let ty = location.ty(&self.mir, bcx.tcx()).to_ty(bcx.tcx());
266+
let ty = location.ty(self.mir, bcx.tcx()).to_ty(bcx.tcx());
267267
let ty = self.monomorphize(&ty);
268268
let drop_fn = monomorphize::resolve_drop_in_place(bcx.ccx.shared(), ty);
269269

@@ -438,7 +438,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
438438

439439
let extra_args = &args[sig.inputs().len()..];
440440
let extra_args = extra_args.iter().map(|op_arg| {
441-
let op_ty = op_arg.ty(&self.mir, bcx.tcx());
441+
let op_ty = op_arg.ty(self.mir, bcx.tcx());
442442
self.monomorphize(&op_ty)
443443
}).collect::<Vec<_>>();
444444

src/librustc_trans/mir/lvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
408408

409409
pub fn monomorphized_lvalue_ty(&self, lvalue: &mir::Lvalue<'tcx>) -> Ty<'tcx> {
410410
let tcx = self.ccx.tcx();
411-
let lvalue_ty = lvalue.ty(&self.mir, tcx);
411+
let lvalue_ty = lvalue.ty(self.mir, tcx);
412412
self.monomorphize(&lvalue_ty.to_ty(tcx))
413413
}
414414
}

0 commit comments

Comments
 (0)