From 1db38c0dfc66c54cabce16e56fa16a8eb38366aa Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 22 Jan 2020 20:24:17 +0100 Subject: [PATCH] Re-introduce FnBox --- .../0015-Remove-usage-of-unsized-locals.patch | 47 +---- patches/0018-Add-FnBox-back.patch | 173 ++++++++++++++++++ 2 files changed, 178 insertions(+), 42 deletions(-) create mode 100644 patches/0018-Add-FnBox-back.patch diff --git a/patches/0015-Remove-usage-of-unsized-locals.patch b/patches/0015-Remove-usage-of-unsized-locals.patch index 02ecaa7a994..9c60ac55a9f 100644 --- a/patches/0015-Remove-usage-of-unsized-locals.patch +++ b/patches/0015-Remove-usage-of-unsized-locals.patch @@ -44,53 +44,16 @@ index f6dee7c..0c6a8c0 100644 #[unstable(feature = "coerce_unsized", issue = "27732")] impl, U: ?Sized> CoerceUnsized> for Box {} -diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs -index 1181b86..20f9251 100644 ---- a/src/libstd/sys_common/at_exit_imp.rs -+++ b/src/libstd/sys_common/at_exit_imp.rs -@@ -38,6 +38,7 @@ unsafe fn init() -> bool { - true - } - -+/* - pub fn cleanup() { - for i in 1..=ITERS { - unsafe { -@@ -60,6 +61,7 @@ pub fn cleanup() { - } - } - } -+*/ - - pub fn push(f: Box) -> bool { - unsafe { -diff --git a/src/libstd/sys_common/mod.rs b/src/libstd/sys_common/mod.rs -index 6260c3b..611ed7e 100644 ---- a/src/libstd/sys_common/mod.rs -+++ b/src/libstd/sys_common/mod.rs -@@ -127,7 +127,6 @@ pub fn cleanup() { - CLEANUP.call_once(|| unsafe { - sys::args::cleanup(); - sys::stack_overflow::cleanup(); -- at_exit_imp::cleanup(); - }); - } - diff --git a/src/libstd/sys_common/thread.rs b/src/libstd/sys_common/thread.rs index b2142e7..718bb1c 100644 --- a/src/libstd/sys_common/thread.rs +++ b/src/libstd/sys_common/thread.rs -@@ -6,12 +6,7 @@ use crate::sys::thread as imp; +@@ -6,7 +6,7 @@ pub unsafe fn start_thread(main: *mut u8) { + let _handler = stack_overflow::Handler::new(); - #[allow(dead_code)] - pub unsafe fn start_thread(main: *mut u8) { -- // Next, set up our stack overflow handler which may get triggered if we run -- // out of stack. -- let _handler = stack_overflow::Handler::new(); -- -- // Finally, let's run some code. + // Finally, let's run some code. - Box::from_raw(main as *mut Box)() -+ panic!("Threads are not yet supported, because cranelift doesn't support atomics."); ++ Box::from_raw(main as *mut Box)() } pub fn min_stack() -> usize { @@ -101,7 +64,7 @@ index f4a1783..362b537 100644 @@ -40,5 +40,7 @@ impl Thread { // unsafe: see thread::Builder::spawn_unchecked for safety requirements pub unsafe fn new(stack: usize, p: Box) -> io::Result { -+ panic!("Threads are not yet supported, because cranelift doesn't support atomics."); ++ panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics."); + let p = box p; let mut native: libc::pthread_t = mem::zeroed(); diff --git a/patches/0018-Add-FnBox-back.patch b/patches/0018-Add-FnBox-back.patch new file mode 100644 index 00000000000..e5616aa5a8b --- /dev/null +++ b/patches/0018-Add-FnBox-back.patch @@ -0,0 +1,173 @@ +From 8617310c3c9e192080df6a0c7b4835d3f02e27b9 Mon Sep 17 00:00:00 2001 +From: bjorn3 +Date: Fri, 1 Nov 2019 20:58:30 +0100 +Subject: [PATCH] Add FnBox back + +--- + src/liballoc/boxed.rs | 13 +++++++++++++ + src/libstd/prelude/v1.rs | 2 +- + src/libstd/sys/cloudabi/thread.rs | 2 +- + src/libstd/sys/hermit/thread.rs | 4 ++-- + src/libstd/sys/unix/thread.rs | 4 ++-- + src/libstd/sys_common/at_exit_imp.rs | 6 +++--- + src/libstd/sys_common/mod.rs | 2 +- + src/libstd/sys_common/thread.rs | 2 +- + src/libstd/thread/mod.rs | 2 +- + 9 files changed, 25 insertions(+), 12 deletions(-) + +diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs +index ef9b648..e32b870 100644 +--- a/src/liballoc/boxed.rs ++++ b/src/liballoc/boxed.rs +@@ -1079,3 +1079,16 @@ impl Future for Box { + F::poll(Pin::new(&mut *self), cx) + } + } ++ ++#[stable(feature = "rust1", since = "1.0.0")] ++pub trait FnBox: FnOnce { ++ #[stable(feature = "rust1", since = "1.0.0")] ++ extern "rust-call" fn call_box(self: Box, args: A) -> Self::Output; ++} ++ ++#[stable(feature = "rust1", since = "1.0.0")] ++impl> FnBox for F { ++ extern "rust-call" fn call_box(self: Box, args: A) -> Self::Output { ++ >::call_once(*self, args) ++ } ++} +diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs +index 3e4cf91..1f50eb3 100644 +--- a/src/libstd/prelude/v1.rs ++++ b/src/libstd/prelude/v1.rs +@@ -94,6 +94,6 @@ pub use core::prelude::v1::{ + #[stable(feature = "rust1", since = "1.0.0")] + #[doc(no_inline)] +-pub use crate::boxed::Box; ++pub use crate::boxed::{Box, FnBox}; + #[stable(feature = "rust1", since = "1.0.0")] + #[doc(no_inline)] + pub use crate::string::{String, ToString}; +diff --git a/src/libstd/sys/cloudabi/thread.rs b/src/libstd/sys/cloudabi/thread.rs +index 240b6ea..6f71c6b 100644 +--- a/src/libstd/sys/cloudabi/thread.rs ++++ b/src/libstd/sys/cloudabi/thread.rs +@@ -21,7 +21,7 @@ unsafe impl Sync for Thread {} + + impl Thread { + // unsafe: see thread::Builder::spawn_unchecked for safety requirements +- pub unsafe fn new(stack: usize, p: Box) -> io::Result { ++ pub unsafe fn new(stack: usize, p: Box) -> io::Result { + let p = box p; + let mut native: libc::pthread_t = mem::zeroed(); + let mut attr: libc::pthread_attr_t = mem::zeroed(); +diff --git a/src/libstd/sys/hermit/thread.rs b/src/libstd/sys/hermit/thread.rs +index 99a9c83..b8bc392 100644 +--- a/src/libstd/sys/hermit/thread.rs ++++ b/src/libstd/sys/hermit/thread.rs +@@ -44,9 +44,9 @@ unsafe impl Sync for Thread {} + pub const DEFAULT_MIN_STACK_SIZE: usize = 262144; + + impl Thread { + pub unsafe fn new_with_coreid( + _stack: usize, +- p: Box, ++ p: Box, + core_id: isize, + ) -> io::Result { + let p = box p; +@@ -67,7 +67,7 @@ impl Thread { + } + } + +- pub unsafe fn new(stack: usize, p: Box) -> io::Result { ++ pub unsafe fn new(stack: usize, p: Box) -> io::Result { + Thread::new_with_coreid(stack, p, -1 /* = no specific core */) + } + +diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs +index 143cf2f..a6e8faf 100644 +--- a/src/libstd/sys/unix/thread.rs ++++ b/src/libstd/sys/unix/thread.rs +@@ -38,8 +38,8 @@ unsafe fn pthread_attr_setstacksize(_attr: *mut libc::pthread_attr_t, + + impl Thread { + // unsafe: see thread::Builder::spawn_unchecked for safety requirements +- pub unsafe fn new(stack: usize, p: Box) -> io::Result { ++ pub unsafe fn new(stack: usize, p: Box) -> io::Result { + panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics."); + + let p = box p; + let mut native: libc::pthread_t = mem::zeroed(); +diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs +index cdb72ee..e523333 100644 +--- a/src/libstd/sys_common/at_exit_imp.rs ++++ b/src/libstd/sys_common/at_exit_imp.rs +@@ -6,7 +6,7 @@ use crate::mem; + use crate::ptr; + use crate::sys_common::mutex::Mutex; + +-type Queue = Vec>; ++type Queue = Vec>; + + // NB these are specifically not types from `std::sync` as they currently rely + // on poisoning and this module needs to operate at a lower level than requiring +@@ -53,14 +53,14 @@ pub fn cleanup() { + let queue: Box = Box::from_raw(queue); + for to_run in *queue { + // We are not holding any lock, so reentrancy is fine. +- to_run(); ++ to_run.call_box(()); + } + } + } + } + } + +-pub fn push(f: Box) -> bool { ++pub fn push(f: Box) -> bool { + unsafe { + let _guard = LOCK.lock(); + if init() { +diff --git a/src/libstd/sys_common/mod.rs b/src/libstd/sys_common/mod.rs +index 7a0bcd0..668bef2 100644 +--- a/src/libstd/sys_common/mod.rs ++++ b/src/libstd/sys_common/mod.rs +@@ -113,7 +113,7 @@ pub trait FromInner { + /// closure will be run once the main thread exits. Returns `Err` to indicate + /// that the closure could not be registered, meaning that it is not scheduled + /// to be run. +-pub fn at_exit(f: F) -> Result<(), ()> { ++pub fn at_exit(f: F) -> Result<(), ()> { + if at_exit_imp::push(Box::new(f)) { Ok(()) } else { Err(()) } + } + +diff --git a/src/libstd/sys_common/thread.rs b/src/libstd/sys_common/thread.rs +index c638be9..5c18a18 100644 +--- a/src/libstd/sys_common/thread.rs ++++ b/src/libstd/sys_common/thread.rs +@@ -10,7 +10,7 @@ pub unsafe fn start_thread(main: *mut u8) { + let _handler = stack_overflow::Handler::new(); + + // Finally, let's run some code. +- Box::from_raw(main as *mut Box)() ++ Box::from_raw(main as *mut Box).call_box(()) + } + + pub fn min_stack() -> usize { +diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs +index 0ffa6ac..4a3e3d6 100644 +--- a/src/libstd/thread/mod.rs ++++ b/src/libstd/thread/mod.rs +@@ -485,7 +485,7 @@ impl Builder { + // returning. + native: Some(imp::Thread::new( + stack_size, +- mem::transmute::, Box>(Box::new( ++ mem::transmute::, Box>(Box::new( + main, + )), + )?), +-- +2.20.1 +