about summary refs log tree commit diff
path: root/src/libstd/rt.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-27 15:41:51 +0000
committerbors <bors@rust-lang.org>2017-12-27 15:41:51 +0000
commitbfbb1f5ce1e6a85a03219767cfc5c9bab3f7bf9e (patch)
treeb72634f6bee5eb0e51d428fe53ecd1c28c11b907 /src/libstd/rt.rs
parent63efff5a71269ae22b8769e2e6f9aaa2f940f823 (diff)
parent09f94bea4aca7696b58dd7e6668ca27d71908984 (diff)
downloadrust-bfbb1f5ce1e6a85a03219767cfc5c9bab3f7bf9e.tar.gz
rust-bfbb1f5ce1e6a85a03219767cfc5c9bab3f7bf9e.zip
Auto merge of #46479 - bkchr:termination_trait, r=arielb1
Implements RFC 1937: `?` in `main`

This is the first part of the RFC 1937 that supports new
`Termination` trait in the rust `main` function.

Thanks @nikomatsakis, @arielb1 and all other people in the gitter channel for all your help!

The support for doctest and `#[test]` is still missing, bu as @nikomatsakis said, smaller pull requests are better :)
Diffstat (limited to 'src/libstd/rt.rs')
-rw-r--r--src/libstd/rt.rs52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/libstd/rt.rs b/src/libstd/rt.rs
index 40b24cedcdc..e2c1bba50c4 100644
--- a/src/libstd/rt.rs
+++ b/src/libstd/rt.rs
@@ -26,7 +26,57 @@
 // Reexport some of our utilities which are expected by other crates.
 pub use panicking::{begin_panic, begin_panic_fmt, update_panic_count};
 
-#[cfg(not(test))]
+// To reduce the generated code of the new `lang_start`, this function is doing
+// the real work.
+#[cfg(not(any(test, stage0)))]
+fn lang_start_internal(main: &(Fn() -> i32 + Sync + ::panic::RefUnwindSafe),
+                       argc: isize, argv: *const *const u8) -> isize {
+    use panic;
+    use sys;
+    use sys_common;
+    use sys_common::thread_info;
+    use thread::Thread;
+    #[cfg(not(feature = "backtrace"))]
+    use mem;
+
+    sys::init();
+
+    unsafe {
+        let main_guard = sys::thread::guard::init();
+        sys::stack_overflow::init();
+
+        // Next, set up the current Thread with the guard information we just
+        // created. Note that this isn't necessary in general for new threads,
+        // but we just do this to name the main thread and to give it correct
+        // info about the stack bounds.
+        let thread = Thread::new(Some("main".to_owned()));
+        thread_info::set(main_guard, thread);
+
+        // Store our args if necessary in a squirreled away location
+        sys::args::init(argc, argv);
+
+        // Let's run some code!
+        #[cfg(feature = "backtrace")]
+        let exit_code = panic::catch_unwind(|| {
+            ::sys_common::backtrace::__rust_begin_short_backtrace(move || main())
+        });
+        #[cfg(not(feature = "backtrace"))]
+        let exit_code = panic::catch_unwind(move || main());
+
+        sys_common::cleanup();
+        exit_code.unwrap_or(101) as isize
+    }
+}
+
+#[cfg(not(any(test, stage0)))]
+#[lang = "start"]
+fn lang_start<T: ::termination::Termination + 'static>
+    (main: fn() -> T, argc: isize, argv: *const *const u8) -> isize
+{
+    lang_start_internal(&move || main().report(), argc, argv)
+}
+
+#[cfg(all(not(test), stage0))]
 #[lang = "start"]
 fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize {
     use panic;