Skip to content

Stabilize debug_closure_helpers - #146099

Open
coolreader18 wants to merge 5 commits into
rust-lang:mainfrom
coolreader18:stabilize-debug_closure_helpers
Open

coolreader18 wants to merge 5 commits into
rust-lang:mainfrom
coolreader18:stabilize-debug_closure_helpers

Conversation

@coolreader18

@coolreader18 coolreader18 commented Sep 1, 2025 •

Copy link
Copy Markdown
Contributor

View all comments

Resolves #117729. The tracking issue still needs an FCP, but I'm hoping that creating a stabilization PR will prompt one.

r? libs-api

@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Sep 1, 2025
@coolreader18 coolreader18 changed the title Stabilize debug closure helpers Stabilize debug_closure_helpers Sep 1, 2025
@tgross35 tgross35 added the I-libs-api-nominated [DEPRECATED; DO NOT USE] label Sep 2, 2025
@Amanieu Amanieu added I-libs-api-nominated [DEPRECATED; DO NOT USE] and removed I-libs-api-nominated [DEPRECATED; DO NOT USE] labels Sep 23, 2025
@bors

bors commented Sep 27, 2025

Copy link
Copy Markdown
Collaborator

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

@Amanieu Amanieu removed the I-libs-api-nominated [DEPRECATED; DO NOT USE] label Sep 30, 2025
@coolreader18

Copy link
Copy Markdown
Contributor Author

Once the FCP is complete, I think it might make sense to merge #145915 first and then merge this on top of that.

Comment thread library/core/src/fmt/builders.rs Outdated
Comment on lines 141 to 143
pub fn field_with<F>(&mut self, name: &str, value_fmt: F) -> &mut Self
where
F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result,

@hanna-kruppe hanna-kruppe Oct 20, 2025 •

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 closure as a generic parameter (and not type-erasing it internally) leads to a lot of code bloat compared to the corresponding stable APIs that take &dyn Debug as value to format. According to cargo +nightly llvm-lines a simple program formatting a struct with two fields generates 694 lines of LLVM IR while the same program using field() generates 42 lines. So at least with the current implementation, these functions have an annoying downside compared to e.g. .field("foo", &fmt::from_fn(|f| ...)

I think this can be addressed after stabilization, without changing the signatures. But I wanted to flag it so the reviewer can think about it as well. It's not entirely obvious to me for the helpers that deal in FnOnce. I'm aware of one workaround (put the closure into an Option, then pass a &dyn FnMut that unwraps this option to call the original impl FnOnce) but it's not quite zero cost in several dimensions.

@jmillikin jmillikin Oct 21, 2025 •

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.

Could this be solved by putting the main logic back into field(), and having field_with be an #[inline] wrapper around it?

I don't want to send in any PRs that might cause Git conflicts with the stabilization, but if the public API is good then I'd be happy to experiment with internal reorganization / optimization after both have landed.

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.

field takes a &dyn Debug. How do you invoke a FnOnce or FnMut from the &self argument of Debug::fmt?

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.

Cell<Option<F>> , then .replace(None).unwrap()? It doesn't need to survive more than one call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think if field_with were changed to use dyn, it'd be important to ensure that it doesn't cause undue stack usage, since that was brought up as an issue with the existing functions in #117729 (comment)

@hanna-kruppe hanna-kruppe Oct 22, 2025 •

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.

That report is in the context of a total stack size of 4 KiB. The DebugStruct::field function in my nightly toolchain's libcore.rlib allocates a very reasonable amount of stack space (add $0x48,%rsp). It then goes on to call other functions, but those seem to be functions shared with most of the formatting infrastructure. It's frankly a miracle that any variation of the code fits within a 4 KiB stack, and the cases that work probably depends on the code being duplicated and specialized a bit for the particular usage, which is fundamentally at odds with optimizing for code size (as core::fmt generally does). So I wouldn't give much weight to that report.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@hanna-kruppe would you mind creating an issue for this? And/or a PR to change it? I think you're probably the best equipped to do so.

I'm ready to approve this but would like your confirmation that we have room to improve this within the current design.

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 can extract my above comment into an issue but I probably won't have time to try out a fix in the next 2-3 weeks. There's definitely room for improvement (e.g., the Cell<Option<F>>-via-&dyn Fn() approach discussed above). But without trying it out, I can't quantify how much improvement it is.

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.

Created #149745

@jmillikin

Copy link
Copy Markdown
Contributor

The stabilization PR for fmt::from_fn has merged, so this one is now ready to rebase.

gentle ping @the8472 for review

@coolreader18
coolreader18 force-pushed the stabilize-debug_closure_helpers branch from a51fcbe to b1bde94 Compare November 4, 2025 18:52
@rustbot

This comment has been minimized.

@bors

bors commented Nov 6, 2025

Copy link
Copy Markdown
Collaborator

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

@coolreader18
coolreader18 force-pushed the stabilize-debug_closure_helpers branch from b1bde94 to 1e39a61 Compare November 6, 2025 02:49
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jmillikin

Copy link
Copy Markdown
Contributor

@bors retry

@bors

bors commented Nov 6, 2025

Copy link
Copy Markdown
Collaborator

@jmillikin: 🔑 Insufficient privileges: not in try users

@jmillikin

Copy link
Copy Markdown
Contributor

@the8472 gentle ping?

@jmillikin

Copy link
Copy Markdown
Contributor

r? libs-api

@rustbot rustbot added the T-libs-api [DEPRECATED; DO NOT USE] label Dec 1, 2025
@rustbot rustbot assigned BurntSushi and unassigned the8472 Dec 1, 2025
@bors

bors commented Dec 6, 2025

Copy link
Copy Markdown
Collaborator

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

@tgross35

tgross35 commented Dec 6, 2025

Copy link
Copy Markdown
Member

r? tgross35

Fyi you can r? libs rather than libs-api for stabilization PRs, libs does more reviews. libs-api just needs to do the FCP.

The diff here LGTM, but I'd just like to make sure we have room to improve on what Hanna mentioned.

rust-bors Bot pushed a commit that referenced this pull request Sep 15, 2026
… r=<try>

Stabilize `debug_closure_helpers`
@rust-bors

rust-bors Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ea402e4 (ea402e47e2330450b216b82a6220fb0fb78150f3)
Base parent: 5392d2f (5392d2f545c6836dc79f209bcc14ac7179dbc4f2)

@clarfonthey

Copy link
Copy Markdown
Contributor

(I mean, we can still rebase, but it seems fine.)

@coolreader18

Copy link
Copy Markdown
Contributor Author

I just rebased it 2 weeks ago, to fix merge conflicts.

@rust-rfcbot rust-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 Sep 15, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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

Comment thread library/core/src/fmt/builders.rs Outdated
Comment on lines 973 to 976

@tgross35 tgross35 Sep 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

key and value have these panic sections. They should be updated here to mention key_with and value_with, and key_with and value_with should get the same thing.

Expect a minor conflict with #162825 if that merges first.

View changes since the review

@tgross35

Copy link
Copy Markdown
Member

Wish I had noticed this earlier: we specify F: FnOnce here, but a lot of API has been moving to impl FnOnce instead. Is this something we want to change? I have a slight preference for switching to impl FnOnce since that's technically less surface area and we can always add back the F if it's actually useful.

Nominating since this is already in FCP.

@rustbot label +I-libs-nominated

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 15, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

(Note: if you want to make that concern blocking, would need someone with checkbox privs to register it, but you can do that as a libs member.)

@rust-bors

This comment has been minimized.

@nia-e

nia-e commented Sep 22, 2026

Copy link
Copy Markdown
Member

on behalf of @tgross35

@rfcbot concern impl-fnonce

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

nia-e commented Sep 22, 2026

Copy link
Copy Markdown
Member

team consensus is to change to using the impl per Trevor's comment - once that's done, we can restart fcp ^^ ty!

@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 22, 2026
@coolreader18
coolreader18 force-pushed the stabilize-debug_closure_helpers branch from c47f842 to 38fed83 Compare September 23, 2026 19:12
@rustbot

rustbot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@nia-e

nia-e commented Sep 23, 2026

Copy link
Copy Markdown
Member

@rfcbot resolve impl-fnonce

@rust-rfcbot rust-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 Sep 23, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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

@rust-log-analyzer

This comment has been minimized.

Comment thread library/core/src/fmt/builders.rs
Comment thread library/core/src/fmt/builders.rs
Comment thread library/core/src/fmt/builders.rs Outdated

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. 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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking Issue for debug_closure_helpers