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

bugfix(semantic): writeln!(f) with no format string writes a newline. - #10182

Merged
orizi merged 1 commit into
mainfrom
orizi/07-01-bugfix_semantic_writeln_f_with_no_format_string_writes_a_newline
Jul 3, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/07-01-bugfix_semantic_writeln_f_with_no_format_string_writes_a_newline

Conversation

@orizi

@orizi orizi commented Jul 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

writeln!(f) with no format string argument now writes a newline character instead of emitting a compile error. println!() with no arguments also compiles via the same expansion path. write!(f) and print!() without a format string still produce an error.


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

Why is this change needed?

writeln!(f) is valid Rust — it writes a bare newline to the formatter without requiring a format string. Cairo's implementation unconditionally required a format string argument for both write!/writeln!, causing writeln!(f) to fail with "Macro expected format string argument." instead of emitting "\n".


What was the behavior or documentation before?

writeln!(f) and println!() both produced a compile-time plugin diagnostic: "Macro expected format string argument.".


What is the behavior or documentation after?

FormattingInfo::extract now receives the with_newline flag. When no format string is provided and with_newline is true, it returns a FormattingInfo with an empty format string instead of an error, causing the macro to emit just a newline. When with_newline is false (write!/print!), the original error is still returned.

A regression test (writeln_no_format_string) verifies that writeln!(f) appends "\n" to the formatter buffer and that println!() compiles successfully.


Related issue or discussion (if any)


Additional context

The format_string_arg field in the no-format-string path reuses the formatter argument node as a placeholder since no actual format string node exists. This is safe because the field is unused when the format string is empty.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jul 1, 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 1, 2026 12:35
@cursor

cursor Bot commented Jul 1, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Small, localized change to inline macro argument parsing in the semantic crate; behavior is gated on the existing newline flag with regression tests.

Overview
writeln! / println! with only a formatter (or no args) now emit a newline instead of failing with "Macro expected format string argument." This matches Rust-style writeln!(f) behavior.

FormattingInfo::extract in write.rs now takes with_newline. When the macro has a formatter but no format string, it returns an empty format string and no placeholder args if with_newline is true; write! / print! still require a format string and keep the old error.

Snapshot diagnostics for bad writeln! / println! cases drop the errors for writeln!(f) and println!(). test_writeln in fmt_test.cairo checks writeln!(f) → "\n" and writeln!(f, "hello") → "\nhello\n".

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

`writeln!(f)` (a formatter with no format string) errored with "Macro expected
format string argument", though in Rust it writes just a newline. Accept the
no-format-string case for `writeln!` and emit a newline; `write!(f)` still
requires a format string.

This also makes `println!()` valid — it expands to `writeln!(f, )` — matching its
documented "prints an empty line" example, while `print!()` → `write!(f, )` still
errors.
@orizi
orizi force-pushed the orizi/07-01-bugfix_semantic_writeln_f_with_no_format_string_writes_a_newline branch from 58e8554 to 030ea75 Compare July 2, 2026 10:09

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi
orizi added this pull request to the merge queue Jul 3, 2026
Merged via the queue into main with commit 845aa10 Jul 3, 2026
55 checks passed
@orizi
orizi deleted the orizi/07-01-bugfix_semantic_writeln_f_with_no_format_string_writes_a_newline branch July 5, 2026 12:14
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