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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Enselic
Copy link
Member

@Enselic Enselic commented Jan 3, 2025

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 #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

@rustbot
Copy link
Collaborator

rustbot commented Jan 3, 2025

r? @BoxyUwU

rustbot has assigned @BoxyUwU.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 3, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 3, 2025

r? libs
lol

@rustbot rustbot assigned Amanieu and unassigned BoxyUwU Jan 3, 2025
@rust-log-analyzer

This comment has been minimized.

@Enselic Enselic force-pushed the debug-ptr-metadata branch from b628909 to ee8187a Compare January 3, 2025 19:56
@rust-log-analyzer

This comment has been minimized.

@Enselic Enselic force-pushed the debug-ptr-metadata branch from ee8187a to 4c641b1 Compare January 4, 2025 09:20
@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jan 4, 2025
@rustbot

This comment was marked as outdated.

@bors
Copy link
Contributor

bors commented Jan 27, 2025

☔ The latest upstream changes (presumably #135937) made this pull request unmergeable. Please resolve the merge conflicts.

@Enselic Enselic force-pushed the debug-ptr-metadata branch from 4c641b1 to d5acbdd Compare January 27, 2025 19:39
@Enselic
Copy link
Member Author

Enselic commented Jan 29, 2025

Amanieu seems busy. Re-rolling reviewer.

r? libs

@rustbot rustbot assigned thomcc and unassigned Amanieu Jan 29, 2025
@thomcc thomcc removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 29, 2025
@thomcc
Copy link
Member

thomcc commented Jan 29, 2025

This needs an FCP.

@rustbot fcp merge

@thomcc
Copy link
Member

thomcc commented Jan 29, 2025

Oh right, wrong bot.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Jan 29, 2025

Team member @thomcc has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jan 29, 2025
@@ -2776,7 +2776,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 core::mem::size_of::<<T as core::ptr::Pointee>::Metadata>() == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit strange to check the size of the metadata. Could we instead explicitly check that the metadata type is ()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing that at first but couldn't figure out how. I tried a bit more now but without success. I can't find any other place in the compiler/libraries that does that either.

Maybe you had some particular technique in mind?

Personally I get peace of mind thinking of this logic as "if the metadata has no size there is nothing of interest to show".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we added a 'static requirement to Pointee::Metadata, then you could compare TypeId. I'm not sure if there's any good reason it doesn't have that already.

Copy link
Member Author

@Enselic Enselic Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that seems to work. I think I can push a commit for that to see if CI passes without disrupting the FCP?

Edit: I'm pretty sure, so I'll do it shortly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointee::Metadata can't be 'static, because the metadata of dyn Trait + 'a is DynMetadata<dyn Trait + 'a> which is not 'static.

To me it seems safer to drop the 'static bound from the type_id intrinsic rather than potentially causing a trait system unsoundness by adding a 'static bound that isn't actually satisfied by the compiler-builtin impls.

Copy link
Member Author

@Enselic Enselic Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Out of interest I'll let CI continue to see if it catches it , and then go back to the code I had when the FCP started.

Trying to drop the 'static bound from the type_id intrinsic sounds like out of scope for this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a local specialization:

trait IsUnit {
    fn is_unit() -> bool;
}

impl<T: ?Sized> IsUnit for T {
    #[inline(always)]
    default fn is_unit() -> bool { false }
}

impl IsUnit for () {
    #[inline(always)]
    fn is_unit() -> bool { true }
}

(It would be better as an associated const, but I don't think min_specialization allows that.)

Copy link
Member Author

@Enselic Enselic Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I now

  • check for () with IsUnit instead of size_of() == 0
  • have added a comment for future adventurers that explains why Pointee::Metadata can't be 'static
  • have rebased on master to resolve the latest conflict

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Feb 12, 2025
@rfcbot
Copy link

rfcbot commented Feb 12, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@Enselic Enselic force-pushed the debug-ptr-metadata branch 2 times, most recently from b772f99 to 9e81efa Compare February 13, 2025 20:45
@bors

This comment was marked as resolved.

Because `.as_ptr()` changes the type of the pointer (e.g. `&[u8]`
becomes `*const u8` instead of `*const [u8]`), and it can't be expected
that different types will be formatted the same way.
@Enselic Enselic force-pushed the debug-ptr-metadata branch 2 times, most recently from 68cd706 to 892bb5f Compare February 15, 2025 16:27
Make Rust pointers less magic by including metadata information in their
`Debug` output.

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

Co-authored-by: Lukas <26522220+lukas-code@users.noreply.github.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 22, 2025
@rfcbot
Copy link

rfcbot commented Feb 22, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-libs Relevant to the library team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debug implementation of fat pointers doesn't show metadata