Skip to content

Commit 951bc28

Browse files
committed
Stablize the alloc module without changing stability of its contents.
1 parent e9fd063 commit 951bc28

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

src/liballoc/alloc.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010

1111
//! Memory allocation APIs
1212
13-
#![unstable(feature = "allocator_api",
14-
reason = "the precise API and guarantees it provides may be tweaked \
15-
slightly, especially to possibly take into account the \
16-
types being stored to make room for a future \
17-
tracing garbage collector",
18-
issue = "32838")]
13+
#![stable(feature = "alloc_module", since = "1.28.0")]
1914

2015
use core::intrinsics::{min_align_of_val, size_of_val};
2116
use core::ptr::{NonNull, Unique};
2217
use core::usize;
2318

19+
#[stable(feature = "alloc_module", since = "1.28.0")]
2420
#[doc(inline)]
2521
pub use core::alloc::*;
2622

@@ -44,6 +40,7 @@ extern "Rust" {
4440
/// This type implements the [`Alloc`] trait by forwarding calls
4541
/// to the allocator registered with the `#[global_allocator]` attribute
4642
/// if there is one, or the `std` crate’s default.
43+
#[unstable(feature = "allocator_api", issue = "32838")]
4744
#[derive(Copy, Clone, Default, Debug)]
4845
pub struct Global;
4946

@@ -119,6 +116,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
119116
__rust_alloc_zeroed(layout.size(), layout.align())
120117
}
121118

119+
#[unstable(feature = "allocator_api", issue = "32838")]
122120
unsafe impl Alloc for Global {
123121
#[inline]
124122
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
@@ -188,6 +186,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
188186
/// and abort the process.
189187
/// It can be replaced with [`std::alloc::set_oom_hook`]
190188
/// and [`std::alloc::take_oom_hook`].
189+
#[unstable(feature = "allocator_api", issue = "32838")]
191190
#[rustc_allocator_nounwind]
192191
pub fn oom(layout: Layout) -> ! {
193192
#[allow(improper_ctypes)]

src/libcore/alloc.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@
1010

1111
//! Memory allocation APIs
1212
13-
#![unstable(feature = "allocator_api",
14-
reason = "the precise API and guarantees it provides may be tweaked \
15-
slightly, especially to possibly take into account the \
16-
types being stored to make room for a future \
17-
tracing garbage collector",
18-
issue = "32838")]
13+
#![stable(feature = "alloc_module", since = "1.28.0")]
1914

2015
use cmp;
2116
use fmt;
@@ -24,11 +19,13 @@ use usize;
2419
use ptr::{self, NonNull};
2520
use num::NonZeroUsize;
2621

22+
#[unstable(feature = "allocator_api", issue = "32838")]
2723
#[cfg(stage0)]
2824
pub type Opaque = u8;
2925

3026
/// Represents the combination of a starting address and
3127
/// a total capacity of the returned block.
28+
#[unstable(feature = "allocator_api", issue = "32838")]
3229
#[derive(Debug)]
3330
pub struct Excess(pub NonNull<u8>, pub usize);
3431

@@ -49,6 +46,7 @@ fn size_align<T>() -> (usize, usize) {
4946
/// requests have positive size. A caller to the `Alloc::alloc`
5047
/// method must either ensure that conditions like this are met, or
5148
/// use specific allocators with looser requirements.)
49+
#[unstable(feature = "allocator_api", issue = "32838")]
5250
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5351
pub struct Layout {
5452
// size of the requested block of memory, measured in bytes.
@@ -74,6 +72,7 @@ impl Layout {
7472
/// * `size`, when rounded up to the nearest multiple of `align`,
7573
/// must not overflow (i.e. the rounded value must be less than
7674
/// `usize::MAX`).
75+
#[unstable(feature = "allocator_api", issue = "32838")]
7776
#[inline]
7877
pub fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
7978
if !align.is_power_of_two() {
@@ -109,20 +108,24 @@ impl Layout {
109108
///
110109
/// This function is unsafe as it does not verify the preconditions from
111110
/// [`Layout::from_size_align`](#method.from_size_align).
111+
#[unstable(feature = "allocator_api", issue = "32838")]
112112
#[inline]
113113
pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
114114
Layout { size_: size, align_: NonZeroUsize::new_unchecked(align) }
115115
}
116116

117117
/// The minimum size in bytes for a memory block of this layout.
118+
#[unstable(feature = "allocator_api", issue = "32838")]
118119
#[inline]
119120
pub fn size(&self) -> usize { self.size_ }
120121

121122
/// The minimum byte alignment for a memory block of this layout.
123+
#[unstable(feature = "allocator_api", issue = "32838")]
122124
#[inline]
123125
pub fn align(&self) -> usize { self.align_.get() }
124126

125127
/// Constructs a `Layout` suitable for holding a value of type `T`.
128+
#[unstable(feature = "allocator_api", issue = "32838")]
126129
#[inline]
127130
pub fn new<T>() -> Self {
128131
let (size, align) = size_align::<T>();
@@ -139,6 +142,7 @@ impl Layout {
139142
/// Produces layout describing a record that could be used to
140143
/// allocate backing structure for `T` (which could be a trait
141144
/// or other unsized type like a slice).
145+
#[unstable(feature = "allocator_api", issue = "32838")]
142146
#[inline]
143147
pub fn for_value<T: ?Sized>(t: &T) -> Self {
144148
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
@@ -166,6 +170,7 @@ impl Layout {
166170
/// Panics if the combination of `self.size()` and the given `align`
167171
/// violates the conditions listed in
168172
/// [`Layout::from_size_align`](#method.from_size_align).
173+
#[unstable(feature = "allocator_api", issue = "32838")]
169174
#[inline]
170175
pub fn align_to(&self, align: usize) -> Self {
171176
Layout::from_size_align(self.size(), cmp::max(self.align(), align)).unwrap()
@@ -187,6 +192,7 @@ impl Layout {
187192
/// to be less than or equal to the alignment of the starting
188193
/// address for the whole allocated block of memory. One way to
189194
/// satisfy this constraint is to ensure `align <= self.align()`.
195+
#[unstable(feature = "allocator_api", issue = "32838")]
190196
#[inline]
191197
pub fn padding_needed_for(&self, align: usize) -> usize {
192198
let len = self.size();
@@ -223,6 +229,7 @@ impl Layout {
223229
/// of each element in the array.
224230
///
225231
/// On arithmetic overflow, returns `LayoutErr`.
232+
#[unstable(feature = "allocator_api", issue = "32838")]
226233
#[inline]
227234
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
228235
let padded_size = self.size().checked_add(self.padding_needed_for(self.align()))
@@ -248,6 +255,7 @@ impl Layout {
248255
/// (assuming that the record itself starts at offset 0).
249256
///
250257
/// On arithmetic overflow, returns `LayoutErr`.
258+
#[unstable(feature = "allocator_api", issue = "32838")]
251259
#[inline]
252260
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
253261
let new_align = cmp::max(self.align(), next.align());
@@ -274,6 +282,7 @@ impl Layout {
274282
/// aligned.
275283
///
276284
/// On arithmetic overflow, returns `LayoutErr`.
285+
#[unstable(feature = "allocator_api", issue = "32838")]
277286
#[inline]
278287
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutErr> {
279288
let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?;
@@ -295,6 +304,7 @@ impl Layout {
295304
/// `extend`.)
296305
///
297306
/// On arithmetic overflow, returns `LayoutErr`.
307+
#[unstable(feature = "allocator_api", issue = "32838")]
298308
#[inline]
299309
pub fn extend_packed(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
300310
let new_size = self.size().checked_add(next.size())
@@ -306,6 +316,7 @@ impl Layout {
306316
/// Creates a layout describing the record for a `[T; n]`.
307317
///
308318
/// On arithmetic overflow, returns `LayoutErr`.
319+
#[unstable(feature = "allocator_api", issue = "32838")]
309320
#[inline]
310321
pub fn array<T>(n: usize) -> Result<Self, LayoutErr> {
311322
Layout::new::<T>()
@@ -320,12 +331,14 @@ impl Layout {
320331
/// The parameters given to `Layout::from_size_align`
321332
/// or some other `Layout` constructor
322333
/// do not satisfy its documented constraints.
334+
#[unstable(feature = "allocator_api", issue = "32838")]
323335
#[derive(Clone, PartialEq, Eq, Debug)]
324336
pub struct LayoutErr {
325337
private: ()
326338
}
327339

328340
// (we need this for downstream impl of trait Error)
341+
#[unstable(feature = "allocator_api", issue = "32838")]
329342
impl fmt::Display for LayoutErr {
330343
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
331344
f.write_str("invalid parameters to Layout::from_size_align")
@@ -336,10 +349,12 @@ impl fmt::Display for LayoutErr {
336349
/// that may be due to resource exhaustion or to
337350
/// something wrong when combining the given input arguments with this
338351
/// allocator.
352+
#[unstable(feature = "allocator_api", issue = "32838")]
339353
#[derive(Clone, PartialEq, Eq, Debug)]
340354
pub struct AllocErr;
341355

342356
// (we need this for downstream impl of trait Error)
357+
#[unstable(feature = "allocator_api", issue = "32838")]
343358
impl fmt::Display for AllocErr {
344359
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345360
f.write_str("memory allocation failed")
@@ -350,16 +365,19 @@ impl fmt::Display for AllocErr {
350365
/// `shrink_in_place` were unable to reuse the given memory block for
351366
/// a requested layout.
352367
// FIXME: should this be in libcore or liballoc?
368+
#[unstable(feature = "allocator_api", issue = "32838")]
353369
#[derive(Clone, PartialEq, Eq, Debug)]
354370
pub struct CannotReallocInPlace;
355371

372+
#[unstable(feature = "allocator_api", issue = "32838")]
356373
impl CannotReallocInPlace {
357374
pub fn description(&self) -> &str {
358375
"cannot reallocate allocator's memory in place"
359376
}
360377
}
361378

362379
// (we need this for downstream impl of trait Error)
380+
#[unstable(feature = "allocator_api", issue = "32838")]
363381
impl fmt::Display for CannotReallocInPlace {
364382
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
365383
write!(f, "{}", self.description())
@@ -449,6 +467,7 @@ impl From<LayoutErr> for CollectionAllocErr {
449467
/// * `Layout` queries and calculations in general must be correct. Callers of
450468
/// this trait are allowed to rely on the contracts defined on each method,
451469
/// and implementors must ensure such contracts remain true.
470+
#[unstable(feature = "allocator_api", issue = "32838")]
452471
pub unsafe trait GlobalAlloc {
453472
/// Allocate memory as described by the given `layout`.
454473
///
@@ -664,6 +683,7 @@ pub unsafe trait GlobalAlloc {
664683
///
665684
/// Note that this list may get tweaked over time as clarifications are made in
666685
/// the future.
686+
#[unstable(feature = "allocator_api", issue = "32838")]
667687
pub unsafe trait Alloc {
668688

669689
// (Note: some existing allocators have unspecified but well-defined

src/libstd/alloc.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010

1111
//! Memory allocation APIs
1212
13-
#![unstable(issue = "32838", feature = "allocator_api")]
14-
15-
#[doc(inline)] pub use alloc_crate::alloc::{Global, Layout, oom};
16-
#[doc(inline)] pub use alloc_crate::alloc::{alloc, alloc_zeroed, dealloc, realloc};
17-
#[doc(inline)] pub use alloc_system::System;
18-
#[doc(inline)] pub use core::alloc::*;
13+
#![stable(feature = "alloc_module", since = "1.28.0")]
1914

2015
use core::sync::atomic::{AtomicPtr, Ordering};
2116
use core::{mem, ptr};
2217
use sys_common::util::dumb_print;
2318

19+
#[stable(feature = "alloc_module", since = "1.28.0")]
20+
#[doc(inline)]
21+
pub use alloc_crate::alloc::*;
22+
23+
#[unstable(feature = "allocator_api", issue = "32838")]
24+
#[doc(inline)]
25+
pub use alloc_system::System;
26+
2427
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
2528

2629
/// Registers a custom OOM hook, replacing any that was previously registered.
@@ -34,6 +37,7 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
3437
/// about the allocation that failed.
3538
///
3639
/// The OOM hook is a global resource.
40+
#[unstable(feature = "allocator_api", issue = "32838")]
3741
pub fn set_oom_hook(hook: fn(Layout)) {
3842
HOOK.store(hook as *mut (), Ordering::SeqCst);
3943
}
@@ -43,6 +47,7 @@ pub fn set_oom_hook(hook: fn(Layout)) {
4347
/// *See also the function [`set_oom_hook`].*
4448
///
4549
/// If no custom hook is registered, the default hook will be returned.
50+
#[unstable(feature = "allocator_api", issue = "32838")]
4651
pub fn take_oom_hook() -> fn(Layout) {
4752
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
4853
if hook.is_null() {
@@ -59,6 +64,7 @@ fn default_oom_hook(layout: Layout) {
5964
#[cfg(not(test))]
6065
#[doc(hidden)]
6166
#[lang = "oom"]
67+
#[unstable(feature = "allocator_api", issue = "32838")]
6268
pub extern fn rust_oom(layout: Layout) -> ! {
6369
let hook = HOOK.load(Ordering::SeqCst);
6470
let hook: fn(Layout) = if hook.is_null() {
@@ -73,6 +79,7 @@ pub extern fn rust_oom(layout: Layout) -> ! {
7379
#[cfg(not(test))]
7480
#[doc(hidden)]
7581
#[allow(unused_attributes)]
82+
#[unstable(feature = "allocator_api", issue = "32838")]
7683
pub mod __default_lib_allocator {
7784
use super::{System, Layout, GlobalAlloc};
7885
// for symbol names src/librustc/middle/allocator.rs

src/test/compile-fail/lint-stability-fields.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
mod cross_crate {
1919
extern crate lint_stability_fields;
2020

21+
mod reexport {
22+
#[stable(feature = "rust1", since = "1.0.0")]
23+
pub use super::lint_stability_fields::*;
24+
}
25+
2126
use self::lint_stability_fields::*;
2227

2328
pub fn foo() {
@@ -73,6 +78,8 @@ mod cross_crate {
7378
// the patterns are all fine:
7479
{ .. } = x;
7580

81+
// Unstable items are still unstable even when used through a stable "pub use".
82+
let x = reexport::Unstable2(1, 2, 3); //~ ERROR use of unstable
7683

7784
let x = Unstable2(1, 2, 3); //~ ERROR use of unstable
7885

0 commit comments

Comments
 (0)