Skip to content

Allow empty identifier @"" where it can be represented #15001

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

Closed
wooster0 opened this issue Mar 18, 2023 · 3 comments
Closed

Allow empty identifier @"" where it can be represented #15001

wooster0 opened this issue Mar 18, 2023 · 3 comments

Comments

@wooster0
Copy link
Contributor

Preface

I'm calling for a partial reversal of a463dc7 which made it so that @"" is disallowed, everywhere.
My question is, how do I parse this valid JSON data using std.json.parse?

{
    "": 123
}

This is no longer possible:

const std = @import("std");
const json = std.json;
const payload =
    \\{
    \\    "": 123
    \\}
;
const Config = struct {
    @"": u8,
};
const config = x: {
    var stream = json.TokenStream.init(payload);
    const res = json.parse(Config, &stream, .{});
    // Assert no error can occur since we are
    // parsing this JSON at comptime!
    break :x res catch unreachable;
};
pub fn main() !void {
    std.log.info("\"\": {d}", .{config.@""});
}
x.zig:9:5: error: identifier cannot be empty
    @"": u8,
    ^~~
x.zig:19:40: error: identifier cannot be empty
    std.log.info("\"\": {d}", .{config.@""});
                                       ^~~

I'm facing a problem like this right now. Especially with Zig not having something like tags/attributes/annotations (#1099, #14656) for fields, the only thing I can think of is to come up with a special representation for empty fields without clashes. It can be kind of annoying especially because Zig should be able to handle @"" in a lot of places like these just fine.

Proposal

I think @"" should be allowed whenever it can be handled properly:

  • const @"" = 1 and var @"" = 1 should be perfectly fine
  • test "" should be fine. It may not look clean and may look weird if the name is interpolated into a string, but so does a test name with a single space or unprintable characters so I think an empty test name should be allowed too.
  • @"" as the field name in packed and normal structs
  • @"" for the name of a non-export function

But it should not be allowed in places such as:

  • extern const or export var
  • extern struct
  • export fn
    Basically it shouldn't be allowed in anything that interfaces with the outside world as it may not be possible to handle there. For example I don't think C has some way of defining a field with an empty name (correct me if I'm wrong).
    Things like @import("") should also stay disallowed, though.

Other Commentary

Another reason I have is that usually Zig avoids special cases like these where an empty string is not allowed so I find it odd that it is not allowed at all. Instead we should move the special cases over to mostly extern/export stuff where it simply cannot be represented properly.

Basically, I think it should be allowed everywhere where it is possible to properly handle an empty identifier.

Also, from #14534:

Even JSON has the ability to represent null bytes in map keys.

JSON also has the ability for empty keys. So allowing @"" would benefit ZON too and bring it on the same level as JSON in this regard. I do think ZON is currently lacking here.

@andrewrk
Copy link
Member

Duplicate of #14534.

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2023
@wooster0
Copy link
Contributor Author

that allows null bytes in symbols and length zero symbol names

Sorry, I totally missed that. I was scanning for "empty". I thought it was only about allowing \x00 in @"".

@andrewrk
Copy link
Member

No problem. I edited the title to make it more clear. Thanks for typing up this proposal - it seems like we are thinking along similar lines.

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

No branches or pull requests

2 participants