Skip to content

Restrict Allocator impl to &'static A #94069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/core/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub unsafe trait Allocator {
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl<A> Allocator for &A
unsafe impl<A> Allocator for &'static A
where
A: Allocator + ?Sized,
{
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl System {
old_size => unsafe {
let new_ptr = self.alloc_impl(new_layout, zeroed)?;
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
Allocator::deallocate(&self, ptr, old_layout);
Allocator::deallocate(self, ptr, old_layout);
Ok(new_ptr)
},
}
Expand Down Expand Up @@ -256,7 +256,7 @@ unsafe impl Allocator for System {
match new_layout.size() {
// SAFETY: conditions must be upheld by the caller
0 => unsafe {
Allocator::deallocate(&self, ptr, old_layout);
Allocator::deallocate(self, ptr, old_layout);
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
},

Expand All @@ -276,9 +276,9 @@ unsafe impl Allocator for System {
// `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
// for `dealloc` must be upheld by the caller.
new_size => unsafe {
let new_ptr = Allocator::allocate(&self, new_layout)?;
let new_ptr = Allocator::allocate(self, new_layout)?;
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
Allocator::deallocate(&self, ptr, old_layout);
Allocator::deallocate(self, ptr, old_layout);
Ok(new_ptr)
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ unsafe impl Allocator for Alloc {

fn use_value(_: u32) {}

const GLOBAL_ALLOC: Alloc = Alloc {};

fn main() {
let boxed_global = Box::new_in(10, &GLOBAL_ALLOC);

let alloc = Alloc {};
let boxed = Box::new_in(10, alloc.by_ref());
let theref = Box::leak(boxed);
drop(alloc);
//~^ ERROR cannot move out of `alloc` because it is borrowed
use_value(*theref)
let boxed = Box::new_in(10, &alloc);
//~^ ERROR `alloc` does not live long enough
}
15 changes: 15 additions & 0 deletions src/test/ui/box/alloc-static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0597]: `alloc` does not live long enough
--> $DIR/alloc-static.rs:28:33
|
LL | let boxed = Box::new_in(10, &alloc);
| ----------------^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `alloc` is borrowed for `'static`
LL |
LL | }
| - `alloc` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
15 changes: 0 additions & 15 deletions src/test/ui/box/leak-alloc.stderr

This file was deleted.