diff options
| author | bors <bors@rust-lang.org> | 2017-12-27 15:41:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-12-27 15:41:51 +0000 |
| commit | bfbb1f5ce1e6a85a03219767cfc5c9bab3f7bf9e (patch) | |
| tree | b72634f6bee5eb0e51d428fe53ecd1c28c11b907 /src/libstd | |
| parent | 63efff5a71269ae22b8769e2e6f9aaa2f940f823 (diff) | |
| parent | 09f94bea4aca7696b58dd7e6668ca27d71908984 (diff) | |
| download | rust-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')
| -rw-r--r-- | src/libstd/lib.rs | 6 | ||||
| -rw-r--r-- | src/libstd/rt.rs | 52 | ||||
| -rw-r--r-- | src/libstd/sys_common/backtrace.rs | 2 | ||||
| -rw-r--r-- | src/libstd/termination.rs | 86 |
4 files changed, 144 insertions, 2 deletions
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 12e6231136e..171c108e3aa 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -308,6 +308,7 @@ #![feature(str_char)] #![feature(str_internals)] #![feature(str_utf16)] +#![feature(termination_trait)] #![feature(test, rustc_private)] #![feature(thread_local)] #![feature(toowned_clone_into)] @@ -499,6 +500,11 @@ mod memchr; // The runtime entry point and a few unstable public functions used by the // compiler pub mod rt; +// The trait to support returning arbitrary types in the main function +mod termination; + +#[unstable(feature = "termination_trait", issue = "43301")] +pub use self::termination::Termination; // Include a number of private modules that exist solely to provide // the rustdoc documentation for primitive types. Using `include!` 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; diff --git a/src/libstd/sys_common/backtrace.rs b/src/libstd/sys_common/backtrace.rs index b5cf6d7d34f..36cbce2df75 100644 --- a/src/libstd/sys_common/backtrace.rs +++ b/src/libstd/sys_common/backtrace.rs @@ -128,7 +128,7 @@ fn filter_frames(frames: &[Frame], /// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. #[inline(never)] pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T - where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static + where F: FnOnce() -> T, F: Send, T: Send { f() } diff --git a/src/libstd/termination.rs b/src/libstd/termination.rs new file mode 100644 index 00000000000..61137ba4922 --- /dev/null +++ b/src/libstd/termination.rs @@ -0,0 +1,86 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use error::Error; +#[cfg(target_arch = "wasm32")] +mod exit { + pub const SUCCESS: i32 = 0; + pub const FAILURE: i32 = 1; +} +#[cfg(not(target_arch = "wasm32"))] +mod exit { + use libc; + pub const SUCCESS: i32 = libc::EXIT_SUCCESS; + pub const FAILURE: i32 = libc::EXIT_FAILURE; +} + +/// A trait for implementing arbitrary return types in the `main` function. +/// +/// The c-main function only supports to return integers as return type. +/// So, every type implementing the `Termination` trait has to be converted +/// to an integer. +/// +/// The default implementations are returning `libc::EXIT_SUCCESS` to indicate +/// a successful execution. In case of a failure, `libc::EXIT_FAILURE` is returned. +#[cfg_attr(not(any(stage0, test)), lang = "termination")] +#[unstable(feature = "termination_trait", issue = "43301")] +#[rustc_on_unimplemented = + "`main` can only return types that implement {Termination}, not `{Self}`"] +pub trait Termination { + /// Is called to get the representation of the value as status code. + /// This status code is returned to the operating system. + fn report(self) -> i32; +} + +#[unstable(feature = "termination_trait", issue = "43301")] +impl Termination for () { + fn report(self) -> i32 { exit::SUCCESS } +} + +#[unstable(feature = "termination_trait", issue = "43301")] +impl<T: Termination, E: Error> Termination for Result<T, E> { + fn report(self) -> i32 { + match self { + Ok(val) => val.report(), + Err(err) => { + print_error(err); + exit::FAILURE + } + } + } +} + +#[unstable(feature = "termination_trait", issue = "43301")] +fn print_error<E: Error>(err: E) { + eprintln!("Error: {}", err.description()); + + if let Some(ref err) = err.cause() { + eprintln!("Caused by: {}", err.description()); + } +} + +#[unstable(feature = "termination_trait", issue = "43301")] +impl Termination for ! { + fn report(self) -> i32 { unreachable!(); } +} + +#[unstable(feature = "termination_trait", issue = "43301")] +impl Termination for bool { + fn report(self) -> i32 { + if self { exit::SUCCESS } else { exit::FAILURE } + } +} + +#[unstable(feature = "termination_trait", issue = "43301")] +impl Termination for i32 { + fn report(self) -> i32 { + self + } +} |
