authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-28 21:01:12+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-29 14:55:43+03:00
log278c32976e93f4900c634913fb2bb06f6a7ecf87
treef4d58bc840b25ccb6281223d466a2d8a5effe9c1
parent5321afcf9c633c5dc0da5148981dfdbd61606f1a

parser: add helpful error for extra = in variable initializer

Closes #12768

3 files changed, 24 insertions(+), 1 deletions(-)

lib/std/zig/Ast.zig+4
...@@ -359,6 +359,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {...@@ -359,6 +359,9 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
359 .expected_var_const => {359 .expected_var_const => {
360 return stream.writeAll("expected 'var' or 'const' before variable declaration");360 return stream.writeAll("expected 'var' or 'const' before variable declaration");
361 },361 },
362 .wrong_equal_var_decl => {
363 return stream.writeAll("variable initialized with '==' instead of '='");
364 },
362365
363 .expected_token => {366 .expected_token => {
364 const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];367 const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
...@@ -2583,6 +2586,7 @@ pub const Error = struct {...@@ -2583,6 +2586,7 @@ pub const Error = struct {
2583 invalid_ampersand_ampersand,2586 invalid_ampersand_ampersand,
2584 c_style_container,2587 c_style_container,
2585 expected_var_const,2588 expected_var_const,
2589 wrong_equal_var_decl,
25862590
2587 zig_style_container,2591 zig_style_container,
2588 previous_field,2592 previous_field,
lib/std/zig/parse.zig+12-1
...@@ -812,7 +812,18 @@ const Parser = struct {...@@ -812,7 +812,18 @@ const Parser = struct {
812 const align_node = try p.parseByteAlign();812 const align_node = try p.parseByteAlign();
813 const addrspace_node = try p.parseAddrSpace();813 const addrspace_node = try p.parseAddrSpace();
814 const section_node = try p.parseLinkSection();814 const section_node = try p.parseLinkSection();
815 const init_node: Node.Index = if (p.eatToken(.equal) == null) 0 else try p.expectExpr();815 const init_node: Node.Index = switch (p.token_tags[p.tok_i]) {
816 .equal_equal => blk: {
817 try p.warn(.wrong_equal_var_decl);
818 p.tok_i += 1;
819 break :blk try p.expectExpr();
820 },
821 .equal => blk: {
822 p.tok_i += 1;
823 break :blk try p.expectExpr();
824 },
825 else => 0,
826 };
816 if (section_node == 0 and addrspace_node == 0) {827 if (section_node == 0 and addrspace_node == 0) {
817 if (align_node == 0) {828 if (align_node == 0) {
818 return p.addNode(.{829 return p.addNode(.{
lib/std/zig/parser_test.zig+8
...@@ -5145,6 +5145,14 @@ test "zig fmt: make single-line if no trailing comma" {...@@ -5145,6 +5145,14 @@ test "zig fmt: make single-line if no trailing comma" {
5145 );5145 );
5146}5146}
51475147
5148test "zig fmt: variable initialized with ==" {
5149 try testError(
5150 \\comptime {
5151 \\ var z: u32 == 12 + 1;
5152 \\}
5153 , &.{.wrong_equal_var_decl});
5154}
5155
5148test "zig fmt: missing const/var before local variable" {5156test "zig fmt: missing const/var before local variable" {
5149 try testError(5157 try testError(
5150 \\comptime {5158 \\comptime {