From 91ffbc43b18842594adb997c8eea8c51035bf0e1 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Fri, 28 Jan 2022 12:53:36 -0800 Subject: [PATCH 1/2] Change Termination::report return type to ExitCode --- library/std/src/process.rs | 30 ++++++++++++++++++++++-------- library/std/src/rt.rs | 2 +- library/test/src/lib.rs | 3 ++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index e012594dd4648..2a66ae03fae32 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -1676,6 +1676,20 @@ impl ExitCode { pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE); } +impl ExitCode { + // This should not be stabilized when stabilizing ExitCode, we don't know that i32 will serve + // all usecases, for example windows seems to use u32, unix uses the 8-15th bits of an i32, we + // likely want to isolate users anything that could restrict the platform specific + // representation of an ExitCode + // + // More info: https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426 + #[unstable(feature = "process_exitcode_placeholder", issue = "48711")] + /// Convert an ExitCode into an i32 + pub fn to_i32(self) -> i32 { + self.0.as_i32() + } +} + impl Child { /// Forces the child process to exit. If the child has already exited, an [`InvalidInput`] /// error is returned. @@ -2016,20 +2030,20 @@ pub fn id() -> u32 { 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; + fn report(self) -> ExitCode; } #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for () { #[inline] - fn report(self) -> i32 { + fn report(self) -> ExitCode { ExitCode::SUCCESS.report() } } #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for Result<(), E> { - fn report(self) -> i32 { + fn report(self) -> ExitCode { match self { Ok(()) => ().report(), Err(err) => Err::(err).report(), @@ -2039,14 +2053,14 @@ impl Termination for Result<(), E> { #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for ! { - fn report(self) -> i32 { + fn report(self) -> ExitCode { self } } #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for Result { - fn report(self) -> i32 { + fn report(self) -> ExitCode { let Err(err) = self; eprintln!("Error: {:?}", err); ExitCode::FAILURE.report() @@ -2055,7 +2069,7 @@ impl Termination for Result { #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for Result { - fn report(self) -> i32 { + fn report(self) -> ExitCode { let Err(err) = self; Err::(err).report() } @@ -2064,7 +2078,7 @@ impl Termination for Result { #[unstable(feature = "termination_trait_lib", issue = "43301")] impl Termination for ExitCode { #[inline] - fn report(self) -> i32 { - self.0.as_i32() + fn report(self) -> ExitCode { + self } } diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index 08e58257572b2..663537a05fa32 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -142,7 +142,7 @@ fn lang_start( argv: *const *const u8, ) -> isize { let Ok(v) = lang_start_internal( - &move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report(), + &move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report().to_i32(), argc, argv, ); diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index fad83094cdf8a..8fc2b4ed748c2 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -20,6 +20,7 @@ #![feature(internal_output_capture)] #![feature(staged_api)] #![feature(termination_trait_lib)] +#![feature(process_exitcode_placeholder)] #![feature(test)] #![feature(total_cmp)] @@ -182,7 +183,7 @@ fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn { /// Tests is considered a failure. By default, invokes `report()` /// and checks for a `0` result. pub fn assert_test_result(result: T) { - let code = result.report(); + let code = result.report().to_i32(); assert_eq!( code, 0, "the test returned a termination value with a non-zero status code ({}) \ From 19db85d6cdd6eade43461dfa75318215c720444b Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 31 Jan 2022 11:57:17 -0800 Subject: [PATCH 2/2] add inline attribute to new method --- library/std/src/process.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 2a66ae03fae32..7d7e08a714938 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -1683,8 +1683,9 @@ impl ExitCode { // representation of an ExitCode // // More info: https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426 - #[unstable(feature = "process_exitcode_placeholder", issue = "48711")] /// Convert an ExitCode into an i32 + #[unstable(feature = "process_exitcode_placeholder", issue = "48711")] + #[inline] pub fn to_i32(self) -> i32 { self.0.as_i32() }