Skip to content

Commit fda4c8d

Browse files
committed
Update documentation to review comments
1 parent 58d62b8 commit fda4c8d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

library/core/src/alloc/global.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ use crate::ptr;
5555
/// and implementors must ensure such contracts remain true.
5656
///
5757
/// * You may not rely on allocations actually happening, even if there are explicit
58-
/// heap allocations in the source. The optimizer may detect allocation/deallocation
59-
/// pairs that it can instead move to stack allocations/deallocations and thus never
60-
/// invoke the allocator here.
58+
/// heap allocations in the source.
59+
/// The optimizer may detect unused allocations that it can either
60+
/// eliminate entirely or move to the stack and thus never invoke the allocator here. The
61+
/// optimizer may further assume that allocation is infallible, so code that used to fail due
62+
/// to allocator failures may now suddenly work because the optimizer worked around the
63+
/// need for an allocation.
6164
/// More concretely, the following code example is unsound, irrespective of whether your
6265
/// custom allocator allows counting how many allocations have happened.
6366
///
@@ -67,10 +70,10 @@ use crate::ptr;
6770
/// unsafe { std::intrinsics::assume(number_of_heap_allocs > 0); }
6871
/// ```
6972
///
70-
/// Note that allocation/deallocation pairs being moved to the stack is not the only
73+
/// Note that the optimizations mentioned above are not the only
7174
/// optimization that can be applied. You may generally not rely on heap allocations
72-
/// happening, if they can be removed without changing program behaviour.
73-
/// Whether allocations happen or not is not part of the program behaviour, even if it
75+
/// happening if they can be removed without changing program behavior.
76+
/// Whether allocations happen or not is not part of the program behavior, even if it
7477
/// could be detected via an allocator that tracks allocations by printing or otherwise
7578
/// having side effects.
7679
#[stable(feature = "global_alloc", since = "1.28.0")]

library/core/src/alloc/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ pub unsafe trait AllocRef {
9595
/// not have its contents initialized.
9696
///
9797
/// Note that you may not rely on this method actually getting called, even if there are calls
98-
/// to it in the source. The optimizer may detect allocation/deallocation pairs that it can
99-
/// instead move to stack allocations/deallocations and thus never invoke the allocator here.
98+
/// to it in the source. The optimizer may detect unused allocations that it can either
99+
/// eliminate entirely or move to the stack and thus never invoke the allocator here. The
100+
/// optimizer may further assume that allocation is infallible, so code that used to fail due
101+
/// to allocator failures may now suddenly work because the optimizer worked around the
102+
/// need for an allocation.
100103
/// More concretely, the following code example is unsound, irrespective of whether your
101104
/// custom allocator allows counting how many allocations have happened.
102105
///
@@ -106,6 +109,13 @@ pub unsafe trait AllocRef {
106109
/// unsafe { std::intrinsics::assume(number_of_heap_allocs > 0); }
107110
/// ```
108111
///
112+
/// Note that the optimizations mentioned above are not the only
113+
/// optimization that can be applied. You may generally not rely on heap allocations
114+
/// happening if they can be removed without changing program behavior.
115+
/// Whether allocations happen or not is not part of the program behavior, even if it
116+
/// could be detected via an allocator that tracks allocations by printing or otherwise
117+
/// having side effects.
118+
///
109119
/// # Errors
110120
///
111121
/// Returning `Err` indicates that either memory is exhausted or `layout` does not meet

0 commit comments

Comments
 (0)