Skip to content

Commit 66fce33

Browse files
committed
overload the mir ty methods to make them more ergonomic to use
1 parent 62b2e54 commit 66fce33

File tree

16 files changed

+72
-56
lines changed

16 files changed

+72
-56
lines changed

src/librustc/mir/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ macro_rules! newtype_index {
6969
/// Types for locals
7070
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
7171

72+
pub trait AsLocalDeclsRef<'tcx> {
73+
fn as_ref(&self) -> &LocalDecls<'tcx>;
74+
}
75+
76+
impl<'tcx> AsLocalDeclsRef<'tcx> for LocalDecls<'tcx> {
77+
fn as_ref(&self) -> &LocalDecls<'tcx> {
78+
self
79+
}
80+
}
81+
82+
impl<'tcx> AsLocalDeclsRef<'tcx> for Mir<'tcx> {
83+
fn as_ref(&self) -> &LocalDecls<'tcx> {
84+
&self.local_decls
85+
}
86+
}
87+
7288
/// Lowered representation of a single function.
7389
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
7490
pub struct Mir<'tcx> {

src/librustc/mir/tcx.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
121121
}
122122

123123
impl<'tcx> Lvalue<'tcx> {
124-
pub fn ty<'a, 'gcx>(&self, local_decls: &LocalDecls<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx> {
124+
pub fn ty<'a, 'gcx, D: AsLocalDeclsRef<'tcx>>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx> {
125125
match *self {
126126
Lvalue::Local(index) =>
127-
LvalueTy::Ty { ty: local_decls[index].ty },
127+
LvalueTy::Ty { ty: local_decls.as_ref()[index].ty },
128128
Lvalue::Static(ref data) =>
129129
LvalueTy::Ty { ty: data.ty },
130130
Lvalue::Projection(ref proj) =>
@@ -134,7 +134,7 @@ impl<'tcx> Lvalue<'tcx> {
134134
}
135135

136136
impl<'tcx> Rvalue<'tcx> {
137-
pub fn ty<'a, 'gcx>(&self, local_decls: &LocalDecls<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
137+
pub fn ty<'a, 'gcx, D: AsLocalDeclsRef<'tcx>>(&self, local_decls: &D, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>
138138
{
139139
match *self {
140140
Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
@@ -206,7 +206,7 @@ impl<'tcx> Rvalue<'tcx> {
206206
}
207207

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

src/librustc_mir/dataflow/drop_flag_effects.rs

+3-3
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.local_decls, tcx).to_ty(tcx);
132+
let ty = lv.ty(mir, 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.local_decls, tcx).to_ty(tcx);
219+
let ty = lvalue.ty(mir, 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.local_decls, tcx).to_ty(tcx);
266+
let ty = lvalue.ty(mir, 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

+2-2
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.local_decls, self.tcx).to_ty(self.tcx);
289+
let lv_ty = proj.base.ty(self.mir, 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.local_decls, self.tcx).to_ty(self.tcx);
507+
let lv_ty = lval.ty(self.mir, 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

+5-5
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.local_decls, 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;
@@ -390,7 +390,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
390390
BorrowKind::Mut,
391391
destination.0);
392392

393-
let ty = dest.ty(&caller_mir.local_decls, self.tcx);
393+
let ty = dest.ty(caller_mir, 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.local_decls, self.tcx);
425+
let ptr_ty = args[0].ty(caller_mir, 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.local_decls, self.tcx);
478+
let ty = arg.ty(caller_mir, 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.local_decls, tcx);
532+
let ty = arg.ty(caller_mir, 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

+1-1
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.local_decls, self.tcx).to_ty(self.tcx).is_region_ptr() {
90+
if projection.base.ty(self.mir, 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

+2-2
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.local_decls, tcx).to_ty(tcx))
365+
(statement.source_info.span, dest.ty(mir, 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.local_decls, tcx)
371+
args[2].ty(mir, tcx)
372372
}
373373
_ => {
374374
span_bug!(terminator.source_info.span,

src/librustc_mir/transform/qualify_consts.rs

+8-8
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.local_decls, this.tcx).to_ty(this.tcx);
510+
let base_ty = proj.base.ty(this.mir, 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.local_decls, this.tcx).to_ty(this.tcx);
533+
let ty = lvalue.ty(this.mir, 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.local_decls, self.tcx).to_ty(self.tcx);
609+
let ty = lvalue.ty(self.mir, 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.local_decls, self.tcx);
674+
let operand_ty = operand.ty(self.mir, 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.local_decls, self.tcx).sty {
692+
if let ty::TyRawPtr(_) = lhs.ty(self.mir, 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.local_decls, self.tcx);
730+
let ty = rvalue.ty(self.mir, 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.local_decls, self.tcx);
751+
let fn_ty = func.ty(self.mir, 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.local_decls, tcx).to_ty(tcx);
831+
let ty = dest.ty(self.mir, tcx).to_ty(tcx);
832832
self.qualif = Qualif::empty();
833833
self.add_type(ty);
834834

src/librustc_mir/transform/type_check.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
8484

8585
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
8686
self.super_rvalue(rvalue, location);
87-
let rval_ty = rvalue.ty(&self.mir.local_decls, self.tcx());
87+
let rval_ty = rvalue.ty(self.mir, self.tcx());
8888
self.sanitize_type(rvalue, rval_ty);
8989
}
9090

@@ -178,7 +178,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
178178
}
179179
ProjectionElem::Index(ref i) => {
180180
self.visit_operand(i, location);
181-
let index_ty = i.ty(&self.mir.local_decls, tcx);
181+
let index_ty = i.ty(self.mir, tcx);
182182
if index_ty != tcx.types.usize {
183183
LvalueTy::Ty {
184184
ty: span_mirbug_and_err!(self, i, "index by non-usize {:?}", i)
@@ -378,15 +378,15 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
378378
let tcx = self.tcx();
379379
match stmt.kind {
380380
StatementKind::Assign(ref lv, ref rv) => {
381-
let lv_ty = lv.ty(&mir.local_decls, tcx).to_ty(tcx);
382-
let rv_ty = rv.ty(&mir.local_decls, tcx);
381+
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
382+
let rv_ty = rv.ty(mir, tcx);
383383
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
384384
span_mirbug!(self, stmt, "bad assignment ({:?} = {:?}): {:?}",
385385
lv_ty, rv_ty, terr);
386386
}
387387
}
388388
StatementKind::SetDiscriminant{ ref lvalue, variant_index } => {
389-
let lvalue_type = lvalue.ty(&mir.local_decls, tcx).to_ty(tcx);
389+
let lvalue_type = lvalue.ty(mir, tcx).to_ty(tcx);
390390
let adt = match lvalue_type.sty {
391391
TypeVariants::TyAdt(adt, _) if adt.is_enum() => adt,
392392
_ => {
@@ -438,15 +438,15 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
438438
ref value,
439439
..
440440
} => {
441-
let lv_ty = location.ty(&mir.local_decls, tcx).to_ty(tcx);
442-
let rv_ty = value.ty(&mir.local_decls, tcx);
441+
let lv_ty = location.ty(mir, tcx).to_ty(tcx);
442+
let rv_ty = value.ty(mir, tcx);
443443
if let Err(terr) = self.sub_types(rv_ty, lv_ty) {
444444
span_mirbug!(self, term, "bad DropAndReplace ({:?} = {:?}): {:?}",
445445
lv_ty, rv_ty, terr);
446446
}
447447
}
448448
TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
449-
let discr_ty = discr.ty(&mir.local_decls, tcx);
449+
let discr_ty = discr.ty(mir, tcx);
450450
if let Err(terr) = self.sub_types(discr_ty, switch_ty) {
451451
span_mirbug!(self, term, "bad SwitchInt ({:?} on {:?}): {:?}",
452452
switch_ty, discr_ty, terr);
@@ -459,7 +459,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
459459
// FIXME: check the values
460460
}
461461
TerminatorKind::Call { ref func, ref args, ref destination, .. } => {
462-
let func_ty = func.ty(&mir.local_decls, tcx);
462+
let func_ty = func.ty(mir, tcx);
463463
debug!("check_terminator: call, func_ty={:?}", func_ty);
464464
let sig = match func_ty.sty {
465465
ty::TyFnDef(..) | ty::TyFnPtr(_) => func_ty.fn_sig(tcx),
@@ -479,16 +479,16 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
479479
}
480480
}
481481
TerminatorKind::Assert { ref cond, ref msg, .. } => {
482-
let cond_ty = cond.ty(&mir.local_decls, tcx);
482+
let cond_ty = cond.ty(mir, tcx);
483483
if cond_ty != tcx.types.bool {
484484
span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
485485
}
486486

487487
if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
488-
if len.ty(&mir.local_decls, tcx) != tcx.types.usize {
488+
if len.ty(mir, tcx) != tcx.types.usize {
489489
span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
490490
}
491-
if index.ty(&mir.local_decls, tcx) != tcx.types.usize {
491+
if index.ty(mir, tcx) != tcx.types.usize {
492492
span_mirbug!(self, index, "bounds-check index non-usize {:?}", index)
493493
}
494494
}
@@ -504,7 +504,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
504504
let tcx = self.tcx();
505505
match *destination {
506506
Some((ref dest, _)) => {
507-
let dest_ty = dest.ty(&mir.local_decls, tcx).to_ty(tcx);
507+
let dest_ty = dest.ty(mir, tcx).to_ty(tcx);
508508
if let Err(terr) = self.sub_types(sig.output(), dest_ty) {
509509
span_mirbug!(self, term,
510510
"call dest mismatch ({:?} <- {:?}): {:?}",
@@ -532,7 +532,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
532532
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
533533
}
534534
for (n, (fn_arg, op_arg)) in sig.inputs().iter().zip(args).enumerate() {
535-
let op_arg_ty = op_arg.ty(&mir.local_decls, self.tcx());
535+
let op_arg_ty = op_arg.ty(mir, self.tcx());
536536
if let Err(terr) = self.sub_types(op_arg_ty, fn_arg) {
537537
span_mirbug!(self, term, "bad arg #{:?} ({:?} <- {:?}): {:?}",
538538
n, fn_arg, op_arg_ty, terr);
@@ -581,7 +581,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
581581
return;
582582
}
583583

584-
let ty = args[0].ty(&mir.local_decls, self.tcx());
584+
let ty = args[0].ty(mir, self.tcx());
585585
let arg_ty = match ty.sty {
586586
ty::TyRawPtr(mt) => mt.ty,
587587
ty::TyAdt(def, _) if def.is_box() => ty.boxed_ty(),

src/librustc_mir/util/elaborate_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
130130
where D: DropElaborator<'b, 'tcx>
131131
{
132132
fn lvalue_ty(&self, lvalue: &Lvalue<'tcx>) -> Ty<'tcx> {
133-
lvalue.ty(&self.elaborator.mir().local_decls, self.tcx()).to_ty(self.tcx())
133+
lvalue.ty(self.elaborator.mir(), self.tcx()).to_ty(self.tcx())
134134
}
135135

136136
fn tcx(&self) -> ty::TyCtxt<'b, 'tcx, 'tcx> {

0 commit comments

Comments
 (0)