Skip to content

Commit d497e43

Browse files
committed
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
2 parents 5f3b84a + e714c3b commit d497e43

File tree

10 files changed

+103
-9
lines changed

10 files changed

+103
-9
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
747747
let tcx = self.cx.tcx();
748748
OperandRef {
749749
val: OperandValue::Immediate(val),
750-
layout: self.cx.layout_of(tcx.types.usize),
750+
layout: self.cx.layout_of(null_op.ty(tcx)),
751751
}
752752
}
753753

compiler/rustc_middle/src/mir/statement.rs

+9
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@ impl BorrowKind {
774774
}
775775
}
776776

777+
impl<'tcx> NullOp<'tcx> {
778+
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
779+
match self {
780+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
781+
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
782+
}
783+
}
784+
}
785+
777786
impl<'tcx> UnOp {
778787
pub fn ty(&self, tcx: TyCtxt<'tcx>, arg_ty: Ty<'tcx>) -> Ty<'tcx> {
779788
match self {

library/Cargo.lock

+26
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ version = "0.0.0"
8585
dependencies = [
8686
"rand",
8787
"rand_xorshift",
88+
"regex",
8889
]
8990

9091
[[package]]
@@ -303,6 +304,31 @@ dependencies = [
303304
"rand_core",
304305
]
305306

307+
[[package]]
308+
name = "regex"
309+
version = "1.11.1"
310+
source = "registry+https://github.com/rust-lang/crates.io-index"
311+
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
312+
dependencies = [
313+
"regex-automata",
314+
"regex-syntax",
315+
]
316+
317+
[[package]]
318+
name = "regex-automata"
319+
version = "0.4.9"
320+
source = "registry+https://github.com/rust-lang/crates.io-index"
321+
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
322+
dependencies = [
323+
"regex-syntax",
324+
]
325+
326+
[[package]]
327+
name = "regex-syntax"
328+
version = "0.8.5"
329+
source = "registry+https://github.com/rust-lang/crates.io-index"
330+
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
331+
306332
[[package]]
307333
name = "rustc-demangle"
308334
version = "0.1.24"

library/core/src/fmt/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2771,7 +2771,14 @@ impl Display for char {
27712771
#[stable(feature = "rust1", since = "1.0.0")]
27722772
impl<T: ?Sized> Pointer for *const T {
27732773
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2774-
pointer_fmt_inner(self.expose_provenance(), f)
2774+
if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() {
2775+
pointer_fmt_inner(self.expose_provenance(), f)
2776+
} else {
2777+
f.debug_struct("Pointer")
2778+
.field_with("addr", |f| pointer_fmt_inner(self.expose_provenance(), f))
2779+
.field("metadata", &core::ptr::metadata(*self))
2780+
.finish()
2781+
}
27752782
}
27762783
}
27772784

library/core/src/ptr/metadata.rs

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ pub trait Pointee {
6161
// NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata`
6262
// in `library/core/src/ptr/metadata.rs`
6363
// in sync with those here:
64+
// NOTE: The metadata of `dyn Trait + 'a` is `DynMetadata<dyn Trait + 'a>`
65+
// so a `'static` bound must not be added.
6466
type Metadata: fmt::Debug + Copy + Send + Sync + Ord + Hash + Unpin + Freeze;
6567
}
6668

library/core/src/unit.rs

+16
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ impl FromIterator<()> for () {
1717
iter.into_iter().for_each(|()| {})
1818
}
1919
}
20+
21+
pub(crate) trait IsUnit {
22+
fn is_unit() -> bool;
23+
}
24+
25+
impl<T: ?Sized> IsUnit for T {
26+
default fn is_unit() -> bool {
27+
false
28+
}
29+
}
30+
31+
impl IsUnit for () {
32+
fn is_unit() -> bool {
33+
true
34+
}
35+
}

library/coretests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ test = true
2525
[dev-dependencies]
2626
rand = { version = "0.9.0", default-features = false }
2727
rand_xorshift = { version = "0.4.0", default-features = false }
28+
regex = { version = "1.11.1", default-features = false }

library/coretests/tests/fmt/mod.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,39 @@ fn test_format_flags() {
1515
fn test_pointer_formats_data_pointer() {
1616
let b: &[u8] = b"";
1717
let s: &str = "";
18-
assert_eq!(format!("{s:p}"), format!("{:p}", s.as_ptr()));
19-
assert_eq!(format!("{b:p}"), format!("{:p}", b.as_ptr()));
18+
assert_eq!(format!("{s:p}"), format!("{:p}", s as *const _));
19+
assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
20+
}
21+
22+
#[test]
23+
fn test_fmt_debug_of_raw_pointers() {
24+
use core::fmt::Debug;
25+
26+
fn check_fmt<T: Debug>(t: T, expected: &str) {
27+
use std::sync::LazyLock;
28+
29+
use regex::Regex;
30+
31+
static ADDR_REGEX: LazyLock<Regex> =
32+
LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());
33+
34+
let formatted = format!("{:?}", t);
35+
let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");
36+
37+
assert_eq!(normalized, expected);
38+
}
39+
40+
let plain = &mut 100;
41+
check_fmt(plain as *mut i32, "$HEX");
42+
check_fmt(plain as *const i32, "$HEX");
43+
44+
let slice = &mut [200, 300, 400][..];
45+
check_fmt(slice as *mut [i32], "Pointer { addr: $HEX, metadata: 3 }");
46+
check_fmt(slice as *const [i32], "Pointer { addr: $HEX, metadata: 3 }");
47+
48+
let vtable = &mut 500 as &mut dyn Debug;
49+
check_fmt(vtable as *mut dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
50+
check_fmt(vtable as *const dyn Debug, "Pointer { addr: $HEX, metadata: DynMetadata($HEX) }");
2051
}
2152

2253
#[test]

library/std/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@
174174
//!
175175
//! - after-main use of thread-locals, which also affects additional features:
176176
//! - [`thread::current()`]
177-
//! - before-main stdio file descriptors are not guaranteed to be open on unix platforms
177+
//! - under UNIX, before main, file descriptors 0, 1, and 2 may be unchanged
178+
//! (they are guaranteed to be open during main,
179+
//! and are opened to /dev/null O_RDWR if they weren't open on program start)
178180
//!
179181
//!
180182
//! [I/O]: io

library/std/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ macro_rules! panic {
4141
/// Use `print!` only for the primary output of your program. Use
4242
/// [`eprint!`] instead to print error and progress messages.
4343
///
44-
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
44+
/// See the formatting documentation in [`std::fmt`](crate::fmt)
4545
/// for details of the macro argument syntax.
4646
///
4747
/// [flush]: crate::io::Write::flush
@@ -106,7 +106,7 @@ macro_rules! print {
106106
/// Use `println!` only for the primary output of your program. Use
107107
/// [`eprintln!`] instead to print error and progress messages.
108108
///
109-
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
109+
/// See the formatting documentation in [`std::fmt`](crate::fmt)
110110
/// for details of the macro argument syntax.
111111
///
112112
/// [`std::fmt`]: crate::fmt
@@ -156,7 +156,7 @@ macro_rules! println {
156156
/// [`io::stderr`]: crate::io::stderr
157157
/// [`io::stdout`]: crate::io::stdout
158158
///
159-
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
159+
/// See the formatting documentation in [`std::fmt`](crate::fmt)
160160
/// for details of the macro argument syntax.
161161
///
162162
/// # Panics
@@ -190,7 +190,7 @@ macro_rules! eprint {
190190
/// Use `eprintln!` only for error and progress messages. Use `println!`
191191
/// instead for the primary output of your program.
192192
///
193-
/// See [the formatting documentation in `std::fmt`](../std/fmt/index.html)
193+
/// See the formatting documentation in [`std::fmt`](crate::fmt)
194194
/// for details of the macro argument syntax.
195195
///
196196
/// [`io::stderr`]: crate::io::stderr

0 commit comments

Comments
 (0)