Skip to content

zig fmt leaves @"_" alone in enums, but not in tagged unions #16714

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
mk12 opened this issue Aug 6, 2023 · 5 comments
Open

zig fmt leaves @"_" alone in enums, but not in tagged unions #16714

mk12 opened this issue Aug 6, 2023 · 5 comments
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. zig fmt
Milestone

Comments

@mk12
Copy link
Contributor

mk12 commented Aug 6, 2023

Zig Version

0.12.0-dev.5+fc6e57568

Steps to Reproduce and Observed Behavior

$ echo 'const A = enum { @"_", _ }; const B = union(enum) { @"_", _ };' > test.zig
$ zig fmt test.zig
test.zig
$ cat test.zig
const A = enum { @"_", _ };
const B = union(enum) { _, _ };

Expected Behavior

I expected zig fmt to treat @"_" in enum and union(enum) the same:

const A = enum { @"_", _ };
const B = union(enum) { @"_", _ };

The behavior was changed for enums in #15617 (cc @jacobly0).

(Although this seems like an obscure corner case, it actually matters for my project. I'm writing a Markdown parser, and enjoy naming my token variants like .@"* * *" instead of e.g. .thematic_break, since that's a needless extra mapping you have to keep in your head. So I want to use .@"_", not .emphasis.)

@mk12 mk12 added the bug Observed behavior contradicts documented or intended behavior label Aug 6, 2023
@mk12
Copy link
Contributor Author

mk12 commented Aug 6, 2023

@nektro Could you explain the 👎? This seemed like it was clearly an oversight, to me. I can't think of any rationale for the behavior being different in enums than in tagged unions.

@andrewrk
Copy link
Member

andrewrk commented Aug 6, 2023

I do wish that people would refrain from using the thumbs down emoji. This isn't a democracy and there is no voting. It just adds negative energy without contributing to the discussion.

@jacobly0
Copy link
Member

jacobly0 commented Aug 7, 2023

const E = enum {
    @"_",
    @"-",
};

const U = union(E) {
    _: u8,
    @"-": u16,
};

const U2 = union(enum) {
    _: u8,
    @"-": u16,
};

comptime {
    @compileLog(U{ ._ = 5 });
    @compileLog(U{ .@"-" = 13 });

    @compileLog(U2{ ._ = 5 });
    @compileLog(U2{ .@"-" = 13 });
}

Without a use case for having both _ and @"_" fields in a union, I don't see a reason to change zig fmt. The idea is that you can only have the non-canonical form if it has a semantic meaning, and currently putting both _ and @"_" in a union results in error: duplicate union field: '_' (aka, there is currently no such thing as a non-exhaustive tagged union).

@mk12
Copy link
Contributor Author

mk12 commented Aug 7, 2023

Oh interesting, I didn't realize that. I think non-exhaustive tagged unions are useful, so I hope #8162 gets accepted and then my request here should follow. If it gets rejected, then I would instead propose that using _ in a tagged union should be a compile error, just like this already is for variables:

$ echo 'pub fn main() void { var _: bool = true; }' > test.zig
$ zig build-exe test.zig
test.zig:1:26: error: '_' used as an identifier without @"_" syntax
pub fn main() void { var _: bool = true; }
                         ^

That way there's no confusion over whether the _ is special (as it very often is) or just a regular variant name.

@ianprime0509
Copy link
Contributor

As a side note, this might be a point in favor of #4859: using ... to indicate non-exhaustiveness would avoid colliding with anything that could possibly be interpreted as an identifier. Consistently banning _ from use as an identifier in any context (including enum and field names) would also achieve the same goal, I suppose.

@jacobly0 jacobly0 added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. frontend Tokenization, parsing, AstGen, Sema, and Liveness. zig fmt and removed bug Observed behavior contradicts documented or intended behavior labels Aug 12, 2023
@jacobly0 jacobly0 added this to the 0.15.0 milestone Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Tokenization, parsing, AstGen, Sema, and Liveness. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. zig fmt
Projects
None yet
Development

No branches or pull requests

4 participants