-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Tracking issue for handle_alloc_error defaulting to panic (for no_std + liballoc) #66741
Copy link
Copy link
Closed
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-impl-incompleteStatus: The implementation is incomplete.Status: The implementation is incomplete.T-langRelevant to the language teamRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.WG-embeddedWorking group: Embedded systemsWorking group: Embedded systemsdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-impl-incompleteStatus: The implementation is incomplete.Status: The implementation is incomplete.T-langRelevant to the language teamRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.WG-embeddedWorking group: Embedded systemsWorking group: Embedded systemsdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
The proposal below was implemented in #76448, feature-gated under
#![feature(default_alloc_error_handler)].Issues to resolve before stabilization:
handle_alloc_errordefaulting to panic (for no_std + liballoc) #66741 (comment)handle_alloc_errordefault to panic (for no_std + liballoc) #76448 (comment) Tracking issue forhandle_alloc_errordefaulting to panic (for no_std + liballoc) #66741 (comment)Initial proposal:
Summary
This issue is for getting consensus on a change initially proposed in the tracking issue for
#[alloc_error_handler]: #51540 (comment)When no
#[alloc_error_handler]is defined (which implies thatstdis not linked, since it literally has such a handler),alloc::alloc::handle_alloc_errorshould default to callingcore::panic!with a message identical to the one thatstdprints to stderr before aborting in that case.Although #51540 (comment) suggested that a full RFC would not be necessary, this is loosely structured after the RFC template.
Background
See the Background section of the sibling issue proposing stabilization of the attribute.
Motivation
As of Rust 1.36, specifying an allocation error handler is the only requirement for using the
alloccrate inno_stdenvironments (i.e. without thestdcrate being also linked in the program) that cannot be fulfilled by users on the Stable release channel.Removing this requirement by having a default behavior would allow:
no_std+liballocapplications to start running on the Stable channelno_stdapplications that run on Stable to start usingliballocGuide-level explanation
When
stdis linked in an application,alloc::alloc::handle_alloc_errordefaults to printing an error message to stderr and aborting the process.When
stdis not linked and no other#[alloc_error_handler]is defined,handle_alloc_errordefaults to panicking as if the following handler were defined:Reference-level explanation
The implementation for this would be very similar to that of
#[global_allocator]. (Links in the next two paragraphs go to that implementation.)alloc::alloc::handle_alloc_erroris modified to call anextern "Rust" { fn … }declaration.The definition of this function does not exist in Rust source code. Instead, it is synthesized by the compiler for “top-level” compilations (executables,
cdylibs, etc.) whenallocis in the crate dependency graph. If an#[alloc_error_handler]is defined, the synthesized function calls it. If not, the synthesized function callsalloc::alloc::default_error_handlerwhich is a new lang item. (Or is it?)In order to allow experimentation for this new default behavior, it should initially be gated behind the
#![feature(default_alloc_error_handler)]feature flag. When no handler is defined, a call to the default is (at first) only synthesized if any of the crates in the dependency graph has that feature gate. If none of them do, the current compilation error continues to be emitted.Alternatives
The status quo is that
no_std+allocrequires Nightly.Stabilizing
#[alloc_error_handler]or some other mechanism for specifying this handler is another way to unlock theno_std+liballocon Stable use case. This removes the initial motivation for coming up with this default behavior. However perhaps this default is still desirable? In ano_stdenvironment where there is no process to abort, the allocation error handler will likely be very similar to the panic handler (which is already mandatory).