Skip to content

Commit 75d830a

Browse files
committed
Add generic parameter for allocator to Box
1 parent aeb4738 commit 75d830a

17 files changed

+41
-36
lines changed

src/liballoc/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
278278
// well.
279279
// For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`,
280280
// this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well.
281-
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
281+
pub(crate) unsafe fn box_free<T: ?Sized, A: AllocRef>(ptr: Unique<T>, mut alloc: A) {
282282
let size = size_of_val(ptr.as_ref());
283283
let align = min_align_of_val(ptr.as_ref());
284284
let layout = Layout::from_size_align_unchecked(size, align);
285-
Global.dealloc(ptr.cast().into(), layout)
285+
alloc.dealloc(ptr.cast().into(), layout)
286286
}
287287

288288
/// Abort on memory allocation error or failure.

src/liballoc/boxed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ use crate::vec::Vec;
156156
#[lang = "owned_box"]
157157
#[fundamental]
158158
#[stable(feature = "rust1", since = "1.0.0")]
159-
pub struct Box<T: ?Sized>(Unique<T>);
159+
pub struct Box<T: ?Sized, A: AllocRef = Global>(Unique<T>, A);
160160

161161
impl<T> Box<T> {
162162
/// Allocates memory on the heap and then places `x` into it.
@@ -392,7 +392,7 @@ impl<T: ?Sized> Box<T> {
392392
#[stable(feature = "box_raw", since = "1.4.0")]
393393
#[inline]
394394
pub unsafe fn from_raw(raw: *mut T) -> Self {
395-
Box(Unique::new_unchecked(raw))
395+
Box(Unique::new_unchecked(raw), Global)
396396
}
397397

398398
/// Consumes the `Box`, returning a wrapped raw pointer.
@@ -567,7 +567,7 @@ impl<T: ?Sized> Box<T> {
567567
}
568568

569569
#[stable(feature = "rust1", since = "1.0.0")]
570-
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
570+
unsafe impl<#[may_dangle] T: ?Sized, A: AllocRef> Drop for Box<T, A> {
571571
fn drop(&mut self) {
572572
// FIXME: Do nothing, drop is currently performed by compiler.
573573
}

src/liballoc/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ impl<T: ?Sized> Rc<T> {
997997
);
998998

999999
// Free the allocation without dropping its contents
1000-
box_free(box_unique);
1000+
box_free(box_unique, Global);
10011001

10021002
Self::from_ptr(ptr)
10031003
}

src/liballoc/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<T: ?Sized> Arc<T> {
950950
);
951951

952952
// Free the allocation without dropping its contents
953-
box_free(box_unique);
953+
box_free(box_unique, Global);
954954

955955
Self::from_ptr(ptr)
956956
}

src/test/mir-opt/inline/inline-into-box-place/64bit/rustc.main.Inline.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
- bb4 (cleanup): {
7070
- _3 = const alloc::alloc::box_free::<std::vec::Vec<u32>>(move (_2.0: std::ptr::Unique<std::vec::Vec<u32>>)) -> bb3; // scope 0 at $DIR/inline-into-box-place.rs:8:42: 8:43
7171
- // ty::Const
72-
- // + ty: unsafe fn(std::ptr::Unique<std::vec::Vec<u32>>) {alloc::alloc::box_free::<std::vec::Vec<u32>>}
72+
- // + ty: unsafe fn(std::ptr::Unique<std::vec::Vec<u32>>, std::alloc::Global) {alloc::alloc::box_free::<std::vec::Vec<u32>, std::alloc::Global>}
7373
- // + val: Value(Scalar(<ZST>))
7474
- // mir::Constant
7575
- // + span: $DIR/inline-into-box-place.rs:8:42: 8:43

src/test/ui/coherence/impl-foreign-for-fundamental[foreign].stderr

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
22
--> $DIR/impl-foreign-for-fundamental[foreign].rs:10:1
33
|
44
LL | impl Remote for Box<i32> {
5-
| ^^^^^^^^^^^^^^^^--------
6-
| | |
7-
| | `i32` is not defined in the current crate
5+
| ^^^^^------^^^^^--------
6+
| | | |
7+
| | | `i32` is not defined in the current crate
8+
| | `std::alloc::Global` is not defined in the current crate
89
| impl doesn't use only types from inside the current crate
910
|
1011
= note: define and implement a trait or new type instead
@@ -13,9 +14,10 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
1314
--> $DIR/impl-foreign-for-fundamental[foreign].rs:14:1
1415
|
1516
LL | impl<T> Remote for Box<Rc<T>> {
16-
| ^^^^^^^^^^^^^^^^^^^----------
17-
| | |
18-
| | `std::rc::Rc` is not defined in the current crate
17+
| ^^^^^^^^------^^^^^----------
18+
| | | |
19+
| | | `std::rc::Rc` is not defined in the current crate
20+
| | `std::alloc::Global` is not defined in the current crate
1921
| impl doesn't use only types from inside the current crate
2022
|
2123
= note: define and implement a trait or new type instead

src/test/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | impl Remote1<Box<String>> for i32 {
66
| | | |
77
| | | `i32` is not defined in the current crate
88
| | `std::string::String` is not defined in the current crate
9+
| | `std::alloc::Global` is not defined in the current crate
910
| impl doesn't use only types from inside the current crate
1011
|
1112
= note: define and implement a trait or new type instead
@@ -18,6 +19,7 @@ LL | impl Remote1<Box<Rc<i32>>> for f64 {
1819
| | | |
1920
| | | `f64` is not defined in the current crate
2021
| | `std::rc::Rc` is not defined in the current crate
22+
| | `std::alloc::Global` is not defined in the current crate
2123
| impl doesn't use only types from inside the current crate
2224
|
2325
= note: define and implement a trait or new type instead
@@ -30,6 +32,7 @@ LL | impl<T> Remote1<Box<Rc<T>>> for f32 {
3032
| | | |
3133
| | | `f32` is not defined in the current crate
3234
| | `std::rc::Rc` is not defined in the current crate
35+
| | `std::alloc::Global` is not defined in the current crate
3336
| impl doesn't use only types from inside the current crate
3437
|
3538
= note: define and implement a trait or new type instead

src/test/ui/issues/issue-14092.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn fn1(0: Box) {}
2-
//~^ ERROR wrong number of type arguments: expected 1, found 0 [E0107]
2+
//~^ ERROR wrong number of type arguments: expected at least 1, found 0 [E0107]
33

44
fn main() {}

src/test/ui/issues/issue-14092.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0107]: wrong number of type arguments: expected 1, found 0
1+
error[E0107]: wrong number of type arguments: expected at least 1, found 0
22
--> $DIR/issue-14092.rs:1:11
33
|
44
LL | fn fn1(0: Box) {}
5-
| ^^^ expected 1 type argument
5+
| ^^^ expected at least 1 type argument
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-3601.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `Box(_)` not covered
1+
error[E0004]: non-exhaustive patterns: `Box(_, _)` not covered
22
--> $DIR/issue-3601.rs:30:44
33
|
44
LL | box NodeKind::Element(ed) => match ed.kind {
5-
| ^^^^^^^ pattern `Box(_)` not covered
5+
| ^^^^^^^ pattern `Box(_, _)` not covered
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88
= note: the matched value is of type `std::boxed::Box<ElementKind>`

0 commit comments

Comments
 (0)