Skip to content

fmt: Improve Debug for FormattingOptions - #166259

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
tgross35:formatoptions-debug
Nov 22, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
tgross35:formatoptions-debug

Conversation

@tgross35

@tgross35 tgross35 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

View all comments

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for "{my_item:x<+0123.456X?}":

FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}

@rustbot rustbot added A-fmt Area: `core::fmt` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 38 candidates
  • Random selection from JohnTitor, Mark-Simulacrum

@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Member Author

r? @hanna-kruppe, as it seems you may have skimmed this already. But feel free to reroll

@rustbot rustbot assigned hanna-kruppe and unassigned JohnTitor Nov 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

hanna-kruppe is currently at their maximum review capacity.
They may take a while to respond.

@tgross35
tgross35 force-pushed the formatoptions-debug branch from 896847d to 7085071 Compare November 20, 2026 14:13
@rustbot

This comment has been minimized.

Comment thread library/core/src/fmt/mod.rs Resolved
Comment thread library/core/src/fmt/mod.rs Outdated
.field("encoded_precision", &precision)
.field_with("specifier", |f| {
write!(f, "\"")?;
self.write_fmt_specifier(f)?;

@hanna-kruppe hanna-kruppe Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Having the specifier spelled like this is cute, I'd be happy to accept that despite needing some custom code. What I'm not so sure about is duplicating the same information in both fields and in the compact specifier. Is it really worth having both? I guess the fields match more directly what you get from the getters you'd use in your code.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I do like having both, the fields are much easier to read but the string hints make it easy to recreate. I think maybe if there was a Display impl it could just be the compact string, but that's probably not worth it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was briefly thinking this could be the alternate format, but since "alternate" is a flag, you can't use it to pick between these without changing the options that you're trying to inspect 🙃

Comment thread library/core/src/fmt/mod.rs Resolved
Comment thread library/core/src/fmt/mod.rs Resolved
Comment thread library/core/src/fmt/mod.rs Outdated
pub fn write_fmt_specifier(&self, f: &mut Formatter<'_>) -> Result {
let fill = self.get_fill();
if fill != ' ' {
write!(f, "{fill}")?;

@hanna-kruppe hanna-kruppe Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small edge case, maybe not worth worrying about: given format_args!("{:\">}") this will print the " unescaped, so FormattingOptions will print the invalid syntax "">". Similarly, fill == '\\' will be printed as "{\>}" which probably isn't valid syntax. Could fix that by using fill.escape_debug().

Arguably this means the "..." wrappers that FormattingOption adds should be part of this method, because if we do escaping then using it in other contexts might be wrong. Do you have other uses in mind?

Unfortunately, adding the " here would make the tests more repetitive. I guess this could be fixed by replacing assert_eq! by a helper function that compares to the debug representation of the expected string.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was pretty trivial to just add a bool toggle which avoids some of the " mess, so I did that and added tests.

Do you have other uses in mind?

I did avoid adding the quotes here originally because I figured it's not impossible that some Display impl for an error would want to print within { .. }, or the compiler may use it. But no, there's nothing concrete.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 20, 2026
@tgross35
tgross35 force-pushed the formatoptions-debug branch from 7085071 to a68197e Compare November 21, 2026 11:36
@rustbot

This comment has been minimized.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2026

@hanna-kruppe hanna-kruppe left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me after changing the commit message to not be interpreted as reference to #123 by Github and the bot. Maybe omit the alternate flag from the example so there's no #?

View changes since this review

The current implementation just includes the encoded flags. Add a custom
implementation that prints all the specifiers. Sample for
`"{my_item:x<+0123.456X?}"`:

    FormattingOptions {
        align: Some(Left),
        alternate: false,
        debug_as_hex: Some(Upper),
        fill: 'x',
        precision: Some(456),
        sign: Some(Plus),
        sign_aware_zero_pad: true,
        width: Some(123),
        specifier: "x<+0123.456X",
    }
@tgross35
tgross35 force-pushed the formatoptions-debug branch from a68197e to 42804ba Compare November 21, 2026 14:56
@tgross35

Copy link
Copy Markdown
Member Author

Thanks!

@bors r=hanna-kruppe

@rust-bors

rust-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 42804ba has been tentatively approved by hanna-kruppe

It will be put into the queue for this repository once PR CI succeeds.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: repeated failures in dist-x86_64-msvc

@tgross35

Copy link
Copy Markdown
Member Author

@bors rollup

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 21, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-aarch64-gnu-llvm-22-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 21, 2026
@rust-bors

rust-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

❌ Commit 42804ba has been unapproved due to PR CI failure. Reapprove it with @bors r+ force if you want to ignore the failure.

View changes since this unapproval

@tgross35

Copy link
Copy Markdown
Member Author

Could not connect to server (Failed to connect to static.crates.io:443 after 1052 ms: Could not connect to server)

Restarted CI

@bors r=hanna-kruppe rollup

@rust-bors

rust-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 42804ba has been tentatively approved by hanna-kruppe

It will be put into the queue for this repository once PR CI succeeds.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: repeated failures in dist-x86_64-msvc

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 21, 2026
…nna-kruppe

fmt: Improve `Debug` for `FormattingOptions`

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:

```rust
FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}
```
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166142 (Lower to `hir::Param`s for extern functions)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166269 (libcore: Revert some comment changes)
 - #166287 (Use `core::ptr` in `implicit_provenance_casts` help for `no_std` crates)
 - #166306 (bootstrap: Stop passing `-Ztrim-paths` to cargo now that it's stable in beta)
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 22, 2026
…nna-kruppe

fmt: Improve `Debug` for `FormattingOptions`

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:

```rust
FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}
```
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Rollup of 15 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #165720 (windows: remove redundant prefix parsing in `PathBuf::_push`)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
…nna-kruppe

fmt: Improve `Debug` for `FormattingOptions`

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:

```rust
FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
…nna-kruppe

fmt: Improve `Debug` for `FormattingOptions`

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:

```rust
FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}
```
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
@rust-bors
rust-bors Bot merged commit 32a65b4 into rust-lang:main Nov 22, 2026
19 of 27 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 22, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Rollup merge of #166259 - tgross35:formatoptions-debug, r=hanna-kruppe

fmt: Improve `Debug` for `FormattingOptions`

The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:

```rust
FormattingOptions {
    align: Some(Left),
    alternate: false,
    debug_as_hex: Some(Upper),
    fill: 'x',
    precision: Some(456),
    sign: Some(Plus),
    sign_aware_zero_pad: true,
    width: Some(123),
    specifier: "x<+0123.456X",
}
```
@tgross35
tgross35 deleted the formatoptions-debug branch November 23, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmt Area: `core::fmt` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants