about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-11-13 17:37:36 +0100
committerGitHub <noreply@github.com>2022-11-13 17:37:36 +0100
commita1b0702ea5efd2f1bacc2bb5cfac7b82abc58bc5 (patch)
tree08aac459556ddc8fc45fde8331e90a206b6d4eea
parentafd7977c850d9ce06f1dd2bebb40db8cc2224a51 (diff)
parent28ea0023408d9edcc61e89b9f511528e4757a53e (diff)
downloadrust-a1b0702ea5efd2f1bacc2bb5cfac7b82abc58bc5.tar.gz
rust-a1b0702ea5efd2f1bacc2bb5cfac7b82abc58bc5.zip
Rollup merge of #103996 - SUPERCILEX:docs, r=RalfJung
Add small clarification around using pointers derived from references

r? `@RalfJung`

One question about your example from https://github.com/rust-lang/libs-team/issues/122: at what point does UB arise? If writing 0 does not cause UB and the reference `x` is never read or written to (explicitly or implicitly by being wrapped in another data structure) after the call to `foo`, does UB only arise when dropping the value? I don't really get that since I thought references were always supposed to point to valid data?

```rust
fn foo(x: &mut NonZeroI32)  {
  let ptr = x as *mut NonZeroI32;
  unsafe { ptr.cast::<i32>().write(0); } // no UB here
  // What now? x is considered garbage when?
}
```
-rw-r--r--library/core/src/ptr/mod.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index 565c38d222a..fd6fe46bf50 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -35,7 +35,8 @@
 //!   be used for inter-thread synchronization.
 //! * The result of casting a reference to a pointer is valid for as long as the
 //!   underlying object is live and no reference (just raw pointers) is used to
-//!   access the same memory.
+//!   access the same memory. That is, reference and pointer accesses cannot be
+//!   interleaved.
 //!
 //! These axioms, along with careful use of [`offset`] for pointer arithmetic,
 //! are enough to correctly implement many useful things in unsafe code. Stronger guarantees
@@ -64,7 +65,6 @@
 //! separate allocated object), heap allocations (each allocation created by the global allocator is
 //! a separate allocated object), and `static` variables.
 //!
-//!
 //! # Strict Provenance
 //!
 //! **The following text is non-normative, insufficiently formal, and is an extremely strict