-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
improve type safety of std.zig.Ast #22397
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
Conversation
d842369
to
80c0304
Compare
Did you try having an fn offset(tok: TokenIndex, off: i32) TokenIndex {
const new_tok = @as(i64, @intFromEnum(tok)) + off;
return @enumFromInt(new_tok);
} Regardless: this PR seems like a fantastic change. My main concern just from the PR description is that final performance data point -- strictly speaking, this change shouldn't impact performance at all. Do you have any idea why the last performance number is consistently so bad? |
Kind of. I had
Here are some of the possibles reasons:
Removing the |
If I remember correctly, wasn't null for optional indexes represented as zero? The PR writeup shows |
|
All of these points seem like a perfectly valid use case for an |
It might be worth seeing whether marking functions However, I don't see a 10% regression in Debug performance for an already fairly fast subcommand as a huge deal, so this PR is fine as-is if you don't feel like looking into that. |
80c0304
to
cb91fda
Compare
Unfortunately, marking the functions as
|
2b23652
to
a8d3767
Compare
a8d3767
to
baffeb8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this.
The types aren't quite right though. To avoid 2 breakages rather than 1 let's get these right.
0330052
to
7657993
Compare
7657993
to
bab85dd
Compare
This function checks for various possibilities that are never produced by the parser. Given that lastToken is unsafe to call on an Ast with errors, I also removed code paths that would be reachable on an Ast with errors.
This commits adds the following distinct integer types to std.zig.Ast: - OptionalTokenIndex - TokenOffset - OptionalTokenOffset - Node.OptionalIndex - Node.Offset - Node.OptionalOffset The `Node.Index` type has also been converted to a distinct type while `TokenIndex` remains unchanged. `Ast.Node.Data` has also been changed to a (untagged) union to provide safety checks.
The existing comment are incomplete, outdated and sometimes incorrect.
…literal` The main_token already has the necessary information.
This makes the `.data` field the better choice over the `.main_token` for this tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, appreciate the follow-ups.
Adaptation zig 0.15 dev AST break change, see this [PR](ziglang/zig#22397)
Adaptation zig 0.15 dev AST break change, see this [PR](ziglang/zig#22397)
This PR introduces named/distinct integer types to
std.zig.Ast
, improving type safety and aligning it more closely with other compiler data structures like ZIR.At it's core, the changes can be summarized by the following:
Before
After
I initial changed
TokenIndex
to a named/distinct integer type as well but it made dealing with them unergonomic. This was especially noticable while portinglib/std/zig/render.zig
because it frequently relied on relative token indicies.The offset types (
Node.Offset
,TokenOffset
, etc.) have been added for ZIR because it stores relative ast indicies as opposed to absolute indicies.zig ast-check src/Sema.zig
(ReleaseFast)zig fmt --check src/Sema.zig
(ReleaseFast)zig ast-check src/Sema.zig
(Debug)zig fmt --check src/Sema.zig
(Debug)