-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
improve type safety of std.zig.Ast #22397
base: master
Are you sure you want to change the base?
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
|
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.
AstGen doesn't seem to report enough information to figure out if a func decl has a calling convention ast node. A calling convention could be present without it by marking the function as inline for example.
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.Item` has also been changed to a (untagged) union to provide safety checks.
The existing comment are incomplete, outdated and sometimes incorrect.
cb91fda
to
2b23652
Compare
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)