Sitelet https://github.com/starkware-libs/cairo/pull/10188
Skip to content

bugfix(formatter): don't glue a comment to { in an empty block. - #10188

Merged
orizi merged 1 commit into
mainfrom
orizi/07-05-bugfix_formatter_don_t_glue_a_comment_to_in_an_empty_block
Jul 8, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/07-05-bugfix_formatter_don_t_glue_a_comment_to_in_an_empty_block

Conversation

@orizi

@orizi orizi commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When an empty StatementList block body has a comment attached to the closing brace, the formatter was omitting the space between { and the comment, producing {// comment instead of { // comment. A space is now inserted before the closing brace's trivia in this case, ensuring idempotent formatting.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

⚠️ Note:
To keep maintainer workload sustainable, we generally do not accept PRs that
are only minor wording, grammar, formatting, or style changes.
Such PRs may be closed without detailed review.


Why is this change needed?

When a block body is empty (zero-width StatementList) and the closing } carries a leading comment in its trivia, the formatter skipped the empty node without inserting any space. This caused the comment to be glued directly to the opening brace ({// comment), which is both visually incorrect and non-idempotent — re-running the formatter on its own output would produce a different result.


What was the behavior or documentation before?

An empty block with a comment on the closing brace was formatted as:

fn glued_in_input() {// no leading space in input
}

What is the behavior or documentation after?

The formatter now inserts a space before the comment, producing:

fn glued_in_input() { // no leading space in input
}

This applies to all empty block bodies, including nested constructs like loop { // comment in loop }.


Related issue or discussion (if any)


Additional context

A new test case (empty_block_comment.cairo) covers three scenarios: a top-level empty function body with a comment, input that already lacks the leading space, and a nested loop block with a comment.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review July 5, 2026 09:13
@cursor

cursor Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Localized formatter whitespace logic with new regression tests; no runtime, auth, or data-path impact.

Overview
Fixes a formatter bug where comments on the closing } of an empty block were glued to {, producing {// comment instead of { // comment.

When a zero-width StatementList is skipped, the formatter now inserts a space if the following } has non-whitespace leading trivia (e.g. a comment). Nested empty blocks (e.g. loop { // ... }) are covered.

Adds fixture empty_block_comment.cairo and extends the formatter test harness to assert idempotency (format → parse → format yields the same string).

Reviewed by Cursor Bugbot for commit bf6ae2a. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware 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.

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: 2 of 4 files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-formatter/test_data/expected_results/empty_block_comment.cairo line 4 at r1 (raw file):

}

fn glued_in_input() { // no leading space in input

We should also test it is not idempotent. Maybe even at the tester level

@eytan-starkware eytan-starkware 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.

@eytan-starkware reviewed 2 files.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).

An empty block whose sole content is a comment (`fn f() { // c }`) formatted the
comment glued to the opening brace (`{// c`): the empty statement list is skipped,
so its wrapping break points are dropped, and a leading comment gets no leading
space. It was also non-idempotent (a second pass inserted the space). When skipping
an empty statement list whose closing brace carries a comment, insert a space so it
reads `{ // c` and formatting is idempotent.
@orizi
orizi force-pushed the orizi/07-05-bugfix_formatter_don_t_glue_a_comment_to_in_an_empty_block branch from c8e5fb7 to bf6ae2a Compare July 7, 2026 04:51

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-formatter/test_data/expected_results/empty_block_comment.cairo line 4 at r1 (raw file):

Previously, eytan-starkware wrote…

We should also test it is not idempotent. Maybe even at the tester level

Done.

@eytan-starkware eytan-starkware 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.

:lgtm:

@eytan-starkware reviewed 4 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jul 8, 2026
Merged via the queue into main with commit c42b3aa Jul 8, 2026
55 checks passed
@orizi
orizi deleted the orizi/07-05-bugfix_formatter_don_t_glue_a_comment_to_in_an_empty_block branch July 8, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants