Skip to content

Commit a29ba6a

Browse files
authored
Merge pull request rust-lang#249 from RalfJung/lvalue
lvalue: refactoring to permit applying a mir projection to a miri lvalue
2 parents 56d4de3 + 2312ac8 commit a29ba6a

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/lvalue.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
189189
return Ok(val);
190190
}
191191
let lvalue = self.eval_lvalue(lvalue)?;
192+
self.read_lvalue(lvalue, ty)
193+
}
192194

195+
fn read_lvalue(&self, lvalue: Lvalue<'tcx>, ty: Ty<'tcx>) -> EvalResult<'tcx, Value> {
193196
if ty.is_never() {
194197
return Err(EvalError::Unreachable);
195198
}
@@ -219,7 +222,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
219222
Lvalue::Global(GlobalId { instance, promoted: None })
220223
}
221224

222-
Projection(ref proj) => return self.eval_lvalue_projection(proj),
225+
Projection(ref proj) => {
226+
let ty = self.lvalue_ty(&proj.base);
227+
let lvalue = self.eval_lvalue(&proj.base)?;
228+
return self.eval_lvalue_projection(lvalue, ty, &proj.elem);
229+
}
223230
};
224231

225232
if log_enabled!(::log::LogLevel::Trace) {
@@ -348,19 +355,17 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
348355

349356
fn eval_lvalue_projection(
350357
&mut self,
351-
proj: &mir::LvalueProjection<'tcx>,
358+
base: Lvalue<'tcx>,
359+
base_ty: Ty<'tcx>,
360+
proj_elem: &mir::ProjectionElem<'tcx, mir::Operand<'tcx>>,
352361
) -> EvalResult<'tcx, Lvalue<'tcx>> {
353362
use rustc::mir::ProjectionElem::*;
354-
let (ptr, extra, aligned) = match proj.elem {
363+
let (ptr, extra, aligned) = match *proj_elem {
355364
Field(field, field_ty) => {
356-
let base = self.eval_lvalue(&proj.base)?;
357-
let base_ty = self.lvalue_ty(&proj.base);
358365
return self.lvalue_field(base, field.index(), base_ty, field_ty);
359366
}
360367

361368
Downcast(_, variant) => {
362-
let base = self.eval_lvalue(&proj.base)?;
363-
let base_ty = self.lvalue_ty(&proj.base);
364369
let base_layout = self.type_layout(base_ty)?;
365370
// FIXME(solson)
366371
let base = self.force_allocation(base)?;
@@ -376,8 +381,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
376381
}
377382

378383
Deref => {
379-
let base_ty = self.lvalue_ty(&proj.base);
380-
let val = self.eval_and_read_lvalue(&proj.base)?;
384+
let val = self.read_lvalue(base, base_ty)?;
381385

382386
let pointee_type = match base_ty.sty {
383387
ty::TyRawPtr(ref tam) |
@@ -402,8 +406,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
402406
}
403407

404408
Index(ref operand) => {
405-
let base = self.eval_lvalue(&proj.base)?;
406-
let base_ty = self.lvalue_ty(&proj.base);
407409
// FIXME(solson)
408410
let base = self.force_allocation(base)?;
409411
let (base_ptr, _, aligned) = base.to_ptr_extra_aligned();
@@ -419,8 +421,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
419421
}
420422

421423
ConstantIndex { offset, min_length, from_end } => {
422-
let base = self.eval_lvalue(&proj.base)?;
423-
let base_ty = self.lvalue_ty(&proj.base);
424424
// FIXME(solson)
425425
let base = self.force_allocation(base)?;
426426
let (base_ptr, _, aligned) = base.to_ptr_extra_aligned();
@@ -440,8 +440,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
440440
}
441441

442442
Subslice { from, to } => {
443-
let base = self.eval_lvalue(&proj.base)?;
444-
let base_ty = self.lvalue_ty(&proj.base);
445443
// FIXME(solson)
446444
let base = self.force_allocation(base)?;
447445
let (base_ptr, _, aligned) = base.to_ptr_extra_aligned();

0 commit comments

Comments
 (0)