Skip to content

Commit 569d9a2

Browse files
committed
Use #[rustc_box] in alloc instead of box syntax
1 parent ec79883 commit 569d9a2

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

Diff for: alloc/src/boxed.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,25 @@ impl<T> Box<T> {
192192
/// ```
193193
/// let five = Box::new(5);
194194
/// ```
195-
#[cfg(not(no_global_oom_handling))]
195+
#[cfg(all(not(no_global_oom_handling), not(bootstrap)))]
196+
#[inline(always)]
197+
#[stable(feature = "rust1", since = "1.0.0")]
198+
#[must_use]
199+
pub fn new(x: T) -> Self {
200+
#[rustc_box]
201+
Box::new(x)
202+
}
203+
204+
/// Allocates memory on the heap and then places `x` into it.
205+
///
206+
/// This doesn't actually allocate if `T` is zero-sized.
207+
///
208+
/// # Examples
209+
///
210+
/// ```
211+
/// let five = Box::new(5);
212+
/// ```
213+
#[cfg(all(not(no_global_oom_handling), bootstrap))]
196214
#[inline(always)]
197215
#[stable(feature = "rust1", since = "1.0.0")]
198216
#[must_use]
@@ -259,7 +277,9 @@ impl<T> Box<T> {
259277
#[must_use]
260278
#[inline(always)]
261279
pub fn pin(x: T) -> Pin<Box<T>> {
262-
(box x).into()
280+
(#[cfg_attr(not(bootstrap), rustc_box)]
281+
Box::new(x))
282+
.into()
263283
}
264284

265285
/// Allocates memory on the heap then places `x` into it,
@@ -1186,7 +1206,8 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
11861206
impl<T: Default> Default for Box<T> {
11871207
/// Creates a `Box<T>`, with the `Default` value for T.
11881208
fn default() -> Self {
1189-
box T::default()
1209+
#[cfg_attr(not(bootstrap), rustc_box)]
1210+
Box::new(T::default())
11901211
}
11911212
}
11921213

@@ -1550,7 +1571,8 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
15501571
/// println!("{boxed:?}");
15511572
/// ```
15521573
fn from(array: [T; N]) -> Box<[T]> {
1553-
box array
1574+
#[cfg_attr(not(bootstrap), rustc_box)]
1575+
Box::new(array)
15541576
}
15551577
}
15561578

Diff for: alloc/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
#![feature(allocator_internals)]
148148
#![feature(allow_internal_unstable)]
149149
#![feature(associated_type_bounds)]
150-
#![feature(box_syntax)]
150+
#![cfg_attr(bootstrap, feature(box_syntax))]
151151
#![feature(cfg_sanitize)]
152152
#![feature(const_deref)]
153153
#![feature(const_mut_refs)]
@@ -170,6 +170,7 @@
170170
#![feature(rustc_attrs)]
171171
#![feature(slice_internals)]
172172
#![feature(staged_api)]
173+
#![feature(stmt_expr_attributes)]
173174
#![cfg_attr(test, feature(test))]
174175
#![feature(unboxed_closures)]
175176
#![feature(unsized_fn_params)]

Diff for: alloc/src/macros.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,28 @@
3434
/// be mindful of side effects.
3535
///
3636
/// [`Vec`]: crate::vec::Vec
37-
#[cfg(all(not(no_global_oom_handling), not(test)))]
37+
#[cfg(all(not(no_global_oom_handling), not(test), not(bootstrap)))]
38+
#[macro_export]
39+
#[stable(feature = "rust1", since = "1.0.0")]
40+
#[rustc_diagnostic_item = "vec_macro"]
41+
#[allow_internal_unstable(rustc_attrs, liballoc_internals)]
42+
macro_rules! vec {
43+
() => (
44+
$crate::__rust_force_expr!($crate::vec::Vec::new())
45+
);
46+
($elem:expr; $n:expr) => (
47+
$crate::__rust_force_expr!($crate::vec::from_elem($elem, $n))
48+
);
49+
($($x:expr),+ $(,)?) => (
50+
$crate::__rust_force_expr!(<[_]>::into_vec(
51+
#[rustc_box]
52+
$crate::boxed::Box::new([$($x),+])
53+
))
54+
);
55+
}
56+
57+
/// Creates a `Vec` containing the arguments (bootstrap version).
58+
#[cfg(all(not(no_global_oom_handling), not(test), bootstrap))]
3859
#[macro_export]
3960
#[stable(feature = "rust1", since = "1.0.0")]
4061
#[rustc_diagnostic_item = "vec_macro"]
@@ -65,7 +86,7 @@ macro_rules! vec {
6586
$crate::vec::from_elem($elem, $n)
6687
);
6788
($($x:expr),*) => (
68-
$crate::slice::into_vec(box [$($x),*])
89+
$crate::slice::into_vec($crate::boxed::Box::new([$($x),*]))
6990
);
7091
($($x:expr,)*) => (vec![$($x),*])
7192
}

Diff for: alloc/src/vec/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2983,12 +2983,15 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> {
29832983
/// ```
29842984
#[cfg(not(test))]
29852985
fn from(s: [T; N]) -> Vec<T> {
2986-
<[T]>::into_vec(box s)
2986+
<[T]>::into_vec(
2987+
#[cfg_attr(not(bootstrap), rustc_box)]
2988+
Box::new(s),
2989+
)
29872990
}
29882991

29892992
#[cfg(test)]
29902993
fn from(s: [T; N]) -> Vec<T> {
2991-
crate::slice::into_vec(box s)
2994+
crate::slice::into_vec(Box::new(s))
29922995
}
29932996
}
29942997

0 commit comments

Comments
 (0)