authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2023-07-28 00:18:29+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-28 00:20:53-07:00
log7e25fb4a430cb8152d9c67899512152a8fcc2640
tree71a674835c440b29bc6bc47a603bef8746b5ba9e
parent282cb5ee5d75b4de2f215479b0c5531750908608

add bound check on for and while nodes


2 files changed, 20 insertions(+), 9 deletions(-)

lib/std/zig/Ast.zig+9-9
...@@ -752,11 +752,11 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -752,11 +752,11 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
752 // Look for a label and inline.752 // Look for a label and inline.
753 const main_token = main_tokens[n];753 const main_token = main_tokens[n];
754 var result = main_token;754 var result = main_token;
755 if (token_tags[result - 1] == .keyword_inline) {755 if (token_tags[result -| 1] == .keyword_inline) {
756 result -= 1;756 result -= 1;
757 }757 }
758 if (token_tags[result - 1] == .colon) {758 if (token_tags[result -| 1] == .colon) {
759 result -= 2;759 result -|= 2;
760 }760 }
761 return result - end_offset;761 return result - end_offset;
762 },762 },
...@@ -2246,13 +2246,13 @@ fn fullWhileComponents(tree: Ast, info: full.While.Components) full.While {...@@ -2246,13 +2246,13 @@ fn fullWhileComponents(tree: Ast, info: full.While.Components) full.While {
2246 .else_token = undefined,2246 .else_token = undefined,
2247 .error_token = null,2247 .error_token = null,
2248 };2248 };
2249 var tok_i = info.while_token - 1;2249 var tok_i = info.while_token -| 1;
2250 if (token_tags[tok_i] == .keyword_inline) {2250 if (token_tags[tok_i] == .keyword_inline) {
2251 result.inline_token = tok_i;2251 result.inline_token = tok_i;
2252 tok_i -= 1;2252 tok_i -|= 1;
2253 }2253 }
2254 if (token_tags[tok_i] == .colon and2254 if (token_tags[tok_i] == .colon and
2255 token_tags[tok_i - 1] == .identifier)2255 token_tags[tok_i -| 1] == .identifier)
2256 {2256 {
2257 result.label_token = tok_i - 1;2257 result.label_token = tok_i - 1;
2258 }2258 }
...@@ -2280,13 +2280,13 @@ fn fullForComponents(tree: Ast, info: full.For.Components) full.For {...@@ -2280,13 +2280,13 @@ fn fullForComponents(tree: Ast, info: full.For.Components) full.For {
2280 .payload_token = undefined,2280 .payload_token = undefined,
2281 .else_token = undefined,2281 .else_token = undefined,
2282 };2282 };
2283 var tok_i = info.for_token - 1;2283 var tok_i = info.for_token -| 1;
2284 if (token_tags[tok_i] == .keyword_inline) {2284 if (token_tags[tok_i] == .keyword_inline) {
2285 result.inline_token = tok_i;2285 result.inline_token = tok_i;
2286 tok_i -= 1;2286 tok_i -|= 1;
2287 }2287 }
2288 if (token_tags[tok_i] == .colon and2288 if (token_tags[tok_i] == .colon and
2289 token_tags[tok_i - 1] == .identifier)2289 token_tags[tok_i -| 1] == .identifier)
2290 {2290 {
2291 result.label_token = tok_i - 1;2291 result.label_token = tok_i - 1;
2292 }2292 }
lib/std/zig/parser_test.zig+11
...@@ -272,6 +272,17 @@ test "zig fmt: top-level enum missing 'const name ='" {...@@ -272,6 +272,17 @@ test "zig fmt: top-level enum missing 'const name ='" {
272 , &[_]Error{.expected_token});272 , &[_]Error{.expected_token});
273}273}
274274
275test "zig fmt: top-level for/while loop" {
276 try testCanonical(
277 \\for (foo) |_| foo
278 \\
279 );
280 try testCanonical(
281 \\while (foo) |_| foo
282 \\
283 );
284}
285
275test "zig fmt: top-level bare asterisk+identifier" {286test "zig fmt: top-level bare asterisk+identifier" {
276 try testCanonical(287 try testCanonical(
277 \\*x288 \\*x