]> git.lizzy.rs Git - rust.git/commitdiff
call_function: make the unit-return-type case more convenient
authorRalf Jung <post@ralfj.de>
Mon, 4 Jul 2022 17:46:11 +0000 (13:46 -0400)
committerRalf Jung <post@ralfj.de>
Mon, 4 Jul 2022 17:46:11 +0000 (13:46 -0400)
src/eval.rs
src/helpers.rs
src/shims/panic.rs
src/shims/tls.rs
src/shims/unix/thread.rs

index 1536b826ac46647ad4e2b1bd6c40d2713c310fff..d75b4f5fa6d2ab04ca5bf3c3d01651266b944335 100644 (file)
@@ -289,7 +289,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
                 start_instance,
                 Abi::Rust,
                 &[Scalar::from_pointer(main_ptr, &ecx).into(), argc.into(), argv],
-                &ret_place.into(),
+                Some(&ret_place.into()),
                 StackPopCleanup::Root { cleanup: true },
             )?;
         }
@@ -298,7 +298,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
                 entry_instance,
                 Abi::Rust,
                 &[argc.into(), argv],
-                &ret_place.into(),
+                Some(&ret_place.into()),
                 StackPopCleanup::Root { cleanup: true },
             )?;
         }
index 86823f28178871e4537aed72099f717731467fd2..c051d44fa25617eb01c167c29fc7cec37279ac74 100644 (file)
@@ -235,12 +235,15 @@ fn gen_random(&mut self, ptr: Pointer<Option<Tag>>, len: u64) -> InterpResult<'t
 
     /// Call a function: Push the stack frame and pass the arguments.
     /// For now, arguments must be scalars (so that the caller does not have to know the layout).
+    ///
+    /// If you do not provie a return place, a dangling zero-sized place will be created
+    /// for your convenience.
     fn call_function(
         &mut self,
         f: ty::Instance<'tcx>,
         caller_abi: Abi,
         args: &[Immediate<Tag>],
-        dest: &PlaceTy<'tcx, Tag>,
+        dest: Option<&PlaceTy<'tcx, Tag>>,
         stack_pop: StackPopCleanup,
     ) -> InterpResult<'tcx> {
         let this = self.eval_context_mut();
@@ -256,7 +259,11 @@ fn call_function(
 
         // Push frame.
         let mir = this.load_mir(f.def, None)?;
-        this.push_stack_frame(f, mir, dest, stack_pop)?;
+        let dest = match dest {
+            Some(dest) => *dest,
+            None => MPlaceTy::dangling(this.layout_of(mir.return_ty())?).into(),
+        };
+        this.push_stack_frame(f, mir, &dest, stack_pop)?;
 
         // Initialize arguments.
         let mut callee_args = this.frame().body.args_iter();
index 2ef0a741d52fb53c41d00e488319315a7dfac710..c356dd86676da67e5d3c0db63b74484f1c2205f3 100644 (file)
@@ -91,12 +91,11 @@ fn handle_try(
         // Now we make a function call, and pass `data` as first and only argument.
         let f_instance = this.get_ptr_fn(try_fn)?.as_instance()?;
         trace!("try_fn: {:?}", f_instance);
-        let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
         this.call_function(
             f_instance,
             Abi::Rust,
             &[data.into()],
-            &ret_place,
+            None,
             // Directly return to caller.
             StackPopCleanup::Goto { ret: Some(ret), unwind: StackPopUnwind::Skip },
         )?;
@@ -144,12 +143,11 @@ fn handle_stack_pop_unwind(
             let f_instance =
                 this.get_ptr_fn(this.scalar_to_ptr(catch_unwind.catch_fn)?)?.as_instance()?;
             trace!("catch_fn: {:?}", f_instance);
-            let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
             this.call_function(
                 f_instance,
                 Abi::Rust,
                 &[catch_unwind.data.into(), payload.into()],
-                &ret_place,
+                None,
                 // Directly return to caller of `try`.
                 StackPopCleanup::Goto { ret: Some(catch_unwind.ret), unwind: StackPopUnwind::Skip },
             )?;
@@ -175,7 +173,7 @@ fn start_panic(&mut self, msg: &str, unwind: StackPopUnwind) -> InterpResult<'tc
             panic,
             Abi::Rust,
             &[msg.to_ref(this)],
-            &MPlaceTy::dangling(this.machine.layouts.unit).into(),
+            None,
             StackPopCleanup::Goto { ret: None, unwind },
         )
     }
@@ -204,7 +202,7 @@ fn assert_panic(
                     panic_bounds_check,
                     Abi::Rust,
                     &[index.into(), len.into()],
-                    &MPlaceTy::dangling(this.machine.layouts.unit).into(),
+                    None,
                     StackPopCleanup::Goto {
                         ret: None,
                         unwind: match unwind {
index 6b4e9d4f753376cd7d1d274194acbc7e665680b7..5a72c872b04d067504749a048d54a3eda432cd1a 100644 (file)
@@ -253,12 +253,11 @@ fn schedule_windows_tls_dtors(&mut self) -> InterpResult<'tcx> {
 
         // The signature of this function is `unsafe extern "system" fn(h: c::LPVOID, dwReason: c::DWORD, pv: c::LPVOID)`.
         let reason = this.eval_path_scalar(&["std", "sys", "windows", "c", "DLL_THREAD_DETACH"])?;
-        let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
         this.call_function(
             thread_callback,
             Abi::System { unwind: false },
             &[Scalar::null_ptr(this).into(), reason.into(), Scalar::null_ptr(this).into()],
-            &ret_place,
+            None,
             StackPopCleanup::Root { cleanup: true },
         )?;
 
@@ -276,12 +275,11 @@ fn schedule_macos_tls_dtor(&mut self) -> InterpResult<'tcx, bool> {
         if let Some((instance, data)) = this.machine.tls.macos_thread_dtors.remove(&thread_id) {
             trace!("Running macos dtor {:?} on {:?} at {:?}", instance, data, thread_id);
 
-            let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
             this.call_function(
                 instance,
                 Abi::C { unwind: false },
                 &[data.into()],
-                &ret_place,
+                None,
                 StackPopCleanup::Root { cleanup: true },
             )?;
 
@@ -319,12 +317,11 @@ fn schedule_next_pthread_tls_dtor(&mut self) -> InterpResult<'tcx, bool> {
                 "data can't be NULL when dtor is called!"
             );
 
-            let ret_place = MPlaceTy::dangling(this.machine.layouts.unit).into();
             this.call_function(
                 instance,
                 Abi::C { unwind: false },
                 &[ptr.into()],
-                &ret_place,
+                None,
                 StackPopCleanup::Root { cleanup: true },
             )?;
 
index 63b9f36d6ffa4edbdd7b3ceba177217de8c6d734..8dc5f81354a32b9bfee25023134bb78b9b25adea 100644 (file)
@@ -47,7 +47,7 @@ fn pthread_create(
             instance,
             Abi::C { unwind: false },
             &[*func_arg],
-            &ret_place.into(),
+            Some(&ret_place.into()),
             StackPopCleanup::Root { cleanup: true },
         )?;