diff options
| author | Veikka Tuominen <git@vexu.eu> | 2023-01-10 15:04:14 +0200 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2023-01-11 21:11:21 +0200 |
| commit | e2adf3b61a32eae2182bcb94981a0b3706040f9a (patch) | |
| tree | 913ea984891b64321b33009ab2f6fab63cc620c3 /lib | |
| parent | 8b1780d9396aa7bd919f2ec5e003f981bbce07d5 (diff) | |
| download | zig-e2adf3b61a32eae2182bcb94981a0b3706040f9a.tar.gz zig-e2adf3b61a32eae2182bcb94981a0b3706040f9a.zip | |
parser: add helpful note for missing const/var before container level decl
Closes #13888
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/zig/Ast.zig | 4 | ||||
| -rw-r--r-- | lib/std/zig/parse.zig | 7 | ||||
| -rw-r--r-- | lib/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 index 0573622d6f..f312093aa3 100644 --- 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 index 3bb27975db..fdb122b19d 100644 --- 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 index 1b8a240642..42eb1abdde 100644 --- 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 { |
