Commit 4cc1008c2d
Changed files (2)
std
std/zig/parser.zig
@@ -4110,14 +4110,30 @@ pub const Parser = struct {
},
ast.Node.Id.ErrorSetDecl => {
const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base);
- try stream.print("error ");
+
+ const decls = err_set_decl.decls.toSliceConst();
+ if (decls.len == 0) {
+ try stream.write("error{}");
+ continue;
+ }
+
+ if (decls.len == 1) blk: {
+ const node = decls[0];
+ if (node.same_line_comment != null or node.doc_comments != null) break :blk;
+
+ try stream.write("error{");
+ try stack.append(RenderState { .Text = "}" });
+ try stack.append(RenderState { .Expression = node });
+ continue;
+ }
+
+ try stream.write("error{");
try stack.append(RenderState { .Text = "}"});
try stack.append(RenderState.PrintIndent);
try stack.append(RenderState { .Indent = indent });
try stack.append(RenderState { .Text = "\n"});
- const decls = err_set_decl.decls.toSliceConst();
var i = decls.len;
while (i != 0) {
i -= 1;
@@ -4142,7 +4158,6 @@ pub const Parser = struct {
});
}
try stack.append(RenderState { .Indent = indent + indent_delta});
- try stack.append(RenderState { .Text = "{"});
},
ast.Node.Id.MultilineStringLiteral => {
const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
std/zig/parser_test.zig
@@ -1,5 +1,35 @@
-test "zig fmt: union(enum(u32)) with assigned enum values" {
+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{};
+ \\
+ );
+}
+
+test "zig fmt: union(enum(u32)) with assigned enum values" {
+ try testCanonical(
\\const MultipleChoice = union(enum(u32)) {
\\ A = 20,
\\ B = 40,
@@ -23,7 +53,7 @@ test "zig fmt: labeled suspend" {
test "zig fmt: comments before error set decl" {
try testCanonical(
- \\const UnexpectedError = error {
+ \\const UnexpectedError = error{
\\ /// The Operating System returned an undocumented error code.
\\ Unexpected,
\\ // another
@@ -601,18 +631,6 @@ test "zig fmt: union declaration" {
);
}
-test "zig fmt: error set declaration" {
- try testCanonical(
- \\const E = error {
- \\ A,
- \\ B,
- \\
- \\ C,
- \\};
- \\
- );
-}
-
test "zig fmt: arrays" {
try testCanonical(
\\test "test array" {