Skip to content

zig fmt: wrong output for tagged empty containers with document comments #23754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gooncreeper opened this issue May 3, 2025 · 0 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@gooncreeper
Copy link
Contributor

gooncreeper commented May 3, 2025

Zig Version

0.15.0-dev.451+a843be44a

Steps to Reproduce and Observed Behavior

Run zig fmt on the following code,

const A = union(enum(u32)) {
    //! Hello World!
};

It incorrectly formats it to

const A = union(enum(u32)) {
    //! Hello World!
//! Hello World!

This applies to packed struct(T), union(T), union(enum(T)), and enum(T)

This happens since Ast.lastToken assumes that when these have no members, the only following tokens are ){}. This also means the bug may also observable in other parts of the compiler, but it is easiest to observe with zig fmt.

Expected Behavior

No change.

@gooncreeper gooncreeper added the bug Observed behavior contradicts documented or intended behavior label May 3, 2025
@gooncreeper gooncreeper changed the title zig fmt: wrong output for empty containers with document comments zig fmt: wrong output for tagged empty containers with document comments May 4, 2025
gooncreeper added a commit to gooncreeper/zig that referenced this issue May 4, 2025
Adds a new fuzz test for zig fmt. This fuzz test checks that Ast.render
succeeds for parsed inputs. Additionally, for inputs it knows that
Ast.render cannot change the order of, it checks that they are not
rewritten.

This fuzz test has been very successful. Using ziglang#23416, it has found
three bugs (one was a TODO); two of them I have fixed and the other is
in ziglang#23754. I have run the test for 650,000,000 iterations (about 2
hours) and haven't found any more bugs.

Some functions in the fuzz test have instrumentation disabled because
their branches are not interesting to the fuzzer; doing this found a
~40% boost to the runs per second. Additionally, the fuzz test handles
tokenization itself since so it can determine if the input can be
rewritten.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant