Skip to content

Commit c335390

Browse files
authored
Rollup merge of #103061 - Amanieu:rewrite_alloc_error_handler, r=bjorn3
Rewrite implementation of `#[alloc_error_handler]` The new implementation doesn't use weak lang items and instead changes `#[alloc_error_handler]` to an attribute macro just like `#[global_allocator]`. The attribute will generate the `__rg_oom` function which is called by the compiler-generated `__rust_alloc_error_handler`. If no `__rg_oom` function is defined in any crate then the compiler shim will call `__rdl_oom` in the alloc crate which will simply panic. This also fixes link errors with `-C link-dead-code` with `default_alloc_error_handler`: `__rg_oom` was previously defined in the alloc crate and would attempt to reference the `oom` lang item, even if it didn't exist. This worked as long as `__rg_oom` was excluded from linking since it was not called. This is a prerequisite for the stabilization of `default_alloc_error_handler` (#102318).
2 parents 031ec50 + 7544927 commit c335390

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

alloc/src/alloc.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,18 @@ pub use std::alloc::handle_alloc_error;
402402
#[allow(unused_attributes)]
403403
#[unstable(feature = "alloc_internals", issue = "none")]
404404
pub mod __alloc_error_handler {
405-
use crate::alloc::Layout;
406-
407-
// called via generated `__rust_alloc_error_handler`
408-
409-
// if there is no `#[alloc_error_handler]`
405+
// called via generated `__rust_alloc_error_handler` if there is no
406+
// `#[alloc_error_handler]`.
410407
#[rustc_std_internal_symbol]
411408
pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
412409
panic!("memory allocation of {size} bytes failed")
413410
}
414411

415-
// if there is an `#[alloc_error_handler]`
412+
#[cfg(bootstrap)]
416413
#[rustc_std_internal_symbol]
417414
pub unsafe fn __rg_oom(size: usize, align: usize) -> ! {
415+
use crate::alloc::Layout;
416+
418417
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
419418
extern "Rust" {
420419
#[lang = "oom"]

core/src/macros/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,17 @@ pub(crate) mod builtin {
15111511
/* compiler built-in */
15121512
}
15131513

1514+
/// Attribute macro applied to a function to register it as a handler for allocation failure.
1515+
///
1516+
/// See also [`std::alloc::handle_alloc_error`](../../../std/alloc/fn.handle_alloc_error.html).
1517+
#[cfg(not(bootstrap))]
1518+
#[unstable(feature = "alloc_error_handler", issue = "51540")]
1519+
#[allow_internal_unstable(rustc_attrs)]
1520+
#[rustc_builtin_macro]
1521+
pub macro alloc_error_handler($item:item) {
1522+
/* compiler built-in */
1523+
}
1524+
15141525
/// Keeps the item it's applied to if the passed path is accessible, and removes it otherwise.
15151526
#[unstable(
15161527
feature = "cfg_accessible",

core/src/prelude/v1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ pub use crate::macros::builtin::{RustcDecodable, RustcEncodable};
7575

7676
// Do not `doc(no_inline)` so that they become doc items on their own
7777
// (no public module for them to be re-exported from).
78+
#[cfg(not(bootstrap))]
79+
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
80+
pub use crate::macros::builtin::alloc_error_handler;
7881
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
7982
pub use crate::macros::builtin::{bench, derive, global_allocator, test, test_case};
8083

std/src/prelude/v1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub use core::prelude::v1::{RustcDecodable, RustcEncodable};
5959

6060
// Do not `doc(no_inline)` so that they become doc items on their own
6161
// (no public module for them to be re-exported from).
62+
#[cfg(not(bootstrap))]
63+
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
64+
pub use core::prelude::v1::alloc_error_handler;
6265
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
6366
pub use core::prelude::v1::{bench, derive, global_allocator, test, test_case};
6467

0 commit comments

Comments
 (0)