From 7e25fb4a430cb8152d9c67899512152a8fcc2640 Mon Sep 17 00:00:00 2001 From: techatrix <19954306+Techatrix@users.noreply.github.com> Date: Fri, 28 Jul 2023 00:18:29 +0200 Subject: [PATCH] add bound check on for and while nodes --- lib/std/zig/Ast.zig | 18 +++++++++--------- lib/std/zig/parser_test.zig | 11 +++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig index a82982e262..7987d19778 100644 --- a/lib/std/zig/Ast.zig +++ b/lib/std/zig/Ast.zig @@ -752,11 +752,11 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex { // Look for a label and inline. const main_token = main_tokens[n]; var result = main_token; - if (token_tags[result - 1] == .keyword_inline) { + if (token_tags[result -| 1] == .keyword_inline) { result -= 1; } - if (token_tags[result - 1] == .colon) { - result -= 2; + if (token_tags[result -| 1] == .colon) { + result -|= 2; } return result - end_offset; }, @@ -2246,13 +2246,13 @@ fn fullWhileComponents(tree: Ast, info: full.While.Components) full.While { .else_token = undefined, .error_token = null, }; - var tok_i = info.while_token - 1; + var tok_i = info.while_token -| 1; if (token_tags[tok_i] == .keyword_inline) { result.inline_token = tok_i; - tok_i -= 1; + tok_i -|= 1; } if (token_tags[tok_i] == .colon and - token_tags[tok_i - 1] == .identifier) + token_tags[tok_i -| 1] == .identifier) { result.label_token = tok_i - 1; } @@ -2280,13 +2280,13 @@ fn fullForComponents(tree: Ast, info: full.For.Components) full.For { .payload_token = undefined, .else_token = undefined, }; - var tok_i = info.for_token - 1; + var tok_i = info.for_token -| 1; if (token_tags[tok_i] == .keyword_inline) { result.inline_token = tok_i; - tok_i -= 1; + tok_i -|= 1; } if (token_tags[tok_i] == .colon and - token_tags[tok_i - 1] == .identifier) + token_tags[tok_i -| 1] == .identifier) { result.label_token = tok_i - 1; } diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index ca3e99b164..00c50525a3 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -272,6 +272,17 @@ test "zig fmt: top-level enum missing 'const name ='" { , &[_]Error{.expected_token}); } +test "zig fmt: top-level for/while loop" { + try testCanonical( + \\for (foo) |_| foo + \\ + ); + try testCanonical( + \\while (foo) |_| foo + \\ + ); +} + test "zig fmt: top-level bare asterisk+identifier" { try testCanonical( \\*x