Skip to content

Stabilize debug_closure_helpers - #146099

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
coolreader18:stabilize-debug_closure_helpers
Oct 5, 2026
Merged

rust-bors[bot] merged 3 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
@kornelski kornelski mentioned this pull request Sep 6, 2025
6 of 7 tasks
@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.

@coolreader18
coolreader18 force-pushed the stabilize-debug_closure_helpers branch from fc4f85a to aa70274 Compare September 28, 2026 17:33
@rustbot

This comment has been minimized.

@clarfonthey

clarfonthey commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

r=me once FCP passes, but please clean up the git history

FYI: bors squash is a thing, if that's ever all that's blocking merge.

@tgross35

Copy link
Copy Markdown
Member

Yeah if one commit made sense then I would, but I figured the author may still separate updates from stabilization (as they did)

@rust-bors

This comment has been minimized.

@rust-rfcbot rust-rfcbot added 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. 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. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Oct 3, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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.

@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot author

Just needs a rebase.

@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 Oct 4, 2026
@rustbot

rustbot commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@coolreader18
coolreader18 force-pushed the stabilize-debug_closure_helpers branch from aa70274 to 384b61a Compare October 4, 2026 21:35
@rustbot

rustbot commented Oct 4, 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.

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r=tgross35 rollup

Thank you!

@rust-bors

rust-bors Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 384b61a has been approved by tgross35

It is now in the queue for this repository.

@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 Oct 4, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 5, 2026
Rollup of 2 pull requests

Successful merges:

 - #146099 (Stabilize `debug_closure_helpers`)
 - #163755 (fix -Z track-diagnostics for errors and lints emitted from rustc_attr_parsing)
@rust-bors
rust-bors Bot merged commit 952bbd0 into rust-lang:main Oct 5, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Oct 5, 2026
Rollup merge of #146099 - coolreader18:stabilize-debug_closure_helpers, r=tgross35

Stabilize `debug_closure_helpers`

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 this to the 1.101.0 milestone Oct 5, 2026
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. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking Issue for debug_closure_helpers