-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Extend and expose API for {:x?} and {:X?} formatting #99138
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
449d5f5
to
5626b02
Compare
This comment has been minimized.
This comment has been minimized.
I keep having issues testing this locally, since Edit: Oh, yeah, nope, this doesn't fix it:
|
5626b02
to
536d834
Compare
This comment has been minimized.
This comment has been minimized.
This is because the LLVM version used in CI is older or newer than the version you're using. I also don't think the failure matters for you. You can (most probably) ignore it. You have however some compilation failures, those failing tests should probably be gated behind |
This comment has been minimized.
This comment has been minimized.
d95c24e
to
ad2b5e6
Compare
This comment has been minimized.
This comment has been minimized.
c6489d7
to
a38651e
Compare
you can check |
My main issue is that the parsing code doesn't have any access to any of the compiler code, and ditto for the core library code. I'm not sure where in the code the two are linked together, and had trouble finding it. Might take some more time over the weekend to poke around more, though. |
ah, sure - I think there's a |
So, looking at this, I'm pretty confident that this should get some review from at least T-libs-api, and maybe also T-lang, since based on the description, this is insta-stably extending the format system to allow special treatment for all of I'm tagging with both of those teams, and nominating it for both of those teams so that they can explicitly untag themselves if they do not think this is in their scope. @rustbot label: I-lang-nominated T-lang T-libs-api I-libs-api-nominated |
We discussed this in today's lang meeting, and agreed that it was the purview of libs-api. |
We discussed this last week in the library api meeting, and we're not sure if we want this. We're a bit worried about the inconsistency between hex/octal/decimal/etc. as flags, vs as different traits, and unsure about the practical value. I've assigned this to myself for me to review and analyse the impact of this change and potential downsides. |
So, it's worth mentioning that the trait system wouldn't even be able to capture the nuance of these flags because there are cases where one field might be able to display one way, but not the others. For example, It's unfortunate that we can't detect whether a flag would have no effect, but that feels like something that a clippy lint might be able to handle. At least from the perspective of a user, it's weird that hex formatting is given an elevated status over octal and binary, since especially in my case, I needed binary but not hex. I can see the Basically: I agree that there may be a better design, but the existing design has been stabilised and I'd like to try and make it work for more than just hex formatting, and this to me seems like the only reasonable way to do it. |
☔ The latest upstream changes (presumably #100920) made this pull request unmergeable. Please resolve the merge conflicts. |
a38651e
to
de31b5e
Compare
This comment has been minimized.
This comment has been minimized.
de31b5e
to
e257b57
Compare
This comment has been minimized.
This comment has been minimized.
e257b57
to
ceace40
Compare
Resolved the merge conflicts anyway, whether review deems this worth merging or not. |
Wondering what the status of this should be. I could try and investigate feature gating this, open up an ACP, or just close it. I would like to see this available, but understand the apprehension in merging it as-is. |
This is stuck waiting for me, sorry about that. I've finally been able to focus a bit on format_args!() again this week, so I'll probably get to this PR soon. |
Understandable; I remember that the last comment mentioned apprehension about including this, so, it makes sense that it got lost in the pool of other issues. I think that variants other than hex makes sense to include in one way or another, although I think the loosest justification is |
I dumped some of my thoughts on Zulip. Copied here: it's a bit of a weird situation now: in assert_eq!(format!("{:x?}", (10, 20)), "(a, 14)");
assert_eq!(format!("{:#x?}", (10, 20)), "(\n 0xa,\n 0x14,\n)"); (other flags are also passed on to the individual elements: assert_eq!(format!("{:04x?}", (1, 2, 3)), "(0001, 0002, 0003)"); but at least those only apply to the elements, not also to the outer layer.) this can be useful sometimes. but it feels like a bit of a dirty hack. we don't have any way of specifying flags separately for the elements/fields or for the tuple/array/struct/whatever itself, like some other languages do. and now it's feels a bit random what exactly a flag applies to we teach people that (and it's also not great for code size in tiny programs, because so it feels to me like the current situation is not great, and adding It looks like this feature never went through an unstable period. the RFC went through FCP, but then #48978 was merged directly without feature gate or FCP. |
So tl;dr: I don't think we're feeling quite sure of the current situation and what to do with it. We should figure that out before extending it to |
☔ The latest upstream changes (presumably #107372) made this pull request unmergeable. Please resolve the merge conflicts. |
I think I'm going to close this for now since @m-ou-se is doing a lot of work on |
@clarfonthey I've posted a summary of the libs-api discussion about |
RFC 2226, originally tracked in #48584, added the
x?
andX?
debug formats, which allow printing integers as hexadecimal even in debug contexts.There was some discussion on exposing this to downstream APIs in both the tracking issue and the RFC, and ultimately, the tracking issue was closed without any progress on this. In fact, the code still references the now-closed tracking issue when asking how this should be exposed.
This takes the approach of expanding the RFC to all formatting traits, such that
o?
,b?
,e?
,E?
, andp?
are all possible. It uses these as applicable for all the types in the library crates, assuming I didn't miss any.This is just a hint, meaning that it can safely be ignored by any debug implementations. For example,
format!("{:x?}", Some("hello"))
is still allowed, and it simply ignores thex
specifier.This implementation replaces the existing flags used for
DebugLowerHex
andDebugUpperHex
with a four-bit bitfield on the formatting flags. It exposes this via a newDebugVariant
field, which states what variant is used inDebug
implementations, defaulting toDisplay
if none is specified. For non-debug flags, it will always showDisplay
, but we could potentially change that to show the current format.Currently, this insta-stabilises the ability to use these variants in formatting (
format!("{var:p?}")
now works) but still leaves theFormatter::debug_variant
method andDebugVariant
enum as unstable under thedebug_variants
feature. This is mostly because I have no idea how we'd actually gate the parsing under the feature; if this is desired, I'd need some guidance on how to accomplish that.Side note, this actually changes the layout of the flags for the hex-debug variants, which may be undesired. I assume this is okay since the
flags
method is deprecated, but will modify it to be backwards-compatible if this is a requirement. My thought process was that it would be better to add some padding to separate out the flags a bit better (allow 8 boolean flags before moving onto bitfield for variant), but this is honestly a weak justification. I'm not sure how much code out in the wild is usingflags
due to the lack of API for the existing hex-debug variants.