Skip to content

Commit ca0af71

Browse files
committed
various cleanups based on review
1 parent 5783e73 commit ca0af71

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

compiler/rustc_const_eval/src/interpret/call.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
502502
// Don't forget to mark "initially live" locals as live.
503503
self.storage_live_for_always_live_locals()?;
504504
};
505-
match res {
506-
Err(err) => {
507-
// Don't show the incomplete stack frame in the error stacktrace.
508-
self.stack_mut().pop();
509-
Err(err)
510-
}
511-
Ok(()) => Ok(()),
512-
}
505+
res.inspect_err(|_| {
506+
// Don't show the incomplete stack frame in the error stacktrace.
507+
self.stack_mut().pop();
508+
})
513509
}
514510

515511
/// Initiate a call to this function -- pushing the stack frame and initializing the arguments.
@@ -907,7 +903,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
907903
.local_to_op(mir::RETURN_PLACE, None)
908904
.expect("return place should always be live");
909905
let dest = self.frame().return_place.clone();
910-
let err = if self.stack().len() == 1 {
906+
let res = if self.stack().len() == 1 {
911907
// The initializer of constants and statics will get validated separately
912908
// after the constant has been fully evaluated. While we could fall back to the default
913909
// code path, that will cause -Zenforce-validity to cycle on static initializers.
@@ -924,7 +920,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
924920
// We delay actually short-circuiting on this error until *after* the stack frame is
925921
// popped, since we want this error to be attributed to the caller, whose type defines
926922
// this transmute.
927-
err
923+
res
928924
} else {
929925
Ok(())
930926
};
@@ -953,22 +949,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
953949
// Normal return, figure out where to jump.
954950
if unwinding {
955951
// Follow the unwind edge.
956-
let unwind = match stack_pop_info.return_to_block {
957-
StackPopCleanup::Goto { unwind, .. } => unwind,
952+
match stack_pop_info.return_to_block {
953+
StackPopCleanup::Goto { unwind, .. } => {
954+
// This must be the very last thing that happens, since it can in fact push a new stack frame.
955+
self.unwind_to_block(unwind)
956+
}
958957
StackPopCleanup::Root { .. } => {
959958
panic!("encountered StackPopCleanup::Root when unwinding!")
960959
}
961-
};
962-
// This must be the very last thing that happens, since it can in fact push a new stack frame.
963-
self.unwind_to_block(unwind)
960+
}
964961
} else {
965962
// Follow the normal return edge.
966963
match stack_pop_info.return_to_block {
967964
StackPopCleanup::Goto { ret, .. } => self.return_to_block(ret),
968965
StackPopCleanup::Root { .. } => {
969966
assert!(
970967
self.stack().is_empty(),
971-
"only the topmost frame can have StackPopCleanup::Root"
968+
"only the bottommost frame can have StackPopCleanup::Root"
972969
);
973970
Ok(())
974971
}

compiler/rustc_const_eval/src/interpret/stack.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'tcx, Prov: Provenance, Extra> Frame<'tcx, Prov, Extra> {
264264
/// this frame (can happen e.g. during frame initialization, and during unwinding on
265265
/// frames without cleanup code).
266266
///
267-
/// Used by priroda.
267+
/// Used by [priroda](https://github.com/oli-obk/priroda).
268268
pub fn current_loc(&self) -> Either<mir::Location, Span> {
269269
self.loc
270270
}
@@ -340,6 +340,8 @@ impl<'tcx, Prov: Provenance, Extra> Frame<'tcx, Prov, Extra> {
340340
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
341341
/// Very low-level helper that pushes a stack frame without initializing
342342
/// the arguments or local variables.
343+
///
344+
/// The high-level version of this is `init_stack_frame`.
343345
#[instrument(skip(self, body, return_place, return_to_block), level = "debug")]
344346
pub(crate) fn push_stack_frame_raw(
345347
&mut self,
@@ -392,13 +394,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
392394
Ok(())
393395
}
394396

395-
/// Pops a stack frame from the stack and returns some information about it.
397+
/// Low-level helper that pops a stack frame from the stack and returns some information about
398+
/// it.
396399
///
397400
/// This also deallocates locals, if necessary.
398401
///
399402
/// [`M::before_stack_pop`] should be called before calling this function.
400403
/// [`M::after_stack_pop`] is called by this function automatically.
401404
///
405+
/// The high-level version of this is `return_from_current_stack_frame`.
406+
///
402407
/// [`M::before_stack_pop`]: Machine::before_stack_pop
403408
/// [`M::after_stack_pop`]: Machine::after_stack_pop
404409
pub(super) fn pop_stack_frame_raw(

compiler/rustc_const_eval/src/interpret/step.rs

+33-36
Original file line numberDiff line numberDiff line change
@@ -369,44 +369,38 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
369369
}
370370

371371
/// Evaluate the arguments of a function call
372-
fn eval_fn_call_arguments(
372+
fn eval_fn_call_argument(
373373
&self,
374-
ops: &[Spanned<mir::Operand<'tcx>>],
375-
) -> InterpResult<'tcx, Vec<FnArg<'tcx, M::Provenance>>> {
376-
ops.iter()
377-
.map(|op| {
378-
let arg = match &op.node {
379-
mir::Operand::Copy(_) | mir::Operand::Constant(_) => {
380-
// Make a regular copy.
381-
let op = self.eval_operand(&op.node, None)?;
374+
op: &mir::Operand<'tcx>,
375+
) -> InterpResult<'tcx, FnArg<'tcx, M::Provenance>> {
376+
Ok(match op {
377+
mir::Operand::Copy(_) | mir::Operand::Constant(_) => {
378+
// Make a regular copy.
379+
let op = self.eval_operand(op, None)?;
380+
FnArg::Copy(op)
381+
}
382+
mir::Operand::Move(place) => {
383+
// If this place lives in memory, preserve its location.
384+
// We call `place_to_op` which will be an `MPlaceTy` whenever there exists
385+
// an mplace for this place. (This is in contrast to `PlaceTy::as_mplace_or_local`
386+
// which can return a local even if that has an mplace.)
387+
let place = self.eval_place(*place)?;
388+
let op = self.place_to_op(&place)?;
389+
390+
match op.as_mplace_or_imm() {
391+
Either::Left(mplace) => FnArg::InPlace(mplace),
392+
Either::Right(_imm) => {
393+
// This argument doesn't live in memory, so there's no place
394+
// to make inaccessible during the call.
395+
// We rely on there not being any stray `PlaceTy` that would let the
396+
// caller directly access this local!
397+
// This is also crucial for tail calls, where we want the `FnArg` to
398+
// stay valid when the old stack frame gets popped.
382399
FnArg::Copy(op)
383400
}
384-
mir::Operand::Move(place) => {
385-
// If this place lives in memory, preserve its location.
386-
// We call `place_to_op` which will be an `MPlaceTy` whenever there exists
387-
// an mplace for this place. (This is in contrast to `PlaceTy::as_mplace_or_local`
388-
// which can return a local even if that has an mplace.)
389-
let place = self.eval_place(*place)?;
390-
let op = self.place_to_op(&place)?;
391-
392-
match op.as_mplace_or_imm() {
393-
Either::Left(mplace) => FnArg::InPlace(mplace),
394-
Either::Right(_imm) => {
395-
// This argument doesn't live in memory, so there's no place
396-
// to make inaccessible during the call.
397-
// We rely on there not being any stray `PlaceTy` that would let the
398-
// caller directly access this local!
399-
// This is also crucial for tail calls, where we want the `FnArg` to
400-
// stay valid when the old stack frame gets popped.
401-
FnArg::Copy(op)
402-
}
403-
}
404-
}
405-
};
406-
407-
Ok(arg)
408-
})
409-
.collect()
401+
}
402+
}
403+
})
410404
}
411405

412406
/// Shared part of `Call` and `TailCall` implementation — finding and evaluating all the
@@ -418,7 +412,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
418412
args: &[Spanned<mir::Operand<'tcx>>],
419413
) -> InterpResult<'tcx, EvaluatedCalleeAndArgs<'tcx, M>> {
420414
let func = self.eval_operand(func, None)?;
421-
let args = self.eval_fn_call_arguments(args)?;
415+
let args = args
416+
.iter()
417+
.map(|arg| self.eval_fn_call_argument(&arg.node))
418+
.collect::<InterpResult<'tcx, Vec<_>>>()?;
422419

423420
let fn_sig_binder = func.layout.ty.fn_sig(*self.tcx);
424421
let fn_sig = self.tcx.normalize_erasing_late_bound_regions(self.param_env, fn_sig_binder);

0 commit comments

Comments
 (0)