Skip to content

Commit 2681f25

Browse files
committed
Auto merge of #93442 - yaahc:Termination-abstraction, r=Mark-Simulacrum
Change Termination::report return type to ExitCode Related to #43301 The goal of this change is to minimize the forward compatibility risks in stabilizing Termination. By using the opaque type `ExitCode` instead of an `i32` we leave room for us to evolve the API over time to provide what cross-platform consistency we can / minimize footguns when working with exit codes, where as stabilizing on `i32` would limit what changes we could make in the future in how we represent and construct exit codes.
2 parents ad88831 + 19db85d commit 2681f25

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

library/std/src/process.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,21 @@ impl ExitCode {
16761676
pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE);
16771677
}
16781678

1679+
impl ExitCode {
1680+
// This should not be stabilized when stabilizing ExitCode, we don't know that i32 will serve
1681+
// all usecases, for example windows seems to use u32, unix uses the 8-15th bits of an i32, we
1682+
// likely want to isolate users anything that could restrict the platform specific
1683+
// representation of an ExitCode
1684+
//
1685+
// More info: https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426
1686+
/// Convert an ExitCode into an i32
1687+
#[unstable(feature = "process_exitcode_placeholder", issue = "48711")]
1688+
#[inline]
1689+
pub fn to_i32(self) -> i32 {
1690+
self.0.as_i32()
1691+
}
1692+
}
1693+
16791694
impl Child {
16801695
/// Forces the child process to exit. If the child has already exited, an [`InvalidInput`]
16811696
/// error is returned.
@@ -2016,20 +2031,20 @@ pub fn id() -> u32 {
20162031
pub trait Termination {
20172032
/// Is called to get the representation of the value as status code.
20182033
/// This status code is returned to the operating system.
2019-
fn report(self) -> i32;
2034+
fn report(self) -> ExitCode;
20202035
}
20212036

20222037
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20232038
impl Termination for () {
20242039
#[inline]
2025-
fn report(self) -> i32 {
2040+
fn report(self) -> ExitCode {
20262041
ExitCode::SUCCESS.report()
20272042
}
20282043
}
20292044

20302045
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20312046
impl<E: fmt::Debug> Termination for Result<(), E> {
2032-
fn report(self) -> i32 {
2047+
fn report(self) -> ExitCode {
20332048
match self {
20342049
Ok(()) => ().report(),
20352050
Err(err) => Err::<!, _>(err).report(),
@@ -2039,14 +2054,14 @@ impl<E: fmt::Debug> Termination for Result<(), E> {
20392054

20402055
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20412056
impl Termination for ! {
2042-
fn report(self) -> i32 {
2057+
fn report(self) -> ExitCode {
20432058
self
20442059
}
20452060
}
20462061

20472062
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20482063
impl<E: fmt::Debug> Termination for Result<!, E> {
2049-
fn report(self) -> i32 {
2064+
fn report(self) -> ExitCode {
20502065
let Err(err) = self;
20512066
eprintln!("Error: {:?}", err);
20522067
ExitCode::FAILURE.report()
@@ -2055,7 +2070,7 @@ impl<E: fmt::Debug> Termination for Result<!, E> {
20552070

20562071
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20572072
impl<E: fmt::Debug> Termination for Result<Infallible, E> {
2058-
fn report(self) -> i32 {
2073+
fn report(self) -> ExitCode {
20592074
let Err(err) = self;
20602075
Err::<!, _>(err).report()
20612076
}
@@ -2064,7 +2079,7 @@ impl<E: fmt::Debug> Termination for Result<Infallible, E> {
20642079
#[unstable(feature = "termination_trait_lib", issue = "43301")]
20652080
impl Termination for ExitCode {
20662081
#[inline]
2067-
fn report(self) -> i32 {
2068-
self.0.as_i32()
2082+
fn report(self) -> ExitCode {
2083+
self
20692084
}
20702085
}

library/std/src/rt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn lang_start<T: crate::process::Termination + 'static>(
142142
argv: *const *const u8,
143143
) -> isize {
144144
let Ok(v) = lang_start_internal(
145-
&move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report(),
145+
&move || crate::sys_common::backtrace::__rust_begin_short_backtrace(main).report().to_i32(),
146146
argc,
147147
argv,
148148
);

library/test/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(internal_output_capture)]
2121
#![feature(staged_api)]
2222
#![feature(termination_trait_lib)]
23+
#![feature(process_exitcode_placeholder)]
2324
#![feature(test)]
2425
#![feature(total_cmp)]
2526

@@ -182,7 +183,7 @@ fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
182183
/// Tests is considered a failure. By default, invokes `report()`
183184
/// and checks for a `0` result.
184185
pub fn assert_test_result<T: Termination>(result: T) {
185-
let code = result.report();
186+
let code = result.report().to_i32();
186187
assert_eq!(
187188
code, 0,
188189
"the test returned a termination value with a non-zero status code ({}) \

0 commit comments

Comments
 (0)