Skip to content

Commit c7bef8e

Browse files
authored
Unrolled build for rust-lang#118890
Rollup merge of rust-lang#118890 - Amanieu:allocator-lifetime, r=Mark-Simulacrum Clarify the lifetimes of allocations returned by the `Allocator` trait The previous definition (accidentally) disallowed the implementation of stack-based allocators whose memory would become invalid once the lifetime of the allocator type ended. This also ensures the validity of the following blanket implementation: ```rust impl<A: Allocator> Allocator for &'_ A {} ```
2 parents 81b757c + 8e9c8dd commit c7bef8e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/core/src/alloc/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ impl fmt::Display for AllocError {
9595
/// # Safety
9696
///
9797
/// * Memory blocks returned from an allocator that are [*currently allocated*] must point to
98-
/// valid memory and retain their validity while they are [*currently allocated*] and at
99-
/// least one of the instance and all of its clones has not been dropped.
98+
/// valid memory and retain their validity while they are [*currently allocated*] and the shorter
99+
/// of:
100+
/// - the borrow-checker lifetime of the allocator type itself.
101+
/// - as long as at least one of the instance and all of its clones has not been dropped.
100102
///
101103
/// * copying, cloning, or moving the allocator must not invalidate memory blocks returned from this
102104
/// allocator. A copied or cloned allocator must behave like the same allocator, and
@@ -114,6 +116,10 @@ pub unsafe trait Allocator {
114116
/// The returned block may have a larger size than specified by `layout.size()`, and may or may
115117
/// not have its contents initialized.
116118
///
119+
/// The returned block of memory remains valid as long as it is [*currently allocated*] and the shorter of:
120+
/// - the borrow-checker lifetime of the allocator type itself.
121+
/// - as long as at the allocator and all its clones has not been dropped.
122+
///
117123
/// # Errors
118124
///
119125
/// Returning `Err` indicates that either memory is exhausted or `layout` does not meet

0 commit comments

Comments
 (0)