Skip to content

Commit e2adf3b

Browse files
committed
parser: add helpful note for missing const/var before container level decl
Closes #13888
1 parent 8b1780d commit e2adf3b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/std/zig/Ast.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5238,6 +5238,18 @@ test "zig fmt: missing const/var before local variable" {
52385238
});
52395239
}
52405240

5241+
test "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+
52415253
test "zig fmt: while continue expr" {
52425254
try testCanonical(
52435255
\\test {

0 commit comments

Comments
 (0)