Skip to content

Commit 28abc0a

Browse files
committed
refactor lvalue_ty to be method of lvalue
1 parent 4c02363 commit 28abc0a

File tree

13 files changed

+57
-55
lines changed

13 files changed

+57
-55
lines changed

src/librustc/mir/repr.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ 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;
3537

3638
macro_rules! newtype_index {
3739
($name:ident, $debug_name:expr) => (
@@ -809,8 +811,29 @@ impl<'tcx> Lvalue<'tcx> {
809811
elem: elem,
810812
}))
811813
}
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+
}
812832
}
813833

834+
835+
836+
814837
impl<'tcx> Debug for Lvalue<'tcx> {
815838
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
816839
use self::Lvalue::*;

src/librustc/mir/tcx.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
119119
-> Ty<'tcx>
120120
{
121121
match *operand {
122-
Operand::Consume(ref l) => self.lvalue_ty(tcx, l).to_ty(tcx),
122+
Operand::Consume(ref l) => l.ty(self, tcx).to_ty(tcx),
123123
Operand::Constant(ref c) => c.ty,
124124
}
125125
}
@@ -148,26 +148,6 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
148148
}
149149
}
150150

151-
pub fn lvalue_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
152-
lvalue: &Lvalue<'tcx>)
153-
-> LvalueTy<'tcx>
154-
{
155-
match *lvalue {
156-
Lvalue::Var(index) =>
157-
LvalueTy::Ty { ty: self.var_decls[index].ty },
158-
Lvalue::Temp(index) =>
159-
LvalueTy::Ty { ty: self.temp_decls[index].ty },
160-
Lvalue::Arg(index) =>
161-
LvalueTy::Ty { ty: self.arg_decls[index].ty },
162-
Lvalue::Static(def_id) =>
163-
LvalueTy::Ty { ty: tcx.lookup_item_type(def_id).ty },
164-
Lvalue::ReturnPointer =>
165-
LvalueTy::Ty { ty: self.return_ty.unwrap() },
166-
Lvalue::Projection(ref proj) =>
167-
self.lvalue_ty(tcx, &proj.base).projection_ty(tcx, &proj.elem)
168-
}
169-
}
170-
171151
pub fn rvalue_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
172152
rvalue: &Rvalue<'tcx>)
173153
-> Option<Ty<'tcx>>
@@ -181,7 +161,7 @@ impl<'a, 'gcx, 'tcx> Mir<'tcx> {
181161
Some(tcx.mk_array(op_ty, count as usize))
182162
}
183163
Rvalue::Ref(reg, bk, ref lv) => {
184-
let lv_ty = self.lvalue_ty(tcx, lv).to_ty(tcx);
164+
let lv_ty = lv.ty(self, tcx).to_ty(tcx);
185165
Some(tcx.mk_ref(
186166
tcx.mk_region(reg),
187167
ty::TypeAndMut {

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
185185
{
186186
match self.move_data().move_paths[path].content {
187187
MovePathContent::Lvalue(ref lvalue) => {
188-
let ty = self.mir.lvalue_ty(self.tcx, lvalue).to_ty(self.tcx);
188+
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
189189
debug!("path_needs_drop({:?}, {:?} : {:?})", path, lvalue, ty);
190190

191191
self.tcx.type_needs_drop_given_env(ty, self.param_env())
@@ -555,7 +555,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
555555

556556
let mut fields = fields;
557557
fields.retain(|&(ref lvalue, _)| {
558-
let ty = self.mir.lvalue_ty(self.tcx, lvalue).to_ty(self.tcx);
558+
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
559559
self.tcx.type_needs_drop_given_env(ty, self.param_env())
560560
});
561561

@@ -706,7 +706,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
706706
/// This creates a "drop ladder" that drops the needed fields of the
707707
/// ADT, both in the success case or if one of the destructors fail.
708708
fn open_drop<'a>(&mut self, c: &DropCtxt<'a, 'tcx>) -> BasicBlock {
709-
let ty = self.mir.lvalue_ty(self.tcx, c.lvalue).to_ty(self.tcx);
709+
let ty = c.lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
710710
match ty.sty {
711711
ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => {
712712
self.open_drop_for_adt(c, def, substs)
@@ -892,7 +892,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
892892
// dataflow can create unneeded children in some cases
893893
// - be sure to ignore them.
894894

895-
let ty = self.mir.lvalue_ty(self.tcx, c.lvalue).to_ty(self.tcx);
895+
let ty = c.lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
896896

897897
match ty.sty {
898898
ty::TyStruct(def, _) | ty::TyEnum(def, _) => {

src/librustc_borrowck/borrowck/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn move_path_children_matching<'tcx, F>(move_paths: &MovePathData<'tcx>,
256256
fn lvalue_contents_drop_state_cannot_differ<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
257257
mir: &Mir<'tcx>,
258258
lv: &repr::Lvalue<'tcx>) -> bool {
259-
let ty = mir.lvalue_ty(tcx, lv).to_ty(tcx);
259+
let ty = lv.ty(mir, tcx).to_ty(tcx);
260260
match ty.sty {
261261
ty::TyArray(..) | ty::TySlice(..) | ty::TyRef(..) | ty::TyRawPtr(..) => {
262262
debug!("lvalue_contents_drop_state_cannot_differ lv: {:?} ty: {:?} refd => false",
@@ -355,7 +355,7 @@ fn drop_flag_effects_for_location<'a, 'tcx, F>(
355355

356356
// don't move out of non-Copy things
357357
if let MovePathContent::Lvalue(ref lvalue) = move_data.move_paths[path].content {
358-
let ty = mir.lvalue_ty(tcx, lvalue).to_ty(tcx);
358+
let ty = lvalue.ty(mir, tcx).to_ty(tcx);
359359
if !ty.moves_by_default(tcx, param_env, DUMMY_SP) {
360360
continue;
361361
}

src/librustc_mir/transform/promote_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
347347
continue;
348348
}
349349
}
350-
(statement.source_info.span, mir.lvalue_ty(tcx, dest).to_ty(tcx))
350+
(statement.source_info.span, dest.ty(mir, tcx).to_ty(tcx))
351351
}
352352
Candidate::ShuffleIndices(bb) => {
353353
let terminator = mir[bb].terminator();

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
485485
this.add(Qualif::STATIC);
486486
}
487487

488-
let base_ty = this.mir.lvalue_ty(this.tcx, &proj.base)
489-
.to_ty(this.tcx);
488+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
490489
if let ty::TyRawPtr(_) = base_ty.sty {
491490
this.add(Qualif::NOT_CONST);
492491
if this.mode != Mode::Fn {
@@ -505,8 +504,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
505504
"cannot refer to the interior of another \
506505
static, use a constant instead");
507506
}
508-
let ty = this.mir.lvalue_ty(this.tcx, lvalue)
509-
.to_ty(this.tcx);
507+
let ty = lvalue.ty(this.mir, this.tcx).to_ty(this.tcx);
510508
this.qualif.restrict(ty, this.tcx, &this.param_env);
511509
}
512510

@@ -591,7 +589,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
591589
self.add(Qualif::STATIC_REF);
592590
}
593591

594-
let ty = self.mir.lvalue_ty(self.tcx, lvalue).to_ty(self.tcx);
592+
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
595593
if kind == BorrowKind::Mut {
596594
// In theory, any zero-sized value could be borrowed
597595
// mutably without consequences. However, only &mut []
@@ -801,7 +799,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
801799
} else {
802800
// Be conservative about the returned value of a const fn.
803801
let tcx = self.tcx;
804-
let ty = self.mir.lvalue_ty(tcx, dest).to_ty(tcx);
802+
let ty = dest.ty(self.mir, tcx).to_ty(tcx);
805803
self.qualif = Qualif::empty();
806804
self.add_type(ty);
807805

src/librustc_mir/transform/type_check.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
355355
let tcx = self.tcx();
356356
match stmt.kind {
357357
StatementKind::Assign(ref lv, ref rv) => {
358-
let lv_ty = mir.lvalue_ty(tcx, lv).to_ty(tcx);
358+
let lv_ty = lv.ty(mir, tcx).to_ty(tcx);
359359
let rv_ty = mir.rvalue_ty(tcx, rv);
360360
if let Some(rv_ty) = rv_ty {
361361
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
@@ -390,7 +390,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
390390
ref value,
391391
..
392392
} => {
393-
let lv_ty = mir.lvalue_ty(tcx, location).to_ty(tcx);
393+
let lv_ty = location.ty(mir, tcx).to_ty(tcx);
394394
let rv_ty = mir.operand_ty(tcx, value);
395395
if let Err(terr) = self.sub_types(self.last_span, rv_ty, lv_ty) {
396396
span_mirbug!(self, term, "bad DropAndReplace ({:?} = {:?}): {:?}",
@@ -408,7 +408,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
408408
}
409409
}
410410
TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => {
411-
let discr_ty = mir.lvalue_ty(tcx, discr).to_ty(tcx);
411+
let discr_ty = discr.ty(mir, tcx).to_ty(tcx);
412412
if let Err(terr) = self.sub_types(self.last_span, discr_ty, switch_ty) {
413413
span_mirbug!(self, term, "bad SwitchInt ({:?} on {:?}): {:?}",
414414
switch_ty, discr_ty, terr);
@@ -421,7 +421,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
421421
// FIXME: check the values
422422
}
423423
TerminatorKind::Switch { ref discr, adt_def, ref targets } => {
424-
let discr_ty = mir.lvalue_ty(tcx, discr).to_ty(tcx);
424+
let discr_ty = discr.ty(mir, tcx).to_ty(tcx);
425425
match discr_ty.sty {
426426
ty::TyEnum(def, _)
427427
if def == adt_def && adt_def.variants.len() == targets.len()
@@ -481,7 +481,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
481481
span_mirbug!(self, term, "call to diverging function {:?} with dest", sig);
482482
}
483483
(&Some((ref dest, _)), ty::FnConverging(ty)) => {
484-
let dest_ty = mir.lvalue_ty(tcx, dest).to_ty(tcx);
484+
let dest_ty = dest.ty(mir, tcx).to_ty(tcx);
485485
if let Err(terr) = self.sub_types(self.last_span, ty, dest_ty) {
486486
span_mirbug!(self, term,
487487
"call dest mismatch ({:?} <- {:?}): {:?}",

src/librustc_trans/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
525525
debug!("visiting lvalue {:?}", *lvalue);
526526

527527
if let mir_visit::LvalueContext::Drop = context {
528-
let ty = self.mir.lvalue_ty(self.scx.tcx(), lvalue)
529-
.to_ty(self.scx.tcx());
528+
let ty = lvalue.ty(self.mir, self.scx.tcx())
529+
.to_ty(self.scx.tcx());
530530

531531
let ty = monomorphize::apply_param_substs(self.scx.tcx(),
532532
self.param_substs,

src/librustc_trans/mir/analyze.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ impl<'mir, 'bcx, 'tcx> Visitor<'tcx> for LocalAnalyzer<'mir, 'bcx, 'tcx> {
143143
// Allow uses of projections of immediate pair fields.
144144
if let mir::Lvalue::Projection(ref proj) = *lvalue {
145145
if self.mir.local_index(&proj.base).is_some() {
146-
let ty = self.mir.lvalue_ty(self.bcx.tcx(), &proj.base);
146+
let ty = proj.base.ty(self.mir, self.bcx.tcx());
147+
147148
let ty = self.bcx.monomorphize(&ty.to_ty(self.bcx.tcx()));
148149
if common::type_is_imm_pair(self.bcx.ccx(), ty) {
149150
if let mir::ProjectionElem::Field(..) = proj.elem {
@@ -170,7 +171,7 @@ impl<'mir, 'bcx, 'tcx> Visitor<'tcx> for LocalAnalyzer<'mir, 'bcx, 'tcx> {
170171
self.mark_as_lvalue(index);
171172
}
172173
LvalueContext::Drop => {
173-
let ty = self.mir.lvalue_ty(self.bcx.tcx(), lvalue);
174+
let ty = lvalue.ty(self.mir, self.bcx.tcx());
174175
let ty = self.bcx.monomorphize(&ty.to_ty(self.bcx.tcx()));
175176

176177
// Only need the lvalue if we're actually dropping it.

src/librustc_trans/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
230230
}
231231

232232
mir::TerminatorKind::Drop { ref location, target, unwind } => {
233-
let ty = mir.lvalue_ty(bcx.tcx(), location).to_ty(bcx.tcx());
233+
let ty = location.ty(&mir, bcx.tcx()).to_ty(bcx.tcx());
234234
let ty = bcx.monomorphize(&ty);
235235

236236
// Double check for necessity to drop
@@ -828,7 +828,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
828828
return ReturnDest::Nothing;
829829
}
830830
let dest = if let Some(index) = self.mir.local_index(dest) {
831-
let ret_ty = self.lvalue_ty(dest);
831+
let ret_ty = self.monomorphized_lvalue_ty(dest);
832832
match self.locals[index] {
833833
LocalRef::Lvalue(dest) => dest,
834834
LocalRef::Operand(None) => {

src/librustc_trans/mir/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
278278
let span = statement.source_info.span;
279279
match statement.kind {
280280
mir::StatementKind::Assign(ref dest, ref rvalue) => {
281-
let ty = self.mir.lvalue_ty(tcx, dest);
281+
let ty = dest.ty(self.mir, tcx);
282282
let ty = self.monomorphize(&ty).to_ty(tcx);
283283
match self.const_rvalue(rvalue, ty, span) {
284284
Ok(value) => self.store(dest, value, span),
@@ -386,7 +386,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
386386
ConstLvalue {
387387
base: Base::Static(consts::get_static(self.ccx, def_id).val),
388388
llextra: ptr::null_mut(),
389-
ty: self.mir.lvalue_ty(tcx, lvalue).to_ty(tcx)
389+
ty: lvalue.ty(self.mir, tcx).to_ty(tcx)
390390
}
391391
}
392392
mir::Lvalue::Projection(ref projection) => {

src/librustc_trans/mir/lvalue.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
108108
mir::Lvalue::Arg(_) |
109109
mir::Lvalue::ReturnPointer => bug!(), // handled above
110110
mir::Lvalue::Static(def_id) => {
111-
let const_ty = self.lvalue_ty(lvalue);
111+
let const_ty = self.monomorphized_lvalue_ty(lvalue);
112112
LvalueRef::new_sized(consts::get_static(ccx, def_id).val,
113113
LvalueTy::from_ty(const_ty))
114114
},
@@ -200,7 +200,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
200200
ty::TyArray(..) => {
201201
// must cast the lvalue pointer type to the new
202202
// array type (*[%_; new_len]).
203-
let base_ty = self.lvalue_ty(lvalue);
203+
let base_ty = self.monomorphized_lvalue_ty(lvalue);
204204
let llbasety = type_of::type_of(bcx.ccx(), base_ty).ptr_to();
205205
let llbase = bcx.pointercast(llbase, llbasety);
206206
(llbase, ptr::null_mut())
@@ -240,7 +240,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
240240
match self.locals[index] {
241241
LocalRef::Lvalue(lvalue) => f(self, lvalue),
242242
LocalRef::Operand(None) => {
243-
let lvalue_ty = self.lvalue_ty(lvalue);
243+
let lvalue_ty = self.monomorphized_lvalue_ty(lvalue);
244244
let lvalue = LvalueRef::alloca(bcx,
245245
lvalue_ty,
246246
"lvalue_temp");
@@ -252,7 +252,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
252252
LocalRef::Operand(Some(_)) => {
253253
// See comments in LocalRef::new_operand as to why
254254
// we always have Some in a ZST LocalRef::Operand.
255-
let ty = self.lvalue_ty(lvalue);
255+
let ty = self.monomorphized_lvalue_ty(lvalue);
256256
if common::type_is_zero_size(bcx.ccx(), ty) {
257257
// Pass an undef pointer as no stores can actually occur.
258258
let llptr = C_undef(type_of(bcx.ccx(), ty).ptr_to());
@@ -289,9 +289,9 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
289289
}
290290
}
291291

292-
pub fn lvalue_ty(&self, lvalue: &mir::Lvalue<'tcx>) -> Ty<'tcx> {
292+
pub fn monomorphized_lvalue_ty(&self, lvalue: &mir::Lvalue<'tcx>) -> Ty<'tcx> {
293293
let tcx = self.fcx.ccx.tcx();
294-
let lvalue_ty = self.mir.lvalue_ty(tcx, lvalue);
294+
let lvalue_ty = lvalue.ty(&self.mir, tcx);
295295
self.fcx.monomorphize(&lvalue_ty.to_ty(tcx))
296296
}
297297
}

src/librustc_trans/mir/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
3939
bcx
4040
}
4141
LocalRef::Operand(Some(_)) => {
42-
let ty = self.lvalue_ty(lvalue);
42+
let ty = self.monomorphized_lvalue_ty(lvalue);
4343

4444
if !common::type_is_zero_size(bcx.ccx(), ty) {
4545
span_bug!(statement.source_info.span,

0 commit comments

Comments
 (0)