zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e2adf3b61a32eae2182bcb94981a0b3706040f9a (tree)
parent 8b1780d9396aa7bd919f2ec5e003f981bbce07d5
Author: Veikka Tuominen <git@vexu.eu>
Date:   Tue, 10 Jan 2023 15:04:14 +0200

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

Closes #13888

Diffstat:
Mlib/std/zig/Ast.zig | 4++++
Mlib/std/zig/parse.zig | 7+++++++
Mlib/std/zig/parser_test.zig | 12++++++++++++
3 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig @@ -362,6 +362,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void { .wrong_equal_var_decl => { return stream.writeAll("variable initialized with '==' instead of '='"); }, + .var_const_decl => { + return stream.writeAll("use 'var' or 'const' to declare variable"); + }, .expected_token => { const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)]; @@ -2743,6 +2746,7 @@ pub const Error = struct { c_style_container, expected_var_const, wrong_equal_var_decl, + var_const_decl, zig_style_container, previous_field, diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig @@ -471,6 +471,13 @@ const Parser = struct { // There is not allowed to be a decl after a field with no comma. // Report error but recover parser. try p.warn(.expected_comma_after_field); + if (p.token_tags[p.tok_i] == .semicolon and p.token_tags[identifier] == .identifier) { + try p.warnMsg(.{ + .tag = .var_const_decl, + .is_note = true, + .token = identifier, + }); + } p.findNextContainerMember(); continue; }, diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig @@ -5238,6 +5238,18 @@ test "zig fmt: missing const/var before local variable" { }); } +test "zig fmt: missing const/var before local variable" { + try testError( + \\std = foo, + \\std = foo; + \\*u32 = foo; + , &.{ + .expected_comma_after_field, + .var_const_decl, + .expected_comma_after_field, + }); +} + test "zig fmt: while continue expr" { try testCanonical( \\test {