diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs index 1f1033b0437f8..2e67bbebd6c6d 100644 --- a/library/core/src/alloc/mod.rs +++ b/library/core/src/alloc/mod.rs @@ -351,7 +351,7 @@ pub unsafe trait Allocator { } #[unstable(feature = "allocator_api", issue = "32838")] -unsafe impl Allocator for &A +unsafe impl Allocator for &'static A where A: Allocator + ?Sized, { diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 8ee55234cea43..39d766c29e58c 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -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) }, } @@ -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)) }, @@ -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) }, } diff --git a/src/test/ui/box/leak-alloc.rs b/src/test/ui/box/alloc-static.rs similarity index 71% rename from src/test/ui/box/leak-alloc.rs rename to src/test/ui/box/alloc-static.rs index 3f0f39f448b91..1469a023613d8 100644 --- a/src/test/ui/box/leak-alloc.rs +++ b/src/test/ui/box/alloc-static.rs @@ -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 } diff --git a/src/test/ui/box/alloc-static.stderr b/src/test/ui/box/alloc-static.stderr new file mode 100644 index 0000000000000..bcd8fd96452f2 --- /dev/null +++ b/src/test/ui/box/alloc-static.stderr @@ -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`. diff --git a/src/test/ui/box/leak-alloc.stderr b/src/test/ui/box/leak-alloc.stderr deleted file mode 100644 index e8a6ad0995a0f..0000000000000 --- a/src/test/ui/box/leak-alloc.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0505]: cannot move out of `alloc` because it is borrowed - --> $DIR/leak-alloc.rs:26:10 - | -LL | let boxed = Box::new_in(10, alloc.by_ref()); - | -------------- borrow of `alloc` occurs here -LL | let theref = Box::leak(boxed); -LL | drop(alloc); - | ^^^^^ move out of `alloc` occurs here -LL | -LL | use_value(*theref) - | ------- borrow later used here - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0505`.