Skip to content
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

Add logs for std.zig.Ast.parse() failure #105

Merged
merged 1 commit into from
Aug 9, 2023
Merged

Add logs for std.zig.Ast.parse() failure #105

merged 1 commit into from
Aug 9, 2023

Conversation

tiawl
Copy link
Contributor

@tiawl tiawl commented Aug 9, 2023

Hi,

As stated by this pull request title, it just provides some logs when std.zig.Ast.parse() failed to avoid some potential headaches.

Here a demo of the output:

cons std = @import ("std");
const fmt = std.fmt;
const allocPrint = fmt.allocPrint

fn main () void
{
  var i: u32 = 0;
  i++;
  const j: v32 = undefined,
  _ = j;
}

ERROR: zig.Ast.Error{ .tag = zig.Ast.Error.Tag.expected_comma_after_field, .is_note = false, .token_is_prev = false, .token = 1, .extra = zig.Ast.Error.Error__union_6445@7ffe2d1231c0 }
TOKEN: zig.tokenizer.Token{ .tag = zig.tokenizer.Token.Tag.identifier, .loc = zig.tokenizer.Token.Loc{ .start = 0, .end = 4 } }

cons std = @import ("std");
^^^^
ERROR: zig.Ast.Error{ .tag = zig.Ast.Error.Tag.expected_semi_after_decl, .is_note = false, .token_is_prev = true, .token = 20, .extra = zig.Ast.Error.Error__union_6445@7ffe2d1231c0 }
TOKEN: zig.tokenizer.Token{ .tag = zig.tokenizer.Token.Tag.period, .loc = zig.tokenizer.Token.Loc{ .start = 71, .end = 72 } }

const allocPrint = fmt.allocPrint
                      ^
ERROR: zig.Ast.Error{ .tag = zig.Ast.Error.Tag.expected_expr, .is_note = false, .token_is_prev = false, .token = 36, .extra = zig.Ast.Error.Error__union_6445@7ffe2d1231c0 }
TOKEN: zig.tokenizer.Token{ .tag = zig.tokenizer.Token.Tag.plus_plus, .loc = zig.tokenizer.Token.Loc{ .start = 123, .end = 125 } }

  i++;
   ^^
ERROR: zig.Ast.Error{ .tag = zig.Ast.Error.Tag.expected_statement, .is_note = false, .token_is_prev = false, .token = 43, .extra = zig.Ast.Error.Error__union_6445@7ffe2d1231c0 }
TOKEN: zig.tokenizer.Token{ .tag = zig.tokenizer.Token.Tag.identifier, .loc = zig.tokenizer.Token.Loc{ .start = 144, .end = 153 } }

  const j: v32 = undefined,
                 ^^^^^^^^^
thread 13107 panic: reached unreachable code
/home/user/.local/bin/lib/std/debug.zig:343:14: 0x2f0bf2 in assert (build)
    if (!ok) unreachable; // assertion failure
             ^
/home/user/Workspace/vulkan-zig/generator/vulkan/build_integration.zig:190:25: 0x389b83 in make (build)
        std.debug.assert(tree.errors.len == 0); // If this triggers, vulkan-zig produced invalid code.
                        ^

It is maybe not totally accurate but it gives a not so bad idea where is the potential mistakes in the generated content.

Hope it could help.

@Snektron
Copy link
Owner

Snektron commented Aug 9, 2023

Thanks, that should be useful during development. I wonder, what prompted you to add this?

@Snektron Snektron merged commit ed9401c into Snektron:master Aug 9, 2023
1 check passed
@tiawl
Copy link
Contributor Author

tiawl commented Aug 10, 2023

I am working on a very similar tool so I am currently reading, copying and pasting some huge parts of your code. I was very frustrated that this necessary assertion did not output more logging data about generated code (specially when it contains thousands of lines). Because I copied and I spent time to understand this part of your code, add useful code in mine should accordingly be useful for your code too. Now you know the all story.

Reading the code I submitted, some parts could be improved:

  • replace var tokens = try std.ArrayList (std.zig.Ast.Error).initCapacity (b.allocator, tree.errors.len); with var tokens: [] std.zig.Ast.Error = @constCast (tree.errors); will avoid unnecessary heap usage,
  • refactor this:
repeat = 1;
spaces = "";
while (repeat < current_token.loc.start - start)
{
  spaces = try std.fmt.allocPrint (b.allocator, "{s} ", .{ spaces, });
  repeat += 1;
}

  into a fn runtime_repeat (allocator: std.mem.Allocator, char: u8, repeat: usize) ![] const u8 will shorten the code and improve the readability.

Do I make another pull request to submit this changes ?

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

Successfully merging this pull request may close these issues.

2 participants