about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-03-16 11:15:56 +0000
committerbors <bors@rust-lang.org>2025-03-16 11:15:56 +0000
commitd497e43a1664eaad5e5850f5f8c11c9e16f5ef66 (patch)
treef5e08d135e0754640157518802e078a54fe8a2e8
parent5f3b84a42199278114b6e3d95f406de05e0446f4 (diff)
parente714c3be9f8c459b1b81b212717e3693d2cb32df (diff)
downloadrust-d497e43a1664eaad5e5850f5f8c11c9e16f5ef66.tar.gz
rust-d497e43a1664eaad5e5850f5f8c11c9e16f5ef66.zip
Auto merge of #138551 - jieyouxu:rollup-ttktelm, r=jieyouxu
Rollup of 4 pull requests

Successful merges:

 - #135080 (core: Make `Debug` impl of raw pointers print metadata if present)
 - #137492 (libstd: rustdoc: correct note on fds 0/1/2 pre-main)
 - #137538 (fix doc path in std::fmt macro)
 - #138549 (Fix the OperandRef type for NullOp::{UbChecks,ContractChecks})

r? `@ghost`
`@rustbot` modify labels: rollup
-rw-r--r--compiler/rustc_codegen_ssa/src/mir/rvalue.rs2
-rw-r--r--compiler/rustc_middle/src/mir/statement.rs9
-rw-r--r--library/Cargo.lock26
-rw-r--r--library/core/src/fmt/mod.rs9
-rw-r--r--library/core/src/ptr/metadata.rs2
-rw-r--r--library/core/src/unit.rs16
-rw-r--r--library/coretests/Cargo.toml1
-rw-r--r--library/coretests/tests/fmt/mod.rs35
-rw-r--r--library/std/src/lib.rs4
-rw-r--r--library/std/src/macros.rs8
10 files changed, 103 insertions, 9 deletions
diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs
index 96be44ee09f..72cfd2bffb5 100644
--- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs
+++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs
@@ -747,7 +747,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
                 let tcx = self.cx.tcx();
                 OperandRef {
                     val: OperandValue::Immediate(val),
-                    layout: self.cx.layout_of(tcx.types.usize),
+                    layout: self.cx.layout_of(null_op.ty(tcx)),
                 }
             }
 
diff --git a/compiler/rustc_middle/src/mir/statement.rs b/compiler/rustc_middle/src/mir/statement.rs
index f05a798949b..d59b6df44ed 100644
--- a/compiler/rustc_middle/src/mir/statement.rs
+++ b/compiler/rustc_middle/src/mir/statement.rs
@@ -774,6 +774,15 @@ impl BorrowKind {
     }
 }
 
+impl<'tcx> NullOp<'tcx> {
+    pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
+        match self {
+            NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
+            NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
+        }
+    }
+}
+
 impl<'tcx> UnOp {
     pub fn ty(&self, tcx: TyCtxt<'tcx>, arg_ty: Ty<'tcx>) -> Ty<'tcx> {
         match self {
diff --git a/library/Cargo.lock b/library/Cargo.lock
index 104e0a3d010..ac874060514 100644
--- a/library/Cargo.lock
+++ b/library/Cargo.lock
@@ -85,6 +85,7 @@ version = "0.0.0"
 dependencies = [
  "rand",
  "rand_xorshift",
+ "regex",
 ]
 
 [[package]]
@@ -304,6 +305,31 @@ dependencies = [
 ]
 
 [[package]]
+name = "regex"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
+dependencies = [
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
+dependencies = [
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
+
+[[package]]
 name = "rustc-demangle"
 version = "0.1.24"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index a0f5b00cd2d..b13c7ee5aa2 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -2771,7 +2771,14 @@ impl Display for char {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized> Pointer for *const T {
     fn fmt(&self, f: &mut Formatter<'_>) -> Result {
-        pointer_fmt_inner(self.expose_provenance(), f)
+        if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
+            pointer_fmt_inner(self.expose_provenance(), f)
+        } else {
+            f.debug_struct("Pointer")
+                .field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f))
+                .field("metadata", &core::ptr::metadata(*self))
+                .finish()
+        }
     }
 }
 
diff --git a/library/core/src/ptr/metadata.rs b/library/core/src/ptr/metadata.rs
index 48707506389..9c5da306e27 100644
--- a/library/core/src/ptr/metadata.rs
+++ b/library/core/src/ptr/metadata.rs
@@ -61,6 +61,8 @@ pub trait Pointee {
     // NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata`
     // in `library/core/src/ptr/metadata.rs`
     // in sync with those here:
+    // NOTE: The metadata of `dyn Trait + 'a` is `DynMetadata<dyn Trait + 'a>`
+    // so a `'static` bound must not be added.
     type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze;
 }
 
diff --git a/library/core/src/unit.rs b/library/core/src/unit.rs
index d656005f3d4..d54816c444b 100644
--- a/library/core/src/unit.rs
+++ b/library/core/src/unit.rs
@@ -17,3 +17,19 @@ impl FromIterator<()> for () {
         iter.into_iter().for_each(|()| {})
     }
 }
+
+pub(crate) trait IsUnit {
+    fn is_unit() -> bool;
+}
+
+impl<T: ?Sized> IsUnit for T {
+    default fn is_unit() -> bool {
+        false
+    }
+}
+
+impl IsUnit for () {
+    fn is_unit() -> bool {
+        true
+    }
+}
diff --git a/library/coretests/Cargo.toml b/library/coretests/Cargo.toml
index e44f01d347b..88a7e159c95 100644
--- a/library/coretests/Cargo.toml
+++ b/library/coretests/Cargo.toml
@@ -25,3 +25,4 @@ test = true
 [dev-dependencies]
 rand = { version = "0.9.0", default-features = false }
 rand_xorshift = { version = "0.4.0", default-features = false }
+regex = { version = "1.11.1", default-features = false }
diff --git a/library/coretests/tests/fmt/mod.rs b/library/coretests/tests/fmt/mod.rs
index 025c69c4f62..cb185dae9de 100644
--- a/library/coretests/tests/fmt/mod.rs
+++ b/library/coretests/tests/fmt/mod.rs
@@ -15,8 +15,39 @@ fn test_format_flags() {
 fn test_pointer_formats_data_pointer() {
     let b: &[u8] = b"";
     let s: &str = "";
-    assert_eq!(format!("{s:p}"), format!("{:p}", s.as_ptr()));
-    assert_eq!(format!("{b:p}"), format!("{:p}", b.as_ptr()));
+    assert_eq!(format!("{s:p}"), format!("{:p}", s as *const _));
+    assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
+}
+
+#[test]
+fn test_fmt_debug_of_raw_pointers() {
+    use core::fmt::Debug;
+
+    fn check_fmt<T: Debug>(t: T, expected: &str) {
+        use std::sync::LazyLock;
+
+        use regex::Regex;
+
+        static ADDR_REGEX: LazyLock<Regex> =
+            LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());
+
+        let formatted = format!("{:?}", t);
+        let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");
+
+        assert_eq!(normalized, expected);
+    }
+
+    let plain = &mut 100;
+    check_fmt(plain as *mut i32, "$HEX");
+    check_fmt(plain as *const i32, "$HEX");
+
+    let slice = &mut [200, 300, 400][..];
+    check_fmt(slice as *mut [i32], "Pointer { addr: $HEX, metadata: 3 }");
+    check_fmt(slice as *const [i32], "Pointer { addr: $HEX, metadata: 3 }");
+
+    let vtable = &mut 500 as &mut dyn Debug;
+    check_fmt(vtable as *mut dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
+    check_fmt(vtable as *const dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
 }
 
 #[test]
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index 35ada678740..ad005833ad5 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -174,7 +174,9 @@
 //!
 //! - after-main use of thread-locals, which also affects additional features:
 //!   - [`thread::current()`]
-//! - before-main stdio file descriptors are not guaranteed to be open on unix platforms
+//! - under UNIX, before main, file descriptors 0, 1, and 2 may be unchanged
+//!   (they are guaranteed to be open during main,
+//!    and are opened to /dev/null O_RDWR if they weren't open on program start)
 //!
 //!
 //! [I/O]: io
diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs
index e0f9f0bb5ce..f008d42804c 100644
--- a/library/std/src/macros.rs
+++ b/library/std/src/macros.rs
@@ -41,7 +41,7 @@ macro_rules! panic {
 /// Use `print!` only for the primary output of your program. Use
 /// [`eprint!`] instead to print error and progress messages.
 ///
-/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
+/// See the formatting documentation in [`std::fmt`](crate::fmt)
 /// for details of the macro argument syntax.
 ///
 /// [flush]: crate::io::Write::flush
@@ -106,7 +106,7 @@ macro_rules! print {
 /// Use `println!` only for the primary output of your program. Use
 /// [`eprintln!`] instead to print error and progress messages.
 ///
-/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
+/// See the formatting documentation in [`std::fmt`](crate::fmt)
 /// for details of the macro argument syntax.
 ///
 /// [`std::fmt`]: crate::fmt
@@ -156,7 +156,7 @@ macro_rules! println {
 /// [`io::stderr`]: crate::io::stderr
 /// [`io::stdout`]: crate::io::stdout
 ///
-/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
+/// See the formatting documentation in [`std::fmt`](crate::fmt)
 /// for details of the macro argument syntax.
 ///
 /// # Panics
@@ -190,7 +190,7 @@ macro_rules! eprint {
 /// Use `eprintln!` only for error and progress messages. Use `println!`
 /// instead for the primary output of your program.
 ///
-/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
+/// See the formatting documentation in [`std::fmt`](crate::fmt)
 /// for details of the macro argument syntax.
 ///
 /// [`io::stderr`]: crate::io::stderr