Skip to content

Commit 709e29a

Browse files
committed
Auto merge of #2327 - RalfJung:unit-return, r=RalfJung
call_function: make the unit-return-type case more convenient
2 parents f338e62 + 22aa7f9 commit 709e29a

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

src/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
289289
start_instance,
290290
Abi::Rust,
291291
&[Scalar::from_pointer(main_ptr, &ecx).into(), argc.into(), argv],
292-
&ret_place.into(),
292+
Some(&ret_place.into()),
293293
StackPopCleanup::Root { cleanup: true },
294294
)?;
295295
}
@@ -298,7 +298,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
298298
entry_instance,
299299
Abi::Rust,
300300
&[argc.into(), argv],
301-
&ret_place.into(),
301+
Some(&ret_place.into()),
302302
StackPopCleanup::Root { cleanup: true },
303303
)?;
304304
}

src/helpers.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
235235

236236
/// Call a function: Push the stack frame and pass the arguments.
237237
/// For now, arguments must be scalars (so that the caller does not have to know the layout).
238+
///
239+
/// If you do not provie a return place, a dangling zero-sized place will be created
240+
/// for your convenience.
238241
fn call_function(
239242
&mut self,
240243
f: ty::Instance<'tcx>,
241244
caller_abi: Abi,
242245
args: &[Immediate<Tag>],
243-
dest: &PlaceTy<'tcx, Tag>,
246+
dest: Option<&PlaceTy<'tcx, Tag>>,
244247
stack_pop: StackPopCleanup,
245248
) -> InterpResult<'tcx> {
246249
let this = self.eval_context_mut();
@@ -256,7 +259,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
256259

257260
// Push frame.
258261
let mir = this.load_mir(f.def, None)?;
259-
this.push_stack_frame(f, mir, dest, stack_pop)?;
262+
let dest = match dest {
263+
Some(dest) => *dest,
264+
None => MPlaceTy::dangling(this.layout_of(mir.return_ty())?).into(),
265+
};
266+
this.push_stack_frame(f, mir, &dest, stack_pop)?;
260267

261268
// Initialize arguments.
262269
let mut callee_args = this.frame().body.args_iter();

src/shims/panic.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9191
// Now we make a function call, and pass `data` as first and only argument.
9292
let f_instance = this.get_ptr_fn(try_fn)?.as_instance()?;
9393
trace!("try_fn: {:?}", f_instance);
94-
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
9594
this.call_function(
9695
f_instance,
9796
Abi::Rust,
9897
&[data.into()],
99-
&ret_place,
98+
None,
10099
// Directly return to caller.
101100
StackPopCleanup::Goto { ret: Some(ret), unwind: StackPopUnwind::Skip },
102101
)?;
@@ -144,12 +143,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
144143
let f_instance =
145144
this.get_ptr_fn(this.scalar_to_ptr(catch_unwind.catch_fn)?)?.as_instance()?;
146145
trace!("catch_fn: {:?}", f_instance);
147-
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
148146
this.call_function(
149147
f_instance,
150148
Abi::Rust,
151149
&[catch_unwind.data.into(), payload.into()],
152-
&ret_place,
150+
None,
153151
// Directly return to caller of `try`.
154152
StackPopCleanup::Goto { ret: Some(catch_unwind.ret), unwind: StackPopUnwind::Skip },
155153
)?;
@@ -175,7 +173,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
175173
panic,
176174
Abi::Rust,
177175
&[msg.to_ref(this)],
178-
&MPlaceTy::dangling(this.machine.layouts.unit).into(),
176+
None,
179177
StackPopCleanup::Goto { ret: None, unwind },
180178
)
181179
}
@@ -204,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
204202
panic_bounds_check,
205203
Abi::Rust,
206204
&[index.into(), len.into()],
207-
&MPlaceTy::dangling(this.machine.layouts.unit).into(),
205+
None,
208206
StackPopCleanup::Goto {
209207
ret: None,
210208
unwind: match unwind {

src/shims/tls.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,11 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
253253

254254
// The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`.
255255
let reason = this.eval_path_scalar(&["std", "sys", "windows", "c", "DLL_THREAD_DETACH"])?;
256-
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
257256
this.call_function(
258257
thread_callback,
259258
Abi::System { unwind: false },
260259
&[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
261-
&ret_place,
260+
None,
262261
StackPopCleanup::Root { cleanup: true },
263262
)?;
264263

@@ -276,12 +275,11 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
276275
if let Some((instance, data)) = this.machine.tls.macos_thread_dtors.remove(&thread_id) {
277276
trace!("Running macos dtor {:?} on {:?} at {:?}", instance, data, thread_id);
278277

279-
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
280278
this.call_function(
281279
instance,
282280
Abi::C { unwind: false },
283281
&[data.into()],
284-
&ret_place,
282+
None,
285283
StackPopCleanup::Root { cleanup: true },
286284
)?;
287285

@@ -319,12 +317,11 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
319317
"data can't be NULL when dtor is called!"
320318
);
321319

322-
let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
323320
this.call_function(
324321
instance,
325322
Abi::C { unwind: false },
326323
&[ptr.into()],
327-
&ret_place,
324+
None,
328325
StackPopCleanup::Root { cleanup: true },
329326
)?;
330327

src/shims/unix/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4747
instance,
4848
Abi::C { unwind: false },
4949
&[*func_arg],
50-
&ret_place.into(),
50+
Some(&ret_place.into()),
5151
StackPopCleanup::Root { cleanup: true },
5252
)?;
5353

0 commit comments

Comments
 (0)