Skip to content

Commit ce810a8

Browse files
committed
Simplify the return lvalue
1 parent 7cf04de commit ce810a8

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

src/eval_context.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ pub struct Frame<'tcx> {
6666
pub return_to_block: StackPopCleanup,
6767

6868
/// The location where the result of the current stack frame should be written to.
69-
/// None if the function is a diverging function
70-
pub return_lvalue: Option<Lvalue<'tcx>>,
69+
pub return_lvalue: Lvalue<'tcx>,
7170

7271
/// The list of locals for this stack frame, stored in order as
7372
/// `[arguments..., variables..., temporaries...]`. The locals are stored as `Option<Value>`s.
@@ -267,7 +266,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
267266
instance: ty::Instance<'tcx>,
268267
span: codemap::Span,
269268
mir: &'tcx mir::Mir<'tcx>,
270-
return_lvalue: Option<Lvalue<'tcx>>,
269+
return_lvalue: Lvalue<'tcx>,
271270
return_to_block: StackPopCleanup,
272271
) -> EvalResult<'tcx> {
273272
::log_settings::settings().indentation += 1;
@@ -324,7 +323,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
324323
::log_settings::settings().indentation -= 1;
325324
let frame = self.stack.pop().expect("tried to pop a stack frame, but there were none");
326325
match frame.return_to_block {
327-
StackPopCleanup::MarkStatic(mutable) => if let Lvalue::Global(id) = frame.return_lvalue.expect("diverging static") {
326+
StackPopCleanup::MarkStatic(mutable) => if let Lvalue::Global(id) = frame.return_lvalue {
328327
let global_value = self.globals.get_mut(&id)
329328
.expect("global should have been cached (static)");
330329
match global_value.value {
@@ -1630,7 +1629,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
16301629
start_instance,
16311630
start_mir.span,
16321631
start_mir,
1633-
Some(Lvalue::from_ptr(ret_ptr)),
1632+
Lvalue::from_ptr(ret_ptr),
16341633
StackPopCleanup::None,
16351634
)?;
16361635

@@ -1657,7 +1656,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
16571656
main_instance,
16581657
main_mir.span,
16591658
main_mir,
1660-
Some(Lvalue::zst()),
1659+
Lvalue::zst(),
16611660
StackPopCleanup::None,
16621661
)?;
16631662
}

src/lvalue.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ pub struct Global<'tcx> {
6464
}
6565

6666
impl<'tcx> Lvalue<'tcx> {
67+
/// Produces an Lvalue that will error if attempted to be read from
68+
pub fn undef() -> Self {
69+
Lvalue::Ptr {
70+
ptr: PrimVal::Undef,
71+
extra: LvalueExtra::None,
72+
}
73+
}
74+
6775
pub fn zst() -> Self {
6876
Self::from_ptr(Pointer::zst_ptr())
6977
}
@@ -148,7 +156,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
148156
pub(super) fn eval_lvalue(&mut self, mir_lvalue: &mir::Lvalue<'tcx>) -> EvalResult<'tcx, Lvalue<'tcx>> {
149157
use rustc::mir::Lvalue::*;
150158
let lvalue = match *mir_lvalue {
151-
Local(mir::RETURN_POINTER) => self.frame().return_lvalue.expect("diverging function returned"),
159+
Local(mir::RETURN_POINTER) => self.frame().return_lvalue,
152160
Local(local) => Lvalue::Local { frame: self.stack.len() - 1, local, field: None },
153161

154162
Static(ref static_) => {

src/step.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
4040
instance,
4141
mir.span,
4242
mir,
43-
Some(Lvalue::zst()),
43+
Lvalue::zst(),
4444
StackPopCleanup::None,
4545
)?;
4646
let arg_local = self.frame().mir.args_iter().next().ok_or(EvalError::AbiViolation("TLS dtor does not take enough arguments.".to_owned()))?;
@@ -206,7 +206,7 @@ impl<'a, 'b, 'tcx> ConstantExtractor<'a, 'b, 'tcx> {
206206
instance,
207207
span,
208208
mir,
209-
Some(Lvalue::Global(cid)),
209+
Lvalue::Global(cid),
210210
cleanup,
211211
)
212212
});
@@ -249,7 +249,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'tcx> {
249249
this.ecx.push_stack_frame(this.instance,
250250
constant.span,
251251
mir,
252-
Some(Lvalue::Global(cid)),
252+
Lvalue::Global(cid),
253253
StackPopCleanup::MarkStatic(false),
254254
)
255255
});

src/terminator/drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
4949
instance,
5050
span,
5151
mir,
52-
Some(Lvalue::zst()),
52+
Lvalue::zst(),
5353
StackPopCleanup::None,
5454
)?;
5555

src/terminator/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
3030
use rustc::mir::TerminatorKind::*;
3131
match terminator.kind {
3232
Return => {
33-
self.dump_local(self.frame().return_lvalue.expect("diverging function returned"));
33+
self.dump_local(self.frame().return_lvalue);
3434
self.pop_stack_frame()?
3535
}
3636

@@ -430,8 +430,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
430430
Err(other) => return Err(other),
431431
};
432432
let (return_lvalue, return_to_block) = match destination {
433-
Some((lvalue, block)) => (Some(lvalue), StackPopCleanup::Goto(block)),
434-
None => (None, StackPopCleanup::None),
433+
Some((lvalue, block)) => (lvalue, StackPopCleanup::Goto(block)),
434+
None => (Lvalue::undef(), StackPopCleanup::None),
435435
};
436436

437437
self.push_stack_frame(
@@ -606,7 +606,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
606606
f_instance,
607607
mir.span,
608608
mir,
609-
Some(Lvalue::zst()),
609+
Lvalue::zst(),
610610
StackPopCleanup::Goto(dest_block),
611611
)?;
612612

0 commit comments

Comments
 (0)