Skip to content

Commit 9f80938

Browse files
committed
refactor other type methods
1 parent 28abc0a commit 9f80938

File tree

9 files changed

+99
-105
lines changed

9 files changed

+99
-105
lines changed

src/librustc/mir/repr.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ use syntax::ast::{self, Name};
3232
use syntax_pos::Span;
3333

3434
use super::cache::Cache;
35-
use super::super::ty::TyCtxt;
36-
use super::tcx::LvalueTy;
3735

3836
macro_rules! newtype_index {
3937
($name:ident, $debug_name:expr) => (
@@ -811,29 +809,8 @@ impl<'tcx> Lvalue<'tcx> {
811809
elem: elem,
812810
}))
813811
}
814-
815-
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx>
816-
{
817-
match self {
818-
&Lvalue::Var(index) =>
819-
LvalueTy::Ty { ty: mir.var_decls[index].ty },
820-
&Lvalue::Temp(index) =>
821-
LvalueTy::Ty { ty: mir.temp_decls[index].ty },
822-
&Lvalue::Arg(index) =>
823-
LvalueTy::Ty { ty: mir.arg_decls[index].ty },
824-
&Lvalue::Static(def_id) =>
825-
LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
826-
&Lvalue::ReturnPointer =>
827-
LvalueTy::Ty { ty: mir.return_ty.unwrap() },
828-
&Lvalue::Projection(ref proj) =>
829-
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
830-
}
831-
}
832812
}
833813

834-
835-
836-
837814
impl<'tcx> Debug for Lvalue<'tcx> {
838815
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
839816
use self::Lvalue::*;

src/librustc/mir/tcx.rs

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -113,55 +113,38 @@ impl<'tcx> TypeFoldable<'tcx> for LvalueTy<'tcx> {
113113
}
114114
}
115115

116-
impl<'a, 'gcx, 'tcx> Mir<'tcx> {
117-
pub fn operand_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
118-
operand: &Operand<'tcx>)
119-
-> Ty<'tcx>
120-
{
121-
match *operand {
122-
Operand::Consume(ref l) => l.ty(self, tcx).to_ty(tcx),
123-
Operand::Constant(ref c) => c.ty,
124-
}
125-
}
126-
127-
pub fn binop_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
128-
op: BinOp,
129-
lhs_ty: Ty<'tcx>,
130-
rhs_ty: Ty<'tcx>)
131-
-> Ty<'tcx>
132-
{
133-
// FIXME: handle SIMD correctly
134-
match op {
135-
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem |
136-
BinOp::BitXor | BinOp::BitAnd | BinOp::BitOr => {
137-
// these should be integers or floats of the same size.
138-
assert_eq!(lhs_ty, rhs_ty);
139-
lhs_ty
140-
}
141-
BinOp::Shl | BinOp::Shr => {
142-
lhs_ty // lhs_ty can be != rhs_ty
143-
}
144-
BinOp::Eq | BinOp::Lt | BinOp::Le |
145-
BinOp::Ne | BinOp::Ge | BinOp::Gt => {
146-
tcx.types.bool
147-
}
116+
impl<'tcx> Lvalue<'tcx> {
117+
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> LvalueTy<'tcx> {
118+
match self {
119+
&Lvalue::Var(index) =>
120+
LvalueTy::Ty { ty: mir.var_decls[index].ty },
121+
&Lvalue::Temp(index) =>
122+
LvalueTy::Ty { ty: mir.temp_decls[index].ty },
123+
&Lvalue::Arg(index) =>
124+
LvalueTy::Ty { ty: mir.arg_decls[index].ty },
125+
&Lvalue::Static(def_id) =>
126+
LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
127+
&Lvalue::ReturnPointer =>
128+
LvalueTy::Ty { ty: mir.return_ty.unwrap() },
129+
&Lvalue::Projection(ref proj) =>
130+
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
148131
}
149132
}
133+
}
150134

151-
pub fn rvalue_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
152-
rvalue: &Rvalue<'tcx>)
153-
-> Option<Ty<'tcx>>
135+
impl<'tcx> Rvalue<'tcx> {
136+
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>>
154137
{
155-
match *rvalue {
156-
Rvalue::Use(ref operand) => Some(self.operand_ty(tcx, operand)),
157-
Rvalue::Repeat(ref operand, ref count) => {
158-
let op_ty = self.operand_ty(tcx, operand);
138+
match self {
139+
&Rvalue::Use(ref operand) => Some(operand.ty(mir, tcx)),
140+
&Rvalue::Repeat(ref operand, ref count) => {
141+
let op_ty = operand.ty(mir, tcx);
159142
let count = count.value.as_u64(tcx.sess.target.uint_type);
160143
assert_eq!(count as usize as u64, count);
161144
Some(tcx.mk_array(op_ty, count as usize))
162145
}
163-
Rvalue::Ref(reg, bk, ref lv) => {
164-
let lv_ty = lv.ty(self, tcx).to_ty(tcx);
146+
&Rvalue::Ref(reg, bk, ref lv) => {
147+
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
165148
Some(tcx.mk_ref(
166149
tcx.mk_region(reg),
167150
ty::TypeAndMut {
@@ -170,39 +153,39 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
170153
}
171154
))
172155
}
173-
Rvalue::Len(..) => Some(tcx.types.usize),
174-
Rvalue::Cast(_, _, ty) => Some(ty),
175-
Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
176-
let lhs_ty = self.operand_ty(tcx, lhs);
177-
let rhs_ty = self.operand_ty(tcx, rhs);
178-
Some(self.binop_ty(tcx, op, lhs_ty, rhs_ty))
156+
&Rvalue::Len(..) => Some(tcx.types.usize),
157+
&Rvalue::Cast(_, _, ty) => Some(ty),
158+
&Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
159+
let lhs_ty = lhs.ty(mir, tcx);
160+
let rhs_ty = rhs.ty(mir, tcx);
161+
Some(op.ty(tcx, lhs_ty, rhs_ty))
179162
}
180-
Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
181-
let lhs_ty = self.operand_ty(tcx, lhs);
182-
let rhs_ty = self.operand_ty(tcx, rhs);
183-
let ty = self.binop_ty(tcx, op, lhs_ty, rhs_ty);
163+
&Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
164+
let lhs_ty = lhs.ty(mir, tcx);
165+
let rhs_ty = rhs.ty(mir, tcx);
166+
let ty = op.ty(tcx, lhs_ty, rhs_ty);
184167
let ty = tcx.mk_tup(vec![ty, tcx.types.bool]);
185168
Some(ty)
186169
}
187-
Rvalue::UnaryOp(_, ref operand) => {
188-
Some(self.operand_ty(tcx, operand))
170+
&Rvalue::UnaryOp(_, ref operand) => {
171+
Some(operand.ty(mir, tcx))
189172
}
190-
Rvalue::Box(t) => {
173+
&Rvalue::Box(t) => {
191174
Some(tcx.mk_box(t))
192175
}
193-
Rvalue::Aggregate(ref ak, ref ops) => {
176+
&Rvalue::Aggregate(ref ak, ref ops) => {
194177
match *ak {
195178
AggregateKind::Vec => {
196179
if let Some(operand) = ops.get(0) {
197-
let ty = self.operand_ty(tcx, operand);
180+
let ty = operand.ty(mir, tcx);
198181
Some(tcx.mk_array(ty, ops.len()))
199182
} else {
200183
None
201184
}
202185
}
203186
AggregateKind::Tuple => {
204187
Some(tcx.mk_tup(
205-
ops.iter().map(|op| self.operand_ty(tcx, op)).collect()
188+
ops.iter().map(|op| op.ty(mir, tcx)).collect()
206189
))
207190
}
208191
AggregateKind::Adt(def, _, substs) => {
@@ -213,11 +196,45 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
213196
}
214197
}
215198
}
216-
Rvalue::InlineAsm { .. } => None
199+
&Rvalue::InlineAsm { .. } => None
217200
}
218201
}
219202
}
220203

204+
impl<'tcx> Operand<'tcx> {
205+
pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
206+
match self {
207+
&Operand::Consume(ref l) => l.ty(mir, tcx).to_ty(tcx),
208+
&Operand::Constant(ref c) => c.ty,
209+
}
210+
}
211+
}
212+
213+
impl<'tcx> BinOp {
214+
pub fn ty<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
215+
lhs_ty: Ty<'tcx>,
216+
rhs_ty: Ty<'tcx>)
217+
-> Ty<'tcx>
218+
{
219+
// FIXME: handle SIMD correctly
220+
match self {
221+
&BinOp::Add | &BinOp::Sub | &BinOp::Mul | &BinOp::Div | &BinOp::Rem |
222+
&BinOp::BitXor | &BinOp::BitAnd | &BinOp::BitOr => {
223+
// these should be integers or floats of the same size.
224+
assert_eq!(lhs_ty, rhs_ty);
225+
lhs_ty
226+
}
227+
&BinOp::Shl | &BinOp::Shr => {
228+
lhs_ty // lhs_ty can be != rhs_ty
229+
}
230+
&BinOp::Eq | &BinOp::Lt | &BinOp::Le |
231+
&BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
232+
tcx.types.bool
233+
}
234+
}
235+
}
236+
}
237+
221238
impl BorrowKind {
222239
pub fn to_mutbl_lossy(self) -> hir::Mutability {
223240
match self {

src/librustc_mir/transform/promote_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
353353
let terminator = mir[bb].terminator();
354354
let ty = match terminator.kind {
355355
TerminatorKind::Call { ref args, .. } => {
356-
mir.operand_ty(tcx, &args[2])
356+
args[2].ty(mir, tcx)
357357
}
358358
_ => {
359359
span_bug!(terminator.source_info.span,

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
649649
}
650650

651651
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
652-
let operand_ty = self.mir.operand_ty(self.tcx, operand);
652+
let operand_ty = operand.ty(self.mir, self.tcx);
653653
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
654654
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
655655
match (cast_in, cast_out) {
@@ -667,7 +667,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
667667
}
668668

669669
Rvalue::BinaryOp(op, ref lhs, _) => {
670-
if let ty::TyRawPtr(_) = self.mir.operand_ty(self.tcx, lhs).sty {
670+
if let ty::TyRawPtr(_) = lhs.ty(self.mir, self.tcx).sty {
671671
assert!(op == BinOp::Eq || op == BinOp::Ne ||
672672
op == BinOp::Le || op == BinOp::Lt ||
673673
op == BinOp::Ge || op == BinOp::Gt);
@@ -697,7 +697,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
697697
}
698698

699699
if Some(def.did) == self.tcx.lang_items.unsafe_cell_type() {
700-
let ty = self.mir.rvalue_ty(self.tcx, rvalue).unwrap();
700+
let ty = rvalue.ty(self.mir, self.tcx).unwrap();
701701
self.add_type(ty);
702702
assert!(self.qualif.intersects(Qualif::MUTABLE_INTERIOR));
703703
// Even if the value inside may not need dropping,
@@ -719,7 +719,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
719719
if let TerminatorKind::Call { ref func, ref args, ref destination, .. } = *kind {
720720
self.visit_operand(func);
721721

722-
let fn_ty = self.mir.operand_ty(self.tcx, func);
722+
let fn_ty = func.ty(self.mir, self.tcx);
723723
let (is_shuffle, is_const_fn) = match fn_ty.sty {
724724
ty::TyFnDef(def_id, _, f) => {
725725
(f.abi == Abi::PlatformIntrinsic &&

src/librustc_mir/transform/type_check.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
8181

8282
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
8383
self.super_rvalue(rvalue);
84-
if let Some(ty) = self.mir.rvalue_ty(self.tcx(), rvalue) {
84+
if let Some(ty) = rvalue.ty(self.mir, self.tcx()) {
8585
self.sanitize_type(rvalue, ty);
8686
}
8787
}
@@ -180,7 +180,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
180180
}
181181
ProjectionElem::Index(ref i) => {
182182
self.visit_operand(i);
183-
let index_ty = self.mir.operand_ty(tcx, i);
183+
let index_ty = i.ty(self.mir, tcx);
184184
if index_ty != tcx.types.usize {
185185
LvalueTy::Ty {
186186
ty: span_mirbug_and_err!(self, i, "index by non-usize {:?}", i)
@@ -356,7 +356,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
356356
match stmt.kind {
357357
StatementKind::Assign(ref lv, ref rv) => {
358358
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
359-
let rv_ty = mir.rvalue_ty(tcx, rv);
359+
let rv_ty = rv.ty(mir, tcx);
360360
if let Some(rv_ty) = rv_ty {
361361
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
362362
span_mirbug!(self, stmt, "bad assignment ({:?} = {:?}): {:?}",
@@ -391,15 +391,15 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
391391
..
392392
} => {
393393
let lv_ty = location.ty(mir, tcx).to_ty(tcx);
394-
let rv_ty = mir.operand_ty(tcx, value);
394+
let rv_ty = value.ty(mir, tcx);
395395
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
396396
span_mirbug!(self, term, "bad DropAndReplace ({:?} = {:?}): {:?}",
397397
lv_ty, rv_ty, terr);
398398
}
399399
}
400400

401401
TerminatorKind::If { ref cond, .. } => {
402-
let cond_ty = mir.operand_ty(tcx, cond);
402+
let cond_ty = cond.ty(mir, tcx);
403403
match cond_ty.sty {
404404
ty::TyBool => {}
405405
_ => {
@@ -433,7 +433,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
433433
}
434434
}
435435
TerminatorKind::Call { ref func, ref args, ref destination, .. } => {
436-
let func_ty = mir.operand_ty(tcx, func);
436+
let func_ty = func.ty(mir, tcx);
437437
debug!("check_terminator: call, func_ty={:?}", func_ty);
438438
let func_ty = match func_ty.sty {
439439
ty::TyFnDef(_, _, func_ty) | ty::TyFnPtr(func_ty) => func_ty,
@@ -453,16 +453,16 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
453453
}
454454
}
455455
TerminatorKind::Assert { ref cond, ref msg, .. } => {
456-
let cond_ty = mir.operand_ty(tcx, cond);
456+
let cond_ty = cond.ty(mir, tcx);
457457
if cond_ty != tcx.types.bool {
458458
span_mirbug!(self, term, "bad Assert ({:?}, not bool", cond_ty);
459459
}
460460

461461
if let AssertMessage::BoundsCheck { ref len, ref index } = *msg {
462-
if mir.operand_ty(tcx, len) != tcx.types.usize {
462+
if len.ty(mir, tcx) != tcx.types.usize {
463463
span_mirbug!(self, len, "bounds-check length non-usize {:?}", len)
464464
}
465-
if mir.operand_ty(tcx, index) != tcx.types.usize {
465+
if index.ty(mir, tcx) != tcx.types.usize {
466466
span_mirbug!(self, index, "bounds-check index non-usize {:?}", index)
467467
}
468468
}
@@ -507,7 +507,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
507507
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
508508
}
509509
for (n, (fn_arg, op_arg)) in sig.inputs.iter().zip(args).enumerate() {
510-
let op_arg_ty = mir.operand_ty(self.tcx(), op_arg);
510+
let op_arg_ty = op_arg.ty(mir, self.tcx());
511511
if let Err(terr) = self.sub_types(self.last_span, op_arg_ty, fn_arg) {
512512
span_mirbug!(self, term, "bad arg #{:?} ({:?} <- {:?}): {:?}",
513513
n, fn_arg, op_arg_ty, terr);
@@ -554,7 +554,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
554554
return;
555555
}
556556

557-
let arg_ty = match mir.operand_ty(self.tcx(), &args[0]).sty {
557+
let arg_ty = match args[0].ty(mir, self.tcx()).sty {
558558
ty::TyRawPtr(mt) => mt.ty,
559559
ty::TyBox(ty) => ty,
560560
_ => {

src/librustc_trans/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
478478
let target_ty = monomorphize::apply_param_substs(self.scx.tcx(),
479479
self.param_substs,
480480
&target_ty);
481-
let source_ty = self.mir.operand_ty(self.scx.tcx(), operand);
481+
let source_ty = operand.ty(self.mir, self.scx.tcx());
482482
let source_ty = monomorphize::apply_param_substs(self.scx.tcx(),
483483
self.param_substs,
484484
&source_ty);
@@ -627,7 +627,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
627627
match constant.ty.sty {
628628
ty::TyFnDef(def_id, _, bare_fn_ty)
629629
if is_drop_in_place_intrinsic(tcx, def_id, bare_fn_ty) => {
630-
let operand_ty = self.mir.operand_ty(tcx, &args[0]);
630+
let operand_ty = args[0].ty(self.mir, tcx);
631631
if let ty::TyRawPtr(mt) = operand_ty.sty {
632632
let operand_ty = monomorphize::apply_param_substs(tcx,
633633
self.param_substs,

src/librustc_trans/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
433433

434434
let extra_args = &args[sig.inputs.len()..];
435435
let extra_args = extra_args.iter().map(|op_arg| {
436-
let op_ty = self.mir.operand_ty(bcx.tcx(), op_arg);
436+
let op_ty = op_arg.ty(&self.mir, bcx.tcx());
437437
bcx.monomorphize(&op_ty)
438438
}).collect::<Vec<_>>();
439439
let fn_ty = callee.direct_fn_type(bcx.ccx(), &extra_args);

0 commit comments

Comments
 (0)