about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee <workingjubilee@gmail.com>2024-09-15 23:51:24 -0700
committerGitHub <noreply@github.com>2024-09-15 23:51:24 -0700
commit0151cbe6e8ca1027f5482c98a1b93d70fbd9a50b (patch)
tree10bd497472d9bce7206b3da687fc7bddf054db4a
parent39b7669347b02f25a36da610822fb3c1e03bac6c (diff)
parent56fb89acee048c80b595f42cdcb749ed39a43e10 (diff)
downloadrust-0151cbe6e8ca1027f5482c98a1b93d70fbd9a50b.tar.gz
rust-0151cbe6e8ca1027f5482c98a1b93d70fbd9a50b.zip
Rollup merge of #127879 - kornelski:bad-pointer-printf, r=workingjubilee
Document futility of printing temporary pointers

In the user forum I've seen a few people trying to understand how borrowing and moves are implemented by peppering their code with printing of `{:p}` of references to variables and expressions. This is a bad idea. It gives misleading and confusing results, because of autoderef magic, printing pointers of temporaries on the stack, and/or causes LLVM to optimize code differently when values had their address exposed.
-rw-r--r--library/core/src/fmt/mod.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 45c2b6a6a0f..8fc43cb1875 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -975,9 +975,17 @@ pub trait UpperHex {
 /// `p` formatting.
 ///
 /// The `Pointer` trait should format its output as a memory location. This is commonly presented
-/// as hexadecimal.
+/// as hexadecimal. For more information on formatters, see [the module-level documentation][module].
 ///
-/// For more information on formatters, see [the module-level documentation][module].
+/// Printing of pointers is not a reliable way to discover how Rust programs are implemented.
+/// The act of reading an address changes the program itself, and may change how the data is represented
+/// in memory, and may affect which optimizations are applied to the code.
+///
+/// The printed pointer values are not guaranteed to be stable nor unique identifiers of objects.
+/// Rust allows moving values to different memory locations, and may reuse the same memory locations
+/// for different purposes.
+///
+/// There is no guarantee that the printed value can be converted back to a pointer.
 ///
 /// [module]: ../../std/fmt/index.html
 ///