diff --git a/parser.c b/parser.c index 102ecdaa2a..c118700a4b 100644 --- a/parser.c +++ b/parser.c @@ -2086,8 +2086,8 @@ static AstNodeIndex expectStatement(Parser* p, bool allow_defer_var) { .tag = AST_NODE_DEFER, .main_token = nextToken(p), .data = { - .lhs = 0, - .rhs = expectBlockExprStatement(p), + .lhs = expectBlockExprStatement(p), + .rhs = 0, }, }); case TOKEN_KEYWORD_ERRDEFER: { diff --git a/parser_test.zig b/parser_test.zig index c3091f0d36..1aa2018a9c 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -2145,6 +2145,100 @@ test "zig fmt: anytype type" { ); } +test "zig fmt: line and doc comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\// test 1 + \\/// test 2 + \\const e = f; + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\/// test 1 + \\// test 2 + \\const e = f; + ); +} + +test "zig fmt: line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test + \\const e = f; + \\ + ); +} + +test "zig fmt: doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test + \\const e = f; + \\ + ); +} + +test "zig fmt: line and doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test1 + \\/// test2 + \\const e = f; + \\ + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test1 + \\// test2 + \\const e = f; + \\ + ); +} + +test "zig fmt: block in slice expression" { + try testCanonical( + \\const a = b[{ + \\ _ = x; + \\}..]; + \\const c = d[0..{ + \\ _ = x; + \\ _ = y; + \\}]; + \\const e = f[0..1 :{ + \\ _ = x; + \\ _ = y; + \\ _ = z; + \\}]; + \\ + ); +} + +test "zig fmt: defer" { + try testCanonical( + \\test "defer" { + \\ defer foo(); + \\ defer { + \\ bar(); + \\ } + \\} + \\ + ); +} + test "zig fmt: container initializers" { try testCanonical( \\const a0 = []u8{};