Skip to content

Commit 26db54d

Browse files
skewb1kVexu
authored andcommitted
zig fmt: fix extra whitespace in StructInit with multiline strings
68d2f68 introduced special handling for StructInit fields containing multiline strings to prevent inserting whitespace after =. However, this logic didn't handle cases without a trailing comma, which resulted in unwanted trailing whitespace.
1 parent a6d444c commit 26db54d

File tree

4 files changed

+102
-91
lines changed

4 files changed

+102
-91
lines changed

lib/std/zig/Ast/Render.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,8 @@ fn renderStructInit(
20562056
const init_token = tree.firstToken(field_init);
20572057
try renderToken(r, init_token - 3, .none); // .
20582058
try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
2059-
try renderToken(r, init_token - 1, .space); // =
2059+
const space_after_equal: Space = if (tree.nodeTag(field_init) == .multiline_string_literal) .none else .space;
2060+
try renderToken(r, init_token - 1, space_after_equal); // =
20602061
try renderExpressionFixup(r, field_init, .comma_space);
20612062
}
20622063
}

lib/std/zig/parser_test.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,16 @@ test "zig fmt: no space before newline before multiline string" {
57195719
\\ ,
57205720
\\ };
57215721
\\ _ = s2;
5722+
\\ const s3 = .{ .text =
5723+
\\ \\hello
5724+
\\ \\world
5725+
\\ , .comment = "test" };
5726+
\\ _ = s3;
5727+
\\ const s4 = .{ .comment = "test", .text =
5728+
\\ \\hello
5729+
\\ \\world
5730+
\\ };
5731+
\\ _ = s4;
57225732
\\}
57235733
\\
57245734
);

test/link/elf.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn testComdatElimination(b: *Build, opts: Options) *Step {
480480
fn testCommentString(b: *Build, opts: Options) *Step {
481481
const test_step = addTestStep(b, "comment-string", opts);
482482

483-
const exe = addExecutable(b, opts, .{ .name = "main", .zig_source_bytes =
483+
const exe = addExecutable(b, opts, .{ .name = "main", .zig_source_bytes =
484484
\\pub fn main() void {}
485485
});
486486

@@ -495,7 +495,7 @@ fn testCommentString(b: *Build, opts: Options) *Step {
495495
fn testCommentStringStaticLib(b: *Build, opts: Options) *Step {
496496
const test_step = addTestStep(b, "comment-string-static-lib", opts);
497497

498-
const lib = addStaticLibrary(b, opts, .{ .name = "lib", .zig_source_bytes =
498+
const lib = addStaticLibrary(b, opts, .{ .name = "lib", .zig_source_bytes =
499499
\\export fn foo() void {}
500500
});
501501

@@ -866,7 +866,7 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
866866
fn testEmitRelocatable(b: *Build, opts: Options) *Step {
867867
const test_step = addTestStep(b, "emit-relocatable", opts);
868868

869-
const a_o = addObject(b, opts, .{ .name = "a", .zig_source_bytes =
869+
const a_o = addObject(b, opts, .{ .name = "a", .zig_source_bytes =
870870
\\const std = @import("std");
871871
\\extern var bar: i32;
872872
\\export fn foo() i32 {
@@ -878,7 +878,7 @@ fn testEmitRelocatable(b: *Build, opts: Options) *Step {
878878
});
879879
a_o.root_module.link_libc = true;
880880

881-
const b_o = addObject(b, opts, .{ .name = "b", .c_source_bytes =
881+
const b_o = addObject(b, opts, .{ .name = "b", .c_source_bytes =
882882
\\#include <stdio.h>
883883
\\int bar = 42;
884884
\\void printBar() {
@@ -891,7 +891,7 @@ fn testEmitRelocatable(b: *Build, opts: Options) *Step {
891891
c_o.root_module.addObject(a_o);
892892
c_o.root_module.addObject(b_o);
893893

894-
const exe = addExecutable(b, opts, .{ .name = "test", .zig_source_bytes =
894+
const exe = addExecutable(b, opts, .{ .name = "test", .zig_source_bytes =
895895
\\const std = @import("std");
896896
\\extern fn printFoo() void;
897897
\\extern fn printBar() void;
@@ -1925,7 +1925,7 @@ fn testInitArrayOrder(b: *Build, opts: Options) *Step {
19251925
});
19261926
g_o.root_module.link_libc = true;
19271927

1928-
const h_o = addObject(b, opts, .{ .name = "h", .c_source_bytes =
1928+
const h_o = addObject(b, opts, .{ .name = "h", .c_source_bytes =
19291929
\\#include <stdio.h>
19301930
\\__attribute__((destructor)) void fini2() { printf("8"); }
19311931
});
@@ -2447,7 +2447,7 @@ fn testLinkingZig(b: *Build, opts: Options) *Step {
24472447
fn testLinksection(b: *Build, opts: Options) *Step {
24482448
const test_step = addTestStep(b, "linksection", opts);
24492449

2450-
const obj = addObject(b, opts, .{ .name = "main", .zig_source_bytes =
2450+
const obj = addObject(b, opts, .{ .name = "main", .zig_source_bytes =
24512451
\\export var test_global: u32 linksection(".TestGlobal") = undefined;
24522452
\\export fn testFn() linksection(".TestFn") callconv(.c) void {
24532453
\\ TestGenericFn("A").f();
@@ -2540,7 +2540,7 @@ fn testMergeStrings(b: *Build, opts: Options) *Step {
25402540
fn testMergeStrings2(b: *Build, opts: Options) *Step {
25412541
const test_step = addTestStep(b, "merge-strings2", opts);
25422542

2543-
const obj1 = addObject(b, opts, .{ .name = "a", .zig_source_bytes =
2543+
const obj1 = addObject(b, opts, .{ .name = "a", .zig_source_bytes =
25442544
\\const std = @import("std");
25452545
\\export fn foo() void {
25462546
\\ var arr: [5:0]u16 = [_:0]u16{ 1, 2, 3, 4, 5 };
@@ -2549,7 +2549,7 @@ fn testMergeStrings2(b: *Build, opts: Options) *Step {
25492549
\\}
25502550
});
25512551

2552-
const obj2 = addObject(b, opts, .{ .name = "b", .zig_source_bytes =
2552+
const obj2 = addObject(b, opts, .{ .name = "b", .zig_source_bytes =
25532553
\\const std = @import("std");
25542554
\\extern fn foo() void;
25552555
\\pub fn main() void {
@@ -2797,7 +2797,7 @@ fn testRelocatableEhFrame(b: *Build, opts: Options) *Step {
27972797
,
27982798
});
27992799
obj2.root_module.link_libcpp = true;
2800-
const obj3 = addObject(b, opts, .{ .name = "obj3", .cpp_source_bytes =
2800+
const obj3 = addObject(b, opts, .{ .name = "obj3", .cpp_source_bytes =
28012801
\\#include <iostream>
28022802
\\#include <stdexcept>
28032803
\\extern int try_again();
@@ -2909,7 +2909,7 @@ fn testRelocatableEhFrameComdatHeavy(b: *Build, opts: Options) *Step {
29092909
fn testRelocatableMergeStrings(b: *Build, opts: Options) *Step {
29102910
const test_step = addTestStep(b, "relocatable-merge-strings", opts);
29112911

2912-
const obj1 = addObject(b, opts, .{ .name = "a", .asm_source_bytes =
2912+
const obj1 = addObject(b, opts, .{ .name = "a", .asm_source_bytes =
29132913
\\.section .rodata.str1.1,"aMS",@progbits,1
29142914
\\val1:
29152915
\\.ascii "Hello \0"
@@ -3075,7 +3075,7 @@ fn testStrip(b: *Build, opts: Options) *Step {
30753075
fn testThunks(b: *Build, opts: Options) *Step {
30763076
const test_step = addTestStep(b, "thunks", opts);
30773077

3078-
const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes =
3078+
const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes =
30793079
\\void foo();
30803080
\\__attribute__((section(".bar"))) void bar() {
30813081
\\ return foo();

0 commit comments

Comments
 (0)