authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-10 15:04:14+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-11 21:11:21+02:00
loge2adf3b61a32eae2182bcb94981a0b3706040f9a
tree913ea984891b64321b33009ab2f6fab63cc620c3
parent8b1780d9396aa7bd919f2ec5e003f981bbce07d5

parser: add helpful note for missing const/var before container level decl

Closes #13888

3 files changed, 23 insertions(+), 0 deletions(-)

lib/std/zig/Ast.zig+4
......@@ -362,6 +362,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
362362 .wrong_equal_var_decl => {
363363 return stream.writeAll("variable initialized with '==' instead of '='");
364364 },
365 .var_const_decl => {
366 return stream.writeAll("use 'var' or 'const' to declare variable");
367 },
365368
366369 .expected_token => {
367370 const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
......@@ -2743,6 +2746,7 @@ pub const Error = struct {
27432746 c_style_container,
27442747 expected_var_const,
27452748 wrong_equal_var_decl,
2749 var_const_decl,
27462750
27472751 zig_style_container,
27482752 previous_field,
lib/std/zig/parse.zig+7
......@@ -471,6 +471,13 @@ const Parser = struct {
471471 // There is not allowed to be a decl after a field with no comma.
472472 // Report error but recover parser.
473473 try p.warn(.expected_comma_after_field);
474 if (p.token_tags[p.tok_i] == .semicolon and p.token_tags[identifier] == .identifier) {
475 try p.warnMsg(.{
476 .tag = .var_const_decl,
477 .is_note = true,
478 .token = identifier,
479 });
480 }
474481 p.findNextContainerMember();
475482 continue;
476483 },
lib/std/zig/parser_test.zig+12
......@@ -5238,6 +5238,18 @@ test "zig fmt: missing const/var before local variable" {
52385238 });
52395239}
52405240
5241test "zig fmt: missing const/var before local variable" {
5242 try testError(
5243 \\std = foo,
5244 \\std = foo;
5245 \\*u32 = foo;
5246 , &.{
5247 .expected_comma_after_field,
5248 .var_const_decl,
5249 .expected_comma_after_field,
5250 });
5251}
5252
52415253test "zig fmt: while continue expr" {
52425254 try testCanonical(
52435255 \\test {