diff --git a/parser.c b/parser.c index 236486971b..292823909c 100644 --- a/parser.c +++ b/parser.c @@ -567,7 +567,7 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) { } case TOKEN_L_BRACE: { const AstTokenIndex error_token = nextToken(p); - nextToken(p); // consume { + const AstTokenIndex lbrace = nextToken(p); while (p->token_tags[p->tok_i] != TOKEN_R_BRACE) p->tok_i++; const AstTokenIndex rbrace = nextToken(p); @@ -575,7 +575,7 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) { (AstNodeItem) { .tag = AST_NODE_ERROR_SET_DECL, .main_token = error_token, - .data = { .lhs = 0, .rhs = rbrace }, + .data = { .lhs = lbrace, .rhs = rbrace }, }); } default: @@ -2328,6 +2328,15 @@ static AstNodeIndex expectStatement(Parser* p, bool allow_defer_var) { }, }); case TOKEN_KEYWORD_SUSPEND: + return addNode(&p->nodes, + (AstNodeItem) { + .tag = AST_NODE_SUSPEND, + .main_token = nextToken(p), + .data = { + .lhs = expectBlockExprStatement(p), + .rhs = 0, + }, + }); case TOKEN_KEYWORD_ENUM: case TOKEN_KEYWORD_STRUCT: case TOKEN_KEYWORD_UNION:; diff --git a/parser_test.zig b/parser_test.zig index 6a81d7447f..29af78a31f 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -3140,6 +3140,114 @@ test "zig fmt: first line comment in struct initializer" { ); } +test "zig fmt: error set declaration" { + try testCanonical( + \\const E = error{ + \\ A, + \\ B, + \\ + \\ C, + \\}; + \\ + \\const Error = error{ + \\ /// no more memory + \\ OutOfMemory, + \\}; + \\ + \\const Error = error{ + \\ /// no more memory + \\ OutOfMemory, + \\ + \\ /// another + \\ Another, + \\ + \\ // end + \\}; + \\ + \\const Error = error{OutOfMemory}; + \\const Error = error{}; + \\ + \\const Error = error{ OutOfMemory, OutOfTime }; + \\ + ); +} + +test "zig fmt: union(enum(u32)) with assigned enum values" { + try testCanonical( + \\const MultipleChoice = union(enum(u32)) { + \\ A = 20, + \\ B = 40, + \\ C = 60, + \\ D = 1000, + \\}; + \\ + ); +} + +test "zig fmt: resume from suspend block" { + try testCanonical( + \\fn foo() void { + \\ suspend { + \\ resume @frame(); + \\ } + \\} + \\ + ); +} + +test "zig fmt: comments before error set decl" { + try testCanonical( + \\const UnexpectedError = error{ + \\ /// The Operating System returned an undocumented error code. + \\ Unexpected, + \\ // another + \\ Another, + \\ + \\ // in between + \\ + \\ // at end + \\}; + \\ + ); +} + +test "zig fmt: comments before switch prong" { + try testCanonical( + \\test "" { + \\ switch (err) { + \\ error.PathAlreadyExists => continue, + \\ + \\ // comment 1 + \\ + \\ // comment 2 + \\ else => return err, + \\ // at end + \\ } + \\} + \\ + ); +} + +test "zig fmt: array literal with 1 item on 1 line" { + try testCanonical( + \\var s = []const u64{0} ** 25; + \\ + ); +} + +test "zig fmt: comments in statements" { + try testCanonical( + \\comptime { + \\ // a + \\ + \\ const x = 42; // b + \\ + \\ // c + \\} + \\ + ); +} + test "zig fmt: doc comments before struct field" { try testCanonical( \\pub const Allocator = struct {