Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug implementation of fat pointers doesn't show metadata #128684

Closed
ia0 opened this issue Aug 5, 2024 · 2 comments · Fixed by #135080
Closed

Debug implementation of fat pointers doesn't show metadata #128684

ia0 opened this issue Aug 5, 2024 · 2 comments · Fixed by #135080
Assignees
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ia0
Copy link
Contributor

ia0 commented Aug 5, 2024

I tried this code:

fn main() {
    let mut x = *b"hello";
    let p: &mut [u8] = &mut x;
    let q: *mut [u8] = &mut p[..3];
    assert_eq!(p as *mut [u8], q);
}

I expected to see this happen:

assertion `left == right` failed
  left: { ptr: 0x7ffdd41d89f3, meta: 5 }
 right: { ptr: 0x7ffdd41d89f3, meta: 3 }

Instead, this happened:

assertion `left == right` failed
  left: 0x7ffdd41d89f3
 right: 0x7ffdd41d89f3

Note: The pointer address is non-deterministic, so the output may differ. What matters is that the pointer address is the same, only the metadata differ.

Meta

rustc --version --verbose:

rustc 1.82.0-nightly (f8060d282 2024-07-30)
binary: rustc
commit-hash: f8060d282d42770fadd73905e3eefb85660d3278
commit-date: 2024-07-30
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 18.1.7
Backtrace

thread 'main' panicked at src/main.rs:5:5:
assertion `left == right` failed
  left: 0x7ffca57ef663
 right: 0x7ffca57ef663
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f8060d282d42770fadd73905e3eefb85660d3278/library/std/src/panicking.rs:662:5
   1: core::panicking::panic_fmt
             at /rustc/f8060d282d42770fadd73905e3eefb85660d3278/library/core/src/panicking.rs:74:14
   2: core::panicking::assert_failed_inner
             at /rustc/f8060d282d42770fadd73905e3eefb85660d3278/library/core/src/panicking.rs:412:17
   3: core::panicking::assert_failed
             at /rustc/f8060d282d42770fadd73905e3eefb85660d3278/library/core/src/panicking.rs:367:5
   4: foo::main
             at ./src/main.rs:5:5
   5: core::ops::function::FnOnce::call_once
             at /rustc/f8060d282d42770fadd73905e3eefb85660d3278/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@ia0 ia0 added the C-bug Category: This is a bug. label Aug 5, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 5, 2024
@lolbinarycat lolbinarycat added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 5, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 18, 2024
@Enselic
Copy link
Member

Enselic commented Jan 4, 2025

@rustbot claim

See #135080.

@rustbot

This comment has been minimized.

@bors bors closed this as completed in a23a93c Mar 16, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 16, 2025
Rollup merge of rust-lang#135080 - Enselic:debug-ptr-metadata, r=thomcc

core: Make `Debug` impl of raw pointers print metadata if present

Make Rust pointers appear less magic by including metadata information in their `Debug` output.

This does not break Rust stability guarantees because `Debug` impl are explicitly exempted from stability:
https://doc.rust-lang.org/std/fmt/trait.Debug.html#stability

> ## Stability
>
> Derived `Debug` formats are not stable, and so may change with future Rust versions. Additionally, `Debug` implementations of types provided by the standard library (`std`, `core`, `alloc`, etc.) are not stable, and may also change with future Rust versions.

Note that a regression test is added as a separate commit to make it clear what impact the last commit has on the output.

Closes rust-lang#128684 because the output of that code now becomes:

```
thread 'main' panicked at src/main.rs:5:5:
assertion `left == right` failed
  left: Pointer { addr: 0x7ffd45c6fc6b, metadata: 5 }
 right: Pointer { addr: 0x7ffd45c6fc6b, metadata: 3 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this issue Mar 19, 2025
core: Make `Debug` impl of raw pointers print metadata if present

Make Rust pointers appear less magic by including metadata information in their `Debug` output.

This does not break Rust stability guarantees because `Debug` impl are explicitly exempted from stability:
https://doc.rust-lang.org/std/fmt/trait.Debug.html#stability

> ## Stability
>
> Derived `Debug` formats are not stable, and so may change with future Rust versions. Additionally, `Debug` implementations of types provided by the standard library (`std`, `core`, `alloc`, etc.) are not stable, and may also change with future Rust versions.

Note that a regression test is added as a separate commit to make it clear what impact the last commit has on the output.

Closes rust-lang#128684 because the output of that code now becomes:

```
thread 'main' panicked at src/main.rs:5:5:
assertion `left == right` failed
  left: Pointer { addr: 0x7ffd45c6fc6b, metadata: 5 }
 right: Pointer { addr: 0x7ffd45c6fc6b, metadata: 3 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants