authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-07-29 19:19:47+12:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-07-29 19:19:47+12:00
logc47b75312dc39a820e9f6533be0293066d9eab3f
treeac1ab843ddd24ca6d32498e42b7a3e071b90a4d5
parent74abc5ad2f4bf4f737b6c910b3048f7937345aad
parenta0ebfa64d9b8e98b31d37c80d118aab84f1a493e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2969 from evangrayk/zero-width-structs

fix zero-width structs in zig.fmt.format

1 files changed, 17 insertions(+), 1 deletions(-)

std/fmt.zig+17-1
......@@ -374,9 +374,10 @@ pub fn formatType(
374374 return output(context, "{ ... }");
375375 }
376376 comptime var field_i = 0;
377 try output(context, "{");
377378 inline while (field_i < @memberCount(T)) : (field_i += 1) {
378379 if (field_i == 0) {
379 try output(context, "{ .");
380 try output(context, " .");
380381 } else {
381382 try output(context, ", .");
382383 }
......@@ -1439,6 +1440,21 @@ test "struct.self-referential" {
14391440 try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", inst);
14401441}
14411442
1443test "struct.zero-size" {
1444 const A = struct {
1445 fn foo() void {}
1446 };
1447 const B = struct {
1448 a: A,
1449 c: i32,
1450 };
1451
1452 const a = A{};
1453 const b = B{ .a = a, .c = 0 };
1454
1455 try testFmt("B{ .a = A{ }, .c = 0 }", "{}", b);
1456}
1457
14421458test "bytes.hex" {
14431459 const some_bytes = "\xCA\xFE\xBA\xBE";
14441460 try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);