Why does std::process::exit take an i32 when there is a dedicated ExitCode type

Seems a bit strange to me why instead of taking the ExitCode type it takes an i32? Could it be that they forgot to update the exit function as ExitCode was introduced way later than the exit fn. And should I open an issue about this?

1 Like

ExitCode got stabilized in 1.61, while exit was already part of 1.0.

1 Like

Changing the signature would be a breaking change and will not be accepted.

1 Like

Could this not be done via an edition? And if it can’t be done via an edition, then they could make a new exit function something like:

pub fn exit_with_code(code: ExitCode) {
    // …
}

The old exit function could then be deprecated which isn’t a breaking change (I think?)

It looks like you could use ExitCode::exit_process when that stabilizes

1 Like

ExitCode::exit_process() is already stable (I believe since 1.61, alongside ExitCode), so ExitCode::from(n).exit_process() covers the typed case without adding exit_with_code. Editions can’t change the signature of an existing std function, and deprecating exit would break a huge amount of existing code for little gain.

1 Like

However in theory editions can affect name resolution in such a way that crates in different editions see different functions.

Depends on how it was changed.

pub fn exit(code: impl Into<ExitCode>) -> !

Or more hypothetically

pub fn exit(code: impl Into<ExitCode> default i32) -> !

Or something which allowed for multiple implementations. ExitCode doesn't implement From<i32>. My guess is there's a comment on a random PR about that. But the point is there are options.

4 Likes

No, it's still unstable. I linked to the stable docs which has a tag that it's unstable :smile:

Here's the tracking issue: Tracking Issue for ExitCode::exit_process() · Issue #97100 · rust-lang/rust · GitHub

From the tracing issue, one concern was inference failures. See this comment

1 Like

Not just in theory.

(Whether it'll be used in this case, I don't know).

process::exit() accepts any i32 value as the exit code for the process; however, there are platforms that only use a subset of that value (see process::exit platform-specific behavior). ExitCode exists because of this; only ExitCodes that are supported by a majority of our platforms can be created, so those problems don’t exist (as much) with this method.

I mean set_var used to be a safe fn and is now an unsafe fn, how did they do that if that also is a breaking change?

Because the other option was failing Rust's safety guarantee, this was seen as significantly worse and making env::set_var "safe" was a mistake.

The bar for a high impact breaking change is very high, and changing set_var to unsafe cleared that bar.

This is also a high impact breaking change, but the motivation doesn't clear that high bar. A type signature being inconvenient or theoretically sub-par is not sufficient.

3 Likes