-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
base: master
Are you sure you want to change the base?
Conversation
r? libs |
This comment has been minimized.
This comment has been minimized.
b628909
to
ee8187a
Compare
This comment has been minimized.
This comment has been minimized.
ee8187a
to
4c641b1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
☔ The latest upstream changes (presumably #135937) made this pull request unmergeable. Please resolve the merge conflicts. |
4c641b1
to
d5acbdd
Compare
Amanieu seems busy. Re-rolling reviewer. r? libs |
This needs an FCP. @rustbot fcp merge |
Oh right, wrong bot. @rfcbot fcp merge |
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. |
library/core/src/fmt/mod.rs
Outdated
@@ -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 { |
There was a problem hiding this comment.
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 ()
?
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
()
withIsUnit
instead ofsize_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
🔔 This is now entering its final comment period, as per the review above. 🔔 |
b772f99
to
9e81efa
Compare
9e81efa
to
dab5a8f
Compare
This comment was marked as resolved.
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.
dab5a8f
to
785f82d
Compare
Co-authored-by: Lukas <26522220+lukas-code@users.noreply.github.com>
68cd706
to
892bb5f
Compare
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>
892bb5f
to
697737a
Compare
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. |
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
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: