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

Closes #13888
This commit is contained in:
Veikka Tuominen 2023-01-10 15:04:14 +02:00
parent 8b1780d939
commit e2adf3b61a
3 changed files with 23 additions and 0 deletions

View File

@ -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,

View File

@ -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;
},

View File

@ -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 {