Skip to content

Commit 9eda70f

Browse files
committed
docs(allocator): improve docs for Address methods (#15697)
Improve docs for `Address::from_ptr` and `Address::from_ref`. Notably, include a warning about double-references in docs for `Address::from_ptr`.
1 parent 22cb1f0 commit 9eda70f

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

crates/oxc_allocator/src/address.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@ impl Address {
1919
/// Get the memory address of a pointer to an AST node in arena.
2020
///
2121
/// **This method is an escape hatch only.**
22-
/// Prefer using `GetAddress::address` instead, because it is more likely to produce a stable `Address`.
22+
/// Prefer using [`GetAddress::address`] instead, because it is more likely to produce a stable [`Address`].
2323
///
24-
/// If the AST node is in a `Box`, the address is guaranteed to be a unique identifier
24+
/// If the AST node is in a [`Box`], the address is guaranteed to be a unique identifier
2525
/// for the duration of the arena's existence.
2626
///
27-
/// But if the node is in a `Vec`, then the `Address` may not remain accurate if the `Vec`
27+
/// But if the node is in a [`Vec`], then the `Address` may not remain accurate if the `Vec`
2828
/// is resized or has elements added or removed before this node.
2929
///
3030
/// The pointer must point to an AST node in the arena (not on the stack),
3131
/// or the returned `Address` will be meaningless.
3232
///
33+
/// If called with a reference, the reference must point to an AST node in the arena (not on the stack),
34+
/// or the returned `Address` will be meaningless. Be careful not to pass a double-reference to `from_ptr`,
35+
/// or the resulting `Address` will point to the reference itself, instead of the thing being referenced.
36+
///
37+
/// ```ignore
38+
/// impl<'a> Visit<'a> for MyVisitor {
39+
/// fn visit_identifier_reference(&mut self, ident: &IdentifierReference<'a>) {
40+
/// // Correct - `address` is address of the `IdentifierReference`
41+
/// let address = Address::from_ptr(ident);
42+
/// // WRONG - `address` is address of `&IdentifierReference` reference itself, which is on the stack
43+
/// let address = Address::from_ptr(&ident);
44+
/// }
45+
/// }
46+
/// ```
47+
///
3348
/// # Example
3449
///
3550
/// Demonstration of the difference between `Address::from_ptr` and `GetAddress::address`:
@@ -71,6 +86,9 @@ impl Address {
7186
/// // Address of the `Statement` has changed again
7287
/// assert!(stmt_address_after_insert != stmt_address_after_push);
7388
/// ```
89+
///
90+
/// [`Box`]: crate::Box
91+
/// [`Vec`]: crate::Vec
7492
#[inline(always)] // Because it's a no-op
7593
pub fn from_ptr<T>(p: *const T) -> Self {
7694
Self(p as usize)
@@ -79,12 +97,12 @@ impl Address {
7997
/// Get the memory address of a reference to an AST node in arena.
8098
///
8199
/// **This method is an escape hatch only.**
82-
/// Prefer using `GetAddress::address` instead, because it is more likely to produce a stable `Address`.
100+
/// Prefer using [`GetAddress::address`] instead, because it is more likely to produce a stable [`Address`].
83101
///
84-
/// If the AST node is in a `Box`, the address is guaranteed to be a unique identifier
102+
/// If the AST node is in a [`Box`], the address is guaranteed to be a unique identifier
85103
/// for the duration of the arena's existence.
86104
///
87-
/// But if the node is in a `Vec`, then the `Address` may not remain accurate if the `Vec`
105+
/// But if the node is in a [`Vec`], then the `Address` may not remain accurate if the `Vec`
88106
/// is resized or has elements added or removed before this node.
89107
///
90108
/// The reference must point to an AST node in the arena (not on the stack), or the returned `Address`
@@ -143,6 +161,9 @@ impl Address {
143161
/// // Address of the `Statement` has changed again
144162
/// assert!(stmt_address_after_insert != stmt_address_after_push);
145163
/// ```
164+
///
165+
/// [`Box`]: crate::Box
166+
/// [`Vec`]: crate::Vec
146167
#[inline(always)] // Because it's a no-op
147168
pub fn from_ref<T>(r: &T) -> Self {
148169
let p = NonNull::from_ref(r);

0 commit comments

Comments
 (0)