rust-lang / libs-team

The home of the library team
Apache License 2.0
123 stars 19 forks source link

abort in core #442

Closed CAD97 closed 2 weeks ago

CAD97 commented 2 weeks ago

Proposal

Problem statement

Contexts without access to std don't have a canonical way to write fn abort() -> !. There should be one.

Motivating examples or use cases

There are multiple ways of aborting the process without std currently, including:

We should provide a simple way to abort that doesn't rely on directly causing a runtime error.

Solution sketch

// core::process

fn abort() -> !;
fn abort_immediate() -> !;

abort_immediate halts the process as abruptly as possible, usually by executing an illegal instruction. (This is the behavior of the abort intrinsic.)

abort uses a mechanism similar to core panic to raise the more user-friendly std abort when possible, or abort_immediate otherwise. (This will require some new compiler/lang support.)

Alternatives

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution:

dtolnay commented 2 weeks ago

Yes! I would love for this to be available.

Thank you for brainstorming other options too.

I expect we'll reassess the options after seeing how the abort part of the PR turns out, but this looks great to me to go ahead implementing. I think your intuition is correct that "something like whatever core's panic does" will work as an implementation strategy.

RalfJung commented 1 week ago

abort uses a mechanism similar to core panic to raise the more user-friendly std abort when possible, or abort_immediate otherwise. (This will require some new compiler/lang support.)

What about making it trigger panic_nounwind under the hood? That doesn't require any new support. That also matches what happens when one panics in extern "C".

However that should probably be named something closer to panic_nounwind than abort, since it e.g. triggers the panic hook.

CAD97 commented 1 week ago

IMO there needs to be an API which doesn't call the panic hook (thus calling uncontrolled code). panic_nounwind is valuable in its own right for sure, but can and should live alongside options for abort.

Whether panic_nounwind and abort_immediate are sufficient without having “user-friendly” abort in core, I'm still unsure.