Skip to content

Fix cfg_if! error count leaks - #7195

Open
jieyouxu wants to merge 3 commits into
rust-lang:mainfrom
jieyouxu:jieyouxu/cfg-if
Open

jieyouxu wants to merge 3 commits into
rust-lang:mainfrom
jieyouxu:jieyouxu/cfg-if

Conversation

@jieyouxu

@jieyouxu jieyouxu commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #7171, essentially the same leaking of shared error count state problem.

Fixes #7184.

Formatting stability

I believe this technically impacts stable cfg_if! formatting, but I consider it acceptable because the previous behavior was just buggy.

Notes for the reviewer

  • There's some iffy-ness with parse_expr/parse_lazy_static/rewrite_macro that I think has similar-ish error count leak bugs. I didn't want to fix them here in the same PR. The helper introduced here could be used (or modified) in follow-ups.
  • I want to keep this PR relatively self-contained since it impacts cfg_if! stable formatting, don't want to address Reset error counts on synthetic parser constructions #7180 in this PR.
  • Commit {1, 2} are for the cfg_if! fix, commit 3 is the refactor to pull out a shared helper.

  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

Used an LLM for co-review only.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Oct 17, 2026
@jieyouxu jieyouxu added A-macros Area: macros (procedural macros, macro_rules! macros, etc.) A-parser Area: parser A-ecosystem-cfg-if Area: ecosystem `cfg_if!` X-impacts-stable-but-broken-formatting Expected formatting impact: affects stable formatting but where broken code was produced X-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) X-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) X-stability-guarantee-exempt Feature: impacts stable, but explicitly exempt from format stability guarantees. See README and removed X-impacts-stable-but-broken-formatting Expected formatting impact: affects stable formatting but where broken code was produced labels Oct 17, 2026

fn main() {
foo!(1 + 2);
foo!(1 + 2);

@jieyouxu jieyouxu Oct 17, 2026 •

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.

NB: this technically changes stable default formatting.

View changes since the review

Comment thread src/parse/macros/mod.rs
Comment on lines +199 to +204
// Indicative of an error count leak elsewhere.
debug_assert_eq!(
dcx.err_count(),
0,
"dcx error count should be 0 before calling `try_synthetic_parse`"
);

@jieyouxu jieyouxu Oct 17, 2026 •

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.

Note that this may trigger for other unrelated leaks. I rather leave this debug assert in even if this triggers some debug ICE, as I would like to know if something triggers this.

View changes since the review

@ytmimi ytmimi self-assigned this Oct 18, 2026

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

A-ecosystem-cfg-if Area: ecosystem `cfg_if!` A-macros Area: macros (procedural macros, macro_rules! macros, etc.) A-parser Area: parser S-waiting-on-review Status: awaiting review from the assignee but also interested parties. X-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) X-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) X-stability-guarantee-exempt Feature: impacts stable, but explicitly exempt from format stability guarantees. See README

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cfg_if! doesn't always properly reset error count

3 participants