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 {
359359 .expected_var_const => {
360360 return stream.writeAll("expected 'var' or 'const' before variable declaration");
361361 },
362 .wrong_equal_var_decl => {
363 return stream.writeAll("variable initialized with '==' instead of '='");
364 },
362365
363366 .expected_token => {
364367 const found_tag = token_tags[parse_error.token + @boolToInt(parse_error.token_is_prev)];
......@@ -2583,6 +2586,7 @@ pub const Error = struct {
25832586 invalid_ampersand_ampersand,
25842587 c_style_container,
25852588 expected_var_const,
2589 wrong_equal_var_decl,
25862590
25872591 zig_style_container,
25882592 previous_field,
lib/std/zig/parse.zig+12-1
......@@ -812,7 +812,18 @@ const Parser = struct {
812812 const align_node = try p.parseByteAlign();
813813 const addrspace_node = try p.parseAddrSpace();
814814 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 };
816827 if (section_node == 0 and addrspace_node == 0) {
817828 if (align_node == 0) {
818829 return p.addNode(.{
lib/std/zig/parser_test.zig+8
......@@ -5145,6 +5145,14 @@ test "zig fmt: make single-line if no trailing comma" {
51455145 );
51465146}
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
51485156test "zig fmt: missing const/var before local variable" {
51495157 try testError(
51505158 \\comptime {