Skip to content

Commit 9110a8f

Browse files
workingjubileegitbot
authored and
gitbot
committed
Rollup merge of rust-lang#135046 - RalfJung:rustc_box_intrinsic, r=compiler-errors
turn rustc_box into an intrinsic I am not entirely sure why this was made a special magic attribute, but an intrinsic seems like a more natural way to add magic expressions to the language.
2 parents d938ba2 + c9e2487 commit 9110a8f

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

alloc/src/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ unsafe impl Allocator for Global {
339339
}
340340
}
341341

342-
/// The allocator for unique pointers.
342+
/// The allocator for `Box`.
343343
#[cfg(all(not(no_global_oom_handling), not(test)))]
344344
#[lang = "exchange_malloc"]
345345
#[inline]

alloc/src/boxed.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ pub struct Box<
233233
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
234234
>(Unique<T>, A);
235235

236+
/// Constructs a `Box<T>` by calling the `exchange_malloc` lang item and moving the argument into
237+
/// the newly allocated memory. This is an intrinsic to avoid unnecessary copies.
238+
///
239+
/// This is the surface syntax for `box <expr>` expressions.
240+
#[cfg(not(bootstrap))]
241+
#[rustc_intrinsic]
242+
#[rustc_intrinsic_must_be_overridden]
243+
#[unstable(feature = "liballoc_internals", issue = "none")]
244+
pub fn box_new<T>(_x: T) -> Box<T> {
245+
unreachable!()
246+
}
247+
248+
/// Transition function for the next bootstrap bump.
249+
#[cfg(bootstrap)]
250+
#[unstable(feature = "liballoc_internals", issue = "none")]
251+
#[inline(always)]
252+
pub fn box_new<T>(x: T) -> Box<T> {
253+
#[rustc_box]
254+
Box::new(x)
255+
}
256+
236257
impl<T> Box<T> {
237258
/// Allocates memory on the heap and then places `x` into it.
238259
///
@@ -250,8 +271,7 @@ impl<T> Box<T> {
250271
#[rustc_diagnostic_item = "box_new"]
251272
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
252273
pub fn new(x: T) -> Self {
253-
#[rustc_box]
254-
Box::new(x)
274+
return box_new(x);
255275
}
256276

257277
/// Constructs a new box with uninitialized contents.

alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
#![feature(dropck_eyepatch)]
169169
#![feature(fundamental)]
170170
#![feature(hashmap_internals)]
171+
#![feature(intrinsics)]
171172
#![feature(lang_items)]
172173
#![feature(min_specialization)]
173174
#![feature(multiple_supertrait_upcastable)]

alloc/src/macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ macro_rules! vec {
4848
);
4949
($($x:expr),+ $(,)?) => (
5050
<[_]>::into_vec(
51-
// This rustc_box is not required, but it produces a dramatic improvement in compile
51+
// Using the intrinsic produces a dramatic improvement in compile
5252
// time when constructing arrays with many elements.
53-
#[rustc_box]
54-
$crate::boxed::Box::new([$($x),+])
53+
$crate::boxed::box_new([$($x),+])
5554
)
5655
);
5756
}

0 commit comments

Comments
 (0)