From 6e38878db506f2d1698eaa2f17ed251d0e8a8fa2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 9 Jun 2026 12:52:22 +0200 Subject: [PATCH 1/4] UnsafeCell: mention shared-ref-to-interior case in type-level docs --- library/core/src/cell.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 1c79cf6bfcb36..4396f669758ce 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -2224,9 +2224,9 @@ impl fmt::Display for RefMut<'_, T> { /// thus this can cause distortions in the type size in these cases. /// /// Note that the only valid way to obtain a `*mut T` pointer to the contents of a -/// _shared_ `UnsafeCell` is through [`.get()`] or [`.raw_get()`]. A `&mut T` reference -/// can be obtained by either dereferencing this pointer or by calling [`.get_mut()`] -/// on an _exclusive_ `UnsafeCell`. Even though `T` and `UnsafeCell` have the +/// _shared_ `UnsafeCell` is through [`.get()`] or [`.raw_get()`]. An `&T` or `&mut T` reference +/// can then be obtained from that pointer, as long as the alasing rules outlined above are obeyed. +/// Even though `T` and `UnsafeCell` have the /// same memory layout, the following is not allowed and undefined behavior: /// /// ```rust,compile_fail @@ -2421,9 +2421,9 @@ impl UnsafeCell { /// Gets a mutable pointer to the wrapped value. /// - /// This can be cast to a pointer of any kind. When creating references, you must uphold the - /// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and - /// caveats. + /// This can be cast to a pointer of any kind. When creating (shared or mutable) references, you + /// must uphold the aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for + /// more discussion and caveats. /// /// # Examples /// @@ -2473,9 +2473,9 @@ impl UnsafeCell { /// The difference from [`get`] is that this function accepts a raw pointer, /// which is useful to avoid the creation of temporary references. /// - /// This can be cast to a pointer of any kind. When creating references, you must uphold the - /// aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for more discussion and - /// caveats. + /// This can be cast to a pointer of any kind. When creating (shared or mutable) references, you + /// must uphold the aliasing rules; see [the type-level docs][UnsafeCell#aliasing-rules] for + /// more discussion and caveats. /// /// [`get`]: UnsafeCell::get() /// From d0d453c0db5c8050e3621ea3a0712f7441003f64 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 9 Jun 2026 12:54:36 +0200 Subject: [PATCH 2/4] fix subtle issue in UnsafeCell aliasing model docs --- library/core/src/cell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 4396f669758ce..ef5f55dd3681f 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -2174,7 +2174,7 @@ impl fmt::Display for RefMut<'_, T> { /// `'a`. For example, this means that if you take the `*mut T` from an `UnsafeCell` and cast it /// to an `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found /// within `T`, of course) until that reference's lifetime expires. Similarly, if you create a -/// `&mut T` reference that is released to safe code, then you must not access the data within the +/// `&mut T` reference, then you must not access the data within the /// `UnsafeCell` until that reference expires. /// /// - For both `&T` without `UnsafeCell<_>` and `&mut T`, you must also not deallocate the data From 44a1ba5115ffca9ad9768677333de4fd21c09619 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 11 Jun 2026 12:17:48 +0200 Subject: [PATCH 3/4] fix typo Co-authored-by: Kevin Reid --- library/core/src/cell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index ef5f55dd3681f..c8a2569d4b9fd 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -2225,7 +2225,7 @@ impl fmt::Display for RefMut<'_, T> { /// /// Note that the only valid way to obtain a `*mut T` pointer to the contents of a /// _shared_ `UnsafeCell` is through [`.get()`] or [`.raw_get()`]. An `&T` or `&mut T` reference -/// can then be obtained from that pointer, as long as the alasing rules outlined above are obeyed. +/// can then be obtained from that pointer, as long as the aliasing rules outlined above are obeyed. /// Even though `T` and `UnsafeCell` have the /// same memory layout, the following is not allowed and undefined behavior: /// From c40fe474fcb768338157e7f75f97c8ad4702e0e6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 11 Jun 2026 17:29:38 +0200 Subject: [PATCH 4/4] =?UTF-8?q?an=20=E2=86=92=20a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/core/src/cell.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index c8a2569d4b9fd..9b2015ddc7101 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -2172,16 +2172,16 @@ impl fmt::Display for RefMut<'_, T> { /// - If you create a safe reference with lifetime `'a` (either a `&T` or `&mut T` reference), then /// you must not access the data in any way that contradicts that reference for the remainder of /// `'a`. For example, this means that if you take the `*mut T` from an `UnsafeCell` and cast it -/// to an `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found +/// to a `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found /// within `T`, of course) until that reference's lifetime expires. Similarly, if you create a /// `&mut T` reference, then you must not access the data within the /// `UnsafeCell` until that reference expires. /// /// - For both `&T` without `UnsafeCell<_>` and `&mut T`, you must also not deallocate the data -/// until the reference expires. As a special exception, given an `&T`, any part of it that is +/// until the reference expires. As a special exception, given a `&T`, any part of it that is /// inside an `UnsafeCell<_>` may be deallocated during the lifetime of the reference, after the /// last time the reference is used (dereferenced or reborrowed). Since you cannot deallocate a part -/// of what a reference points to, this means the memory an `&T` points to can be deallocated only if +/// of what a reference points to, this means the memory a `&T` points to can be deallocated only if /// *every part of it* (including padding) is inside an `UnsafeCell`. /// /// However, whenever a `&UnsafeCell` is constructed or dereferenced, it must still point to @@ -2197,7 +2197,7 @@ impl fmt::Display for RefMut<'_, T> { /// 2. A `&mut T` reference may be released to safe code provided neither other `&mut T` nor `&T` /// co-exist with it. A `&mut T` must always be unique. /// -/// Note that whilst mutating the contents of an `&UnsafeCell` (even while other +/// Note that whilst mutating the contents of a `&UnsafeCell` (even while other /// `&UnsafeCell` references alias the cell) is /// ok (provided you enforce the above invariants some other way), it is still undefined behavior /// to have multiple `&mut UnsafeCell` aliases. That is, `UnsafeCell` is a wrapper @@ -2224,7 +2224,7 @@ impl fmt::Display for RefMut<'_, T> { /// thus this can cause distortions in the type size in these cases. /// /// Note that the only valid way to obtain a `*mut T` pointer to the contents of a -/// _shared_ `UnsafeCell` is through [`.get()`] or [`.raw_get()`]. An `&T` or `&mut T` reference +/// _shared_ `UnsafeCell` is through [`.get()`] or [`.raw_get()`]. A `&T` or `&mut T` reference /// can then be obtained from that pointer, as long as the aliasing rules outlined above are obeyed. /// Even though `T` and `UnsafeCell` have the /// same memory layout, the following is not allowed and undefined behavior: