about summary refs log tree commit diff
path: root/src/libstd/rt.rs
diff options
context:
space:
mode:
authorBastian Köcher <git@kchr.de>2017-12-03 22:16:24 +0100
committerBastian Köcher <git@kchr.de>2017-12-26 12:26:39 +0100
commitd7918fb2e889c5ccb58ab8b97d2581cc763f2306 (patch)
treea25c4ba1da52089945ce84e9ec8e1b51f45cfd8f /src/libstd/rt.rs
parent8cdde6db7138cf2365dd9ceb5b8814e92a922ed4 (diff)
downloadrust-d7918fb2e889c5ccb58ab8b97d2581cc763f2306.tar.gz
rust-d7918fb2e889c5ccb58ab8b97d2581cc763f2306.zip
Implements RFC 1937: `?` in `main`
This is the first part of the RFC 1937 that supports new
`Termination` trait in the rust `main` function.
Diffstat (limited to 'src/libstd/rt.rs')
-rw-r--r--src/libstd/rt.rs45
1 files changed, 44 insertions, 1 deletions
diff --git a/src/libstd/rt.rs b/src/libstd/rt.rs
index 40b24cedcdc..2b75201ad2b 100644
--- a/src/libstd/rt.rs
+++ b/src/libstd/rt.rs
@@ -26,7 +26,50 @@
 // 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))]
+#[cfg(not(any(test, stage0)))]
+#[lang = "start"]
+fn lang_start<T: ::termination::Termination + 'static>
+    (main: fn() -> T, argc: isize, argv: *const *const u8) -> !
+{
+    use panic;
+    use sys;
+    use sys_common;
+    use sys_common::thread_info;
+    use thread::Thread;
+    use process;
+    #[cfg(not(feature = "backtrace"))]
+    use mem;
+
+    sys::init();
+
+    process::exit(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().report())
+        });
+        #[cfg(not(feature = "backtrace"))]
+        let exit_code = panic::catch_unwind(mem::transmute::<_, fn()>(main).report());
+
+        sys_common::cleanup();
+        exit_code.unwrap_or(101)
+    });
+}
+
+#[cfg(all(not(test), stage0))]
 #[lang = "start"]
 fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize {
     use panic;