Skip to content

Commit 34c9fd0

Browse files
committed
Stablize the alloc module without changing stability of its contents.
1 parent 148e2cd commit 34c9fd0

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

src/liballoc/alloc.rs

+5-6
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

+26-6
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@
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;
2217
use mem;
2318
use usize;
2419
use ptr::{self, NonNull};
2520

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

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

@@ -48,6 +45,7 @@ fn size_align<T>() -> (usize, usize) {
4845
/// requests have positive size. A caller to the `Alloc::alloc`
4946
/// method must either ensure that conditions like this are met, or
5047
/// use specific allocators with looser requirements.)
48+
#[unstable(feature = "allocator_api", issue = "32838")]
5149
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5250
pub struct Layout {
5351
// size of the requested block of memory, measured in bytes.
@@ -78,6 +76,7 @@ impl Layout {
7876
/// * `size`, when rounded up to the nearest multiple of `align`,
7977
/// must not overflow (i.e. the rounded value must be less than
8078
/// `usize::MAX`).
79+
#[unstable(feature = "allocator_api", issue = "32838")]
8180
#[inline]
8281
pub fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
8382
if !align.is_power_of_two() {
@@ -114,20 +113,24 @@ impl Layout {
114113
/// This function is unsafe as it does not verify that `align` is
115114
/// a power-of-two nor `size` aligned to `align` fits within the
116115
/// address space (i.e. the `Layout::from_size_align` preconditions).
116+
#[unstable(feature = "allocator_api", issue = "32838")]
117117
#[inline]
118118
pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
119119
Layout { size: size, align: align }
120120
}
121121

122122
/// The minimum size in bytes for a memory block of this layout.
123+
#[unstable(feature = "allocator_api", issue = "32838")]
123124
#[inline]
124125
pub fn size(&self) -> usize { self.size }
125126

126127
/// The minimum byte alignment for a memory block of this layout.
128+
#[unstable(feature = "allocator_api", issue = "32838")]
127129
#[inline]
128130
pub fn align(&self) -> usize { self.align }
129131

130132
/// Constructs a `Layout` suitable for holding a value of type `T`.
133+
#[unstable(feature = "allocator_api", issue = "32838")]
131134
pub fn new<T>() -> Self {
132135
let (size, align) = size_align::<T>();
133136
// Note that the align is guaranteed by rustc to be a power of two and
@@ -143,6 +146,7 @@ impl Layout {
143146
/// Produces layout describing a record that could be used to
144147
/// allocate backing structure for `T` (which could be a trait
145148
/// or other unsized type like a slice).
149+
#[unstable(feature = "allocator_api", issue = "32838")]
146150
pub fn for_value<T: ?Sized>(t: &T) -> Self {
147151
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
148152
// See rationale in `new` for why this us using an unsafe variant below
@@ -168,6 +172,7 @@ impl Layout {
168172
///
169173
/// Panics if the combination of `self.size` and the given `align`
170174
/// violates the conditions listed in `from_size_align`.
175+
#[unstable(feature = "allocator_api", issue = "32838")]
171176
#[inline]
172177
pub fn align_to(&self, align: usize) -> Self {
173178
Layout::from_size_align(self.size, cmp::max(self.align, align)).unwrap()
@@ -189,6 +194,7 @@ impl Layout {
189194
/// to be less than or equal to the alignment of the starting
190195
/// address for the whole allocated block of memory. One way to
191196
/// satisfy this constraint is to ensure `align <= self.align`.
197+
#[unstable(feature = "allocator_api", issue = "32838")]
192198
#[inline]
193199
pub fn padding_needed_for(&self, align: usize) -> usize {
194200
let len = self.size();
@@ -224,6 +230,7 @@ impl Layout {
224230
/// of each element in the array.
225231
///
226232
/// On arithmetic overflow, returns `None`.
233+
#[unstable(feature = "allocator_api", issue = "32838")]
227234
#[inline]
228235
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
229236
let padded_size = self.size.checked_add(self.padding_needed_for(self.align))
@@ -244,6 +251,7 @@ impl Layout {
244251
/// (assuming that the record itself starts at offset 0).
245252
///
246253
/// On arithmetic overflow, returns `None`.
254+
#[unstable(feature = "allocator_api", issue = "32838")]
247255
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
248256
let new_align = cmp::max(self.align, next.align);
249257
let realigned = Layout::from_size_align(self.size, new_align)?;
@@ -271,6 +279,7 @@ impl Layout {
271279
/// aligned.
272280
///
273281
/// On arithmetic overflow, returns `None`.
282+
#[unstable(feature = "allocator_api", issue = "32838")]
274283
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutErr> {
275284
let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?;
276285
Layout::from_size_align(size, self.align)
@@ -291,6 +300,7 @@ impl Layout {
291300
/// `extend`.)
292301
///
293302
/// On arithmetic overflow, returns `None`.
303+
#[unstable(feature = "allocator_api", issue = "32838")]
294304
pub fn extend_packed(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
295305
let new_size = self.size().checked_add(next.size())
296306
.ok_or(LayoutErr { private: () })?;
@@ -301,6 +311,7 @@ impl Layout {
301311
/// Creates a layout describing the record for a `[T; n]`.
302312
///
303313
/// On arithmetic overflow, returns `None`.
314+
#[unstable(feature = "allocator_api", issue = "32838")]
304315
pub fn array<T>(n: usize) -> Result<Self, LayoutErr> {
305316
Layout::new::<T>()
306317
.repeat(n)
@@ -314,12 +325,14 @@ impl Layout {
314325
/// The parameters given to `Layout::from_size_align`
315326
/// or some other `Layout` constructor
316327
/// do not satisfy its documented constraints.
328+
#[unstable(feature = "allocator_api", issue = "32838")]
317329
#[derive(Clone, PartialEq, Eq, Debug)]
318330
pub struct LayoutErr {
319331
private: ()
320332
}
321333

322334
// (we need this for downstream impl of trait Error)
335+
#[unstable(feature = "allocator_api", issue = "32838")]
323336
impl fmt::Display for LayoutErr {
324337
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
325338
f.write_str("invalid parameters to Layout::from_size_align")
@@ -330,10 +343,12 @@ impl fmt::Display for LayoutErr {
330343
/// that may be due to resource exhaustion or to
331344
/// something wrong when combining the given input arguments with this
332345
/// allocator.
346+
#[unstable(feature = "allocator_api", issue = "32838")]
333347
#[derive(Clone, PartialEq, Eq, Debug)]
334348
pub struct AllocErr;
335349

336350
// (we need this for downstream impl of trait Error)
351+
#[unstable(feature = "allocator_api", issue = "32838")]
337352
impl fmt::Display for AllocErr {
338353
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
339354
f.write_str("memory allocation failed")
@@ -344,16 +359,19 @@ impl fmt::Display for AllocErr {
344359
/// `shrink_in_place` were unable to reuse the given memory block for
345360
/// a requested layout.
346361
// FIXME: should this be in libcore or liballoc?
362+
#[unstable(feature = "allocator_api", issue = "32838")]
347363
#[derive(Clone, PartialEq, Eq, Debug)]
348364
pub struct CannotReallocInPlace;
349365

366+
#[unstable(feature = "allocator_api", issue = "32838")]
350367
impl CannotReallocInPlace {
351368
pub fn description(&self) -> &str {
352369
"cannot reallocate allocator's memory in place"
353370
}
354371
}
355372

356373
// (we need this for downstream impl of trait Error)
374+
#[unstable(feature = "allocator_api", issue = "32838")]
357375
impl fmt::Display for CannotReallocInPlace {
358376
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
359377
write!(f, "{}", self.description())
@@ -435,6 +453,7 @@ impl From<AllocErr> for CollectionAllocErr {
435453
/// * `Layout` queries and calculations in general must be correct. Callers of
436454
/// this trait are allowed to rely on the contracts defined on each method,
437455
/// and implementors must ensure such contracts remain true.
456+
#[unstable(feature = "allocator_api", issue = "32838")]
438457
pub unsafe trait GlobalAlloc {
439458
/// Allocate memory as described by the given `layout`.
440459
///
@@ -650,6 +669,7 @@ pub unsafe trait GlobalAlloc {
650669
///
651670
/// Note that this list may get tweaked over time as clarifications are made in
652671
/// the future.
672+
#[unstable(feature = "allocator_api", issue = "32838")]
653673
pub unsafe trait Alloc {
654674

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

src/libstd/alloc.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
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

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

2528
/// Registers a custom OOM hook, replacing any that was previously registered.
@@ -33,6 +36,7 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
3336
/// about the allocation that failed.
3437
///
3538
/// The OOM hook is a global resource.
39+
#[unstable(feature = "allocator_api", issue = "32838")]
3640
pub fn set_oom_hook(hook: fn(Layout) -> !) {
3741
HOOK.store(hook as *mut (), Ordering::SeqCst);
3842
}
@@ -42,6 +46,7 @@ pub fn set_oom_hook(hook: fn(Layout) -> !) {
4246
/// *See also the function [`set_oom_hook`].*
4347
///
4448
/// If no custom hook is registered, the default hook will be returned.
49+
#[unstable(feature = "allocator_api", issue = "32838")]
4550
pub fn take_oom_hook() -> fn(Layout) -> ! {
4651
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
4752
if hook.is_null() {
@@ -58,6 +63,7 @@ fn default_oom_hook(layout: Layout) -> ! {
5863
#[cfg(not(test))]
5964
#[doc(hidden)]
6065
#[lang = "oom"]
66+
#[unstable(feature = "allocator_api", issue = "32838")]
6167
pub extern fn rust_oom(layout: Layout) -> ! {
6268
let hook = HOOK.load(Ordering::SeqCst);
6369
let hook: fn(Layout) -> ! = if hook.is_null() {
@@ -71,6 +77,7 @@ pub extern fn rust_oom(layout: Layout) -> ! {
7177
#[cfg(not(test))]
7278
#[doc(hidden)]
7379
#[allow(unused_attributes)]
80+
#[unstable(feature = "allocator_api", issue = "32838")]
7481
pub mod __default_lib_allocator {
7582
use super::{System, Layout, GlobalAlloc};
7683
// for symbol names src/librustc/middle/allocator.rs

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

+7
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)