Skip to content

Commit 8d49e94

Browse files
committed
Doc fixes from review
1 parent c155d51 commit 8d49e94

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

compiler/rustc_data_structures/src/tagged_ptr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub use drop::TaggedPtr;
4040
/// [`into_ptr`] must be valid for writes (and thus calling [`NonNull::as_mut`]
4141
/// on it must be safe).
4242
///
43-
/// The [`BITS`] constant must be correct. At least [`BITS`] least significant
44-
/// bits, must be zero on all pointers returned from [`into_ptr`].
43+
/// The [`BITS`] constant must be correct. [`BITS`] least-significant bits,
44+
/// must be zero on all pointers returned from [`into_ptr`].
4545
///
4646
/// For example, if the alignment of [`Self::Target`] is 2, then `BITS` should be 1.
4747
///
@@ -52,9 +52,12 @@ pub use drop::TaggedPtr;
5252
/// [`Self::Target`]: Deref::Target
5353
/// [`DerefMut`]: std::ops::DerefMut
5454
pub unsafe trait Pointer: Deref {
55-
/// Number of unused (always zero) **least significant bits** in this
55+
/// Number of unused (always zero) **least-significant bits** in this
5656
/// pointer, usually related to the pointees alignment.
5757
///
58+
/// For example if [`BITS`] = `2`, then given `ptr = Self::into_ptr(..)`,
59+
/// `ptr.addr() & 0b11 == 0` must be true.
60+
///
5861
/// Most likely the value you want to use here is the following, unless
5962
/// your [`Self::Target`] type is unsized (e.g., `ty::List<T>` in rustc)
6063
/// or your pointer is over/under aligned, in which case you'll need to
@@ -71,6 +74,7 @@ pub unsafe trait Pointer: Deref {
7174
/// # }
7275
/// ```
7376
///
77+
/// [`BITS`]: Pointer::BITS
7478
/// [`Self::Target`]: Deref::Target
7579
const BITS: usize;
7680

@@ -105,7 +109,7 @@ pub unsafe trait Pointer: Deref {
105109
///
106110
/// The [`BITS`] constant must be correct.
107111
///
108-
/// No more than [`BITS`] least significant bits may be set in the returned usize.
112+
/// No more than [`BITS`] least-significant bits may be set in the returned usize.
109113
///
110114
/// [`BITS`]: Tag::BITS
111115
pub unsafe trait Tag: Copy {

compiler/rustc_data_structures/src/tagged_ptr/copy.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
/// those are embeddable in instruction encoding, for example:
3434
///
3535
/// ```asm
36-
/// // (https://godbolt.org/z/jqcYPWEr3)
36+
/// // (<https://godbolt.org/z/jqcYPWEr3>)
3737
/// example::shift_read3:
3838
/// mov eax, dword ptr [8*rdi]
3939
/// ret
@@ -49,7 +49,8 @@ where
4949
/// - `shift_read3` uses `<< 3` (the tag is in the most-significant bits)
5050
/// - `mask_read3` uses `& !0b111` (the tag is in the least-significant bits)
5151
///
52-
/// The shift approach thus produces less instructions and is likely faster.
52+
/// The shift approach thus produces less instructions and is likely faster
53+
/// (see <https://godbolt.org/z/Y913sMdWb>).
5354
///
5455
/// Encoding diagram:
5556
/// ```text
@@ -66,12 +67,21 @@ where
6667
tag_ghost: PhantomData<T>,
6768
}
6869

70+
// Note that even though `CopyTaggedPtr` is only really expected to work with
71+
// `P: Copy`, can't add `P: Copy` bound, because `CopyTaggedPtr` is used in the
72+
// `TaggedPtr`'s implementation.
6973
impl<P, T, const CP: bool> CopyTaggedPtr<P, T, CP>
7074
where
7175
P: Pointer,
7276
T: Tag,
7377
{
7478
/// Tags `pointer` with `tag`.
79+
///
80+
/// Note that this leaks `pointer`: it won't be dropped when
81+
/// `CopyTaggedPtr` is dropped. If you have a pointer with a significant
82+
/// drop, use [`TaggedPtr`] instead.
83+
///
84+
/// [`TaggedPtr`]: crate::tagged_ptr::TaggedPtr
7585
pub fn new(pointer: P, tag: T) -> Self {
7686
Self { packed: Self::pack(P::into_ptr(pointer), tag), tag_ghost: PhantomData }
7787
}
@@ -95,7 +105,8 @@ where
95105
let tag = self.packed.addr().get() >> Self::TAG_BIT_SHIFT;
96106

97107
// Safety:
98-
//
108+
// The shift retrieves the original value from `T::into_usize`,
109+
// satisfying `T::from_usize`'s preconditions.
99110
unsafe { T::from_usize(tag) }
100111
}
101112

@@ -152,6 +163,10 @@ where
152163
//
153164
// Semantically this is just `f(&self.pointer)` (where `self.pointer`
154165
// is non-packed original pointer).
166+
//
167+
// Note that even though `CopyTaggedPtr` is only really expected to
168+
// work with `P: Copy`, we have to assume `P: ?Copy`, because
169+
// `CopyTaggedPtr` is used in the `TaggedPtr`'s implementation.
155170
let ptr = unsafe { ManuallyDrop::new(P::from_ptr(self.pointer_raw())) };
156171
f(&ptr)
157172
}

0 commit comments

Comments
 (0)