Commit e2adf3b61a

Veikka Tuominen <git@vexu.eu>
2023-01-10 14:04:14
parser: add helpful note for missing const/var before container level decl
Closes #13888
1 parent 8b1780d
Changed files (3)
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,
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;
                 },
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 {