authorgravatar for yujiri@disroot.orgyujiri8 <yujiri@disroot.org> 2022-12-16 22:24:58+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-17 00:24:58+02:00
log68d2f68ed823984a59724e256e78c5654d55b088
treebc407334939073aaa1a7b095314fcbe458c76356
parent8da9cc85af8d8f587b12e2d1ee275f50bf83b224
signature Signed by PGP key 4AEE18F83AFDEB23

zig fmt: fix extra whitespace with multiline strings

Fixes #13937

4 files changed, 107 insertions(+), 72 deletions(-)

lib/std/zig/parser_test.zig+29
......@@ -5500,6 +5500,35 @@ test "zig fmt: canonicalize symbols (keywords)" {
55005500 );
55015501}
55025502
5503test "zig fmt: no space before newline before multiline string" {
5504 try testCanonical(
5505 \\const S = struct {
5506 \\ text: []const u8,
5507 \\ comment: []const u8,
5508 \\};
5509 \\
5510 \\test {
5511 \\ const s1 = .{
5512 \\ .text =
5513 \\ \\hello
5514 \\ \\world
5515 \\ ,
5516 \\ .comment = "test",
5517 \\ };
5518 \\ _ = s1;
5519 \\ const s2 = .{
5520 \\ .comment = "test",
5521 \\ .text =
5522 \\ \\hello
5523 \\ \\world
5524 \\ ,
5525 \\ };
5526 \\ _ = s2;
5527 \\}
5528 \\
5529 );
5530}
5531
55035532// Normalize \xNN and \u{NN} escapes and unicode inside @"" escapes.
55045533test "zig fmt: canonicalize symbols (character escapes)" {
55055534 try testTransform(
lib/std/zig/render.zig+8-2
......@@ -1675,7 +1675,12 @@ fn renderStructInit(
16751675
16761676 try renderToken(ais, tree, struct_init.ast.lbrace + 1, .none); // .
16771677 try renderIdentifier(ais, tree, struct_init.ast.lbrace + 2, .space, .eagerly_unquote); // name
1678 try renderToken(ais, tree, struct_init.ast.lbrace + 3, .space); // =
1678 // Don't output a space after the = if expression is a multiline string,
1679 // since then it will start on the next line.
1680 const nodes = tree.nodes.items(.tag);
1681 const expr = nodes[struct_init.ast.fields[0]];
1682 var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;
1683 try renderToken(ais, tree, struct_init.ast.lbrace + 3, space_after_equal); // =
16791684 try renderExpression(gpa, ais, tree, struct_init.ast.fields[0], .comma);
16801685
16811686 for (struct_init.ast.fields[1..]) |field_init| {
......@@ -1683,7 +1688,8 @@ fn renderStructInit(
16831688 try renderExtraNewlineToken(ais, tree, init_token - 3);
16841689 try renderToken(ais, tree, init_token - 3, .none); // .
16851690 try renderIdentifier(ais, tree, init_token - 2, .space, .eagerly_unquote); // name
1686 try renderToken(ais, tree, init_token - 1, .space); // =
1691 space_after_equal = if (nodes[field_init] == .multiline_string_literal) .none else .space;
1692 try renderToken(ais, tree, init_token - 1, space_after_equal); // =
16871693 try renderExpression(gpa, ais, tree, field_init, .comma);
16881694 }
16891695
src/codegen/llvm.zig+3-3
......@@ -9948,7 +9948,7 @@ pub const FuncGen = struct {
99489948 constraints: [:0]const u8,
99499949 } = switch (target.cpu.arch) {
99509950 .x86 => .{
9951 .template =
9951 .template =
99529952 \\roll $$3, %edi ; roll $$13, %edi
99539953 \\roll $$61, %edi ; roll $$51, %edi
99549954 \\xchgl %ebx,%ebx
......@@ -9956,7 +9956,7 @@ pub const FuncGen = struct {
99569956 .constraints = "={edx},{eax},0,~{cc},~{memory}",
99579957 },
99589958 .x86_64 => .{
9959 .template =
9959 .template =
99609960 \\rolq $$3, %rdi ; rolq $$13, %rdi
99619961 \\rolq $$61, %rdi ; rolq $$51, %rdi
99629962 \\xchgq %rbx,%rbx
......@@ -9964,7 +9964,7 @@ pub const FuncGen = struct {
99649964 .constraints = "={rdx},{rax},0,~{cc},~{memory}",
99659965 },
99669966 .aarch64, .aarch64_32, .aarch64_be => .{
9967 .template =
9967 .template =
99689968 \\ror x12, x12, #3 ; ror x12, x12, #13
99699969 \\ror x12, x12, #51 ; ror x12, x12, #61
99709970 \\orr x10, x10, x10
test/stack_traces.zig+67-67
......@@ -5,13 +5,13 @@ const tests = @import("tests.zig");
55pub fn addCases(cases: *tests.StackTracesContext) void {
66 cases.addCase(.{
77 .name = "return",
8 .source =
8 .source =
99 \\pub fn main() !void {
1010 \\ return error.TheSkyIsFalling;
1111 \\}
1212 ,
1313 .Debug = .{
14 .expect =
14 .expect =
1515 \\error: TheSkyIsFalling
1616 \\source.zig:2:5: [address] in main (test)
1717 \\ return error.TheSkyIsFalling;
......@@ -24,7 +24,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
2424 .windows, // TODO
2525 .linux, // defeated by aggressive inlining
2626 },
27 .expect =
27 .expect =
2828 \\error: TheSkyIsFalling
2929 \\source.zig:2:5: [address] in [function]
3030 \\ return error.TheSkyIsFalling;
......@@ -33,13 +33,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
3333 ,
3434 },
3535 .ReleaseFast = .{
36 .expect =
36 .expect =
3737 \\error: TheSkyIsFalling
3838 \\
3939 ,
4040 },
4141 .ReleaseSmall = .{
42 .expect =
42 .expect =
4343 \\error: TheSkyIsFalling
4444 \\
4545 ,
......@@ -48,7 +48,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
4848
4949 cases.addCase(.{
5050 .name = "try return",
51 .source =
51 .source =
5252 \\fn foo() !void {
5353 \\ return error.TheSkyIsFalling;
5454 \\}
......@@ -58,7 +58,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
5858 \\}
5959 ,
6060 .Debug = .{
61 .expect =
61 .expect =
6262 \\error: TheSkyIsFalling
6363 \\source.zig:2:5: [address] in foo (test)
6464 \\ return error.TheSkyIsFalling;
......@@ -73,7 +73,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
7373 .exclude_os = .{
7474 .windows, // TODO
7575 },
76 .expect =
76 .expect =
7777 \\error: TheSkyIsFalling
7878 \\source.zig:2:5: [address] in [function]
7979 \\ return error.TheSkyIsFalling;
......@@ -85,13 +85,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
8585 ,
8686 },
8787 .ReleaseFast = .{
88 .expect =
88 .expect =
8989 \\error: TheSkyIsFalling
9090 \\
9191 ,
9292 },
9393 .ReleaseSmall = .{
94 .expect =
94 .expect =
9595 \\error: TheSkyIsFalling
9696 \\
9797 ,
......@@ -99,7 +99,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
9999 });
100100 cases.addCase(.{
101101 .name = "non-error return pops error trace",
102 .source =
102 .source =
103103 \\fn bar() !void {
104104 \\ return error.UhOh;
105105 \\}
......@@ -116,7 +116,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
116116 \\}
117117 ,
118118 .Debug = .{
119 .expect =
119 .expect =
120120 \\error: UnrelatedError
121121 \\source.zig:13:5: [address] in main (test)
122122 \\ return error.UnrelatedError;
......@@ -129,7 +129,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
129129 .windows, // TODO
130130 .linux, // defeated by aggressive inlining
131131 },
132 .expect =
132 .expect =
133133 \\error: UnrelatedError
134134 \\source.zig:13:5: [address] in [function]
135135 \\ return error.UnrelatedError;
......@@ -138,13 +138,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
138138 ,
139139 },
140140 .ReleaseFast = .{
141 .expect =
141 .expect =
142142 \\error: UnrelatedError
143143 \\
144144 ,
145145 },
146146 .ReleaseSmall = .{
147 .expect =
147 .expect =
148148 \\error: UnrelatedError
149149 \\
150150 ,
......@@ -153,7 +153,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
153153
154154 cases.addCase(.{
155155 .name = "continue in while loop",
156 .source =
156 .source =
157157 \\fn foo() !void {
158158 \\ return error.UhOh;
159159 \\}
......@@ -167,7 +167,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
167167 \\}
168168 ,
169169 .Debug = .{
170 .expect =
170 .expect =
171171 \\error: UnrelatedError
172172 \\source.zig:10:5: [address] in main (test)
173173 \\ return error.UnrelatedError;
......@@ -180,7 +180,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
180180 .windows, // TODO
181181 .linux, // defeated by aggressive inlining
182182 },
183 .expect =
183 .expect =
184184 \\error: UnrelatedError
185185 \\source.zig:10:5: [address] in [function]
186186 \\ return error.UnrelatedError;
......@@ -189,13 +189,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
189189 ,
190190 },
191191 .ReleaseFast = .{
192 .expect =
192 .expect =
193193 \\error: UnrelatedError
194194 \\
195195 ,
196196 },
197197 .ReleaseSmall = .{
198 .expect =
198 .expect =
199199 \\error: UnrelatedError
200200 \\
201201 ,
......@@ -204,7 +204,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
204204
205205 cases.addCase(.{
206206 .name = "try return + handled catch/if-else",
207 .source =
207 .source =
208208 \\fn foo() !void {
209209 \\ return error.TheSkyIsFalling;
210210 \\}
......@@ -218,7 +218,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
218218 \\}
219219 ,
220220 .Debug = .{
221 .expect =
221 .expect =
222222 \\error: TheSkyIsFalling
223223 \\source.zig:2:5: [address] in foo (test)
224224 \\ return error.TheSkyIsFalling;
......@@ -234,7 +234,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
234234 .windows, // TODO
235235 .linux, // defeated by aggressive inlining
236236 },
237 .expect =
237 .expect =
238238 \\error: TheSkyIsFalling
239239 \\source.zig:2:5: [address] in [function]
240240 \\ return error.TheSkyIsFalling;
......@@ -246,13 +246,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
246246 ,
247247 },
248248 .ReleaseFast = .{
249 .expect =
249 .expect =
250250 \\error: TheSkyIsFalling
251251 \\
252252 ,
253253 },
254254 .ReleaseSmall = .{
255 .expect =
255 .expect =
256256 \\error: TheSkyIsFalling
257257 \\
258258 ,
......@@ -261,7 +261,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
261261
262262 cases.addCase(.{
263263 .name = "break from inline loop pops error return trace",
264 .source =
264 .source =
265265 \\fn foo() !void { return error.FooBar; }
266266 \\
267267 \\pub fn main() !void {
......@@ -277,7 +277,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
277277 \\}
278278 ,
279279 .Debug = .{
280 .expect =
280 .expect =
281281 \\error: BadTime
282282 \\source.zig:12:5: [address] in main (test)
283283 \\ return error.BadTime;
......@@ -290,7 +290,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
290290 .windows, // TODO
291291 .linux, // defeated by aggressive inlining
292292 },
293 .expect =
293 .expect =
294294 \\error: BadTime
295295 \\source.zig:12:5: [address] in [function]
296296 \\ return error.BadTime;
......@@ -299,13 +299,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
299299 ,
300300 },
301301 .ReleaseFast = .{
302 .expect =
302 .expect =
303303 \\error: BadTime
304304 \\
305305 ,
306306 },
307307 .ReleaseSmall = .{
308 .expect =
308 .expect =
309309 \\error: BadTime
310310 \\
311311 ,
......@@ -314,7 +314,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
314314
315315 cases.addCase(.{
316316 .name = "catch and re-throw error",
317 .source =
317 .source =
318318 \\fn foo() !void {
319319 \\ return error.TheSkyIsFalling;
320320 \\}
......@@ -324,7 +324,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
324324 \\}
325325 ,
326326 .Debug = .{
327 .expect =
327 .expect =
328328 \\error: AndMyCarIsOutOfGas
329329 \\source.zig:2:5: [address] in foo (test)
330330 \\ return error.TheSkyIsFalling;
......@@ -340,7 +340,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
340340 .windows, // TODO
341341 .linux, // defeated by aggressive inlining
342342 },
343 .expect =
343 .expect =
344344 \\error: AndMyCarIsOutOfGas
345345 \\source.zig:2:5: [address] in [function]
346346 \\ return error.TheSkyIsFalling;
......@@ -352,13 +352,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
352352 ,
353353 },
354354 .ReleaseFast = .{
355 .expect =
355 .expect =
356356 \\error: AndMyCarIsOutOfGas
357357 \\
358358 ,
359359 },
360360 .ReleaseSmall = .{
361 .expect =
361 .expect =
362362 \\error: AndMyCarIsOutOfGas
363363 \\
364364 ,
......@@ -367,7 +367,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
367367
368368 cases.addCase(.{
369369 .name = "errors stored in var do not contribute to error trace",
370 .source =
370 .source =
371371 \\fn foo() !void {
372372 \\ return error.TheSkyIsFalling;
373373 \\}
......@@ -382,7 +382,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
382382 \\}
383383 ,
384384 .Debug = .{
385 .expect =
385 .expect =
386386 \\error: SomethingUnrelatedWentWrong
387387 \\source.zig:11:5: [address] in main (test)
388388 \\ return error.SomethingUnrelatedWentWrong;
......@@ -395,7 +395,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
395395 .windows, // TODO
396396 .linux, // defeated by aggressive inlining
397397 },
398 .expect =
398 .expect =
399399 \\error: SomethingUnrelatedWentWrong
400400 \\source.zig:11:5: [address] in [function]
401401 \\ return error.SomethingUnrelatedWentWrong;
......@@ -404,13 +404,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
404404 ,
405405 },
406406 .ReleaseFast = .{
407 .expect =
407 .expect =
408408 \\error: SomethingUnrelatedWentWrong
409409 \\
410410 ,
411411 },
412412 .ReleaseSmall = .{
413 .expect =
413 .expect =
414414 \\error: SomethingUnrelatedWentWrong
415415 \\
416416 ,
......@@ -419,7 +419,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
419419
420420 cases.addCase(.{
421421 .name = "error stored in const has trace preserved for duration of block",
422 .source =
422 .source =
423423 \\fn foo() !void { return error.TheSkyIsFalling; }
424424 \\fn bar() !void { return error.InternalError; }
425425 \\fn baz() !void { return error.UnexpectedReality; }
......@@ -446,7 +446,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
446446 \\}
447447 ,
448448 .Debug = .{
449 .expect =
449 .expect =
450450 \\error: StillUnresolved
451451 \\source.zig:1:18: [address] in foo (test)
452452 \\fn foo() !void { return error.TheSkyIsFalling; }
......@@ -465,7 +465,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
465465 .windows, // TODO
466466 .linux, // defeated by aggressive inlining
467467 },
468 .expect =
468 .expect =
469469 \\error: StillUnresolved
470470 \\source.zig:1:18: [address] in [function]
471471 \\fn foo() !void { return error.TheSkyIsFalling; }
......@@ -480,13 +480,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
480480 ,
481481 },
482482 .ReleaseFast = .{
483 .expect =
483 .expect =
484484 \\error: StillUnresolved
485485 \\
486486 ,
487487 },
488488 .ReleaseSmall = .{
489 .expect =
489 .expect =
490490 \\error: StillUnresolved
491491 \\
492492 ,
......@@ -495,7 +495,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
495495
496496 cases.addCase(.{
497497 .name = "error passed to function has its trace preserved for duration of the call",
498 .source =
498 .source =
499499 \\pub fn expectError(expected_error: anyerror, actual_error: anyerror!void) !void {
500500 \\ actual_error catch |err| {
501501 \\ if (err == expected_error) return {};
......@@ -516,7 +516,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
516516 \\}
517517 ,
518518 .Debug = .{
519 .expect =
519 .expect =
520520 \\error: TestExpectedError
521521 \\source.zig:9:18: [address] in foo (test)
522522 \\fn foo() !void { return error.Foo; }
......@@ -534,7 +534,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
534534 .exclude_os = .{
535535 .windows, // TODO
536536 },
537 .expect =
537 .expect =
538538 \\error: TestExpectedError
539539 \\source.zig:9:18: [address] in [function]
540540 \\fn foo() !void { return error.Foo; }
......@@ -549,13 +549,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
549549 ,
550550 },
551551 .ReleaseFast = .{
552 .expect =
552 .expect =
553553 \\error: TestExpectedError
554554 \\
555555 ,
556556 },
557557 .ReleaseSmall = .{
558 .expect =
558 .expect =
559559 \\error: TestExpectedError
560560 \\
561561 ,
......@@ -564,7 +564,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
564564
565565 cases.addCase(.{
566566 .name = "try return from within catch",
567 .source =
567 .source =
568568 \\fn foo() !void {
569569 \\ return error.TheSkyIsFalling;
570570 \\}
......@@ -580,7 +580,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
580580 \\}
581581 ,
582582 .Debug = .{
583 .expect =
583 .expect =
584584 \\error: AndMyCarIsOutOfGas
585585 \\source.zig:2:5: [address] in foo (test)
586586 \\ return error.TheSkyIsFalling;
......@@ -598,7 +598,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
598598 .exclude_os = .{
599599 .windows, // TODO
600600 },
601 .expect =
601 .expect =
602602 \\error: AndMyCarIsOutOfGas
603603 \\source.zig:2:5: [address] in [function]
604604 \\ return error.TheSkyIsFalling;
......@@ -613,13 +613,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
613613 ,
614614 },
615615 .ReleaseFast = .{
616 .expect =
616 .expect =
617617 \\error: AndMyCarIsOutOfGas
618618 \\
619619 ,
620620 },
621621 .ReleaseSmall = .{
622 .expect =
622 .expect =
623623 \\error: AndMyCarIsOutOfGas
624624 \\
625625 ,
......@@ -628,7 +628,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
628628
629629 cases.addCase(.{
630630 .name = "try return from within if-else",
631 .source =
631 .source =
632632 \\fn foo() !void {
633633 \\ return error.TheSkyIsFalling;
634634 \\}
......@@ -644,7 +644,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
644644 \\}
645645 ,
646646 .Debug = .{
647 .expect =
647 .expect =
648648 \\error: AndMyCarIsOutOfGas
649649 \\source.zig:2:5: [address] in foo (test)
650650 \\ return error.TheSkyIsFalling;
......@@ -662,7 +662,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
662662 .exclude_os = .{
663663 .windows, // TODO
664664 },
665 .expect =
665 .expect =
666666 \\error: AndMyCarIsOutOfGas
667667 \\source.zig:2:5: [address] in [function]
668668 \\ return error.TheSkyIsFalling;
......@@ -677,13 +677,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
677677 ,
678678 },
679679 .ReleaseFast = .{
680 .expect =
680 .expect =
681681 \\error: AndMyCarIsOutOfGas
682682 \\
683683 ,
684684 },
685685 .ReleaseSmall = .{
686 .expect =
686 .expect =
687687 \\error: AndMyCarIsOutOfGas
688688 \\
689689 ,
......@@ -692,7 +692,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
692692
693693 cases.addCase(.{
694694 .name = "try try return return",
695 .source =
695 .source =
696696 \\fn foo() !void {
697697 \\ try bar();
698698 \\}
......@@ -710,7 +710,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
710710 \\}
711711 ,
712712 .Debug = .{
713 .expect =
713 .expect =
714714 \\error: TheSkyIsFalling
715715 \\source.zig:10:5: [address] in make_error (test)
716716 \\ return error.TheSkyIsFalling;
......@@ -731,7 +731,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
731731 .exclude_os = .{
732732 .windows, // TODO
733733 },
734 .expect =
734 .expect =
735735 \\error: TheSkyIsFalling
736736 \\source.zig:10:5: [address] in [function]
737737 \\ return error.TheSkyIsFalling;
......@@ -749,13 +749,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
749749 ,
750750 },
751751 .ReleaseFast = .{
752 .expect =
752 .expect =
753753 \\error: TheSkyIsFalling
754754 \\
755755 ,
756756 },
757757 .ReleaseSmall = .{
758 .expect =
758 .expect =
759759 \\error: TheSkyIsFalling
760760 \\
761761 ,
......@@ -768,7 +768,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
768768 .windows, // TODO intermittent failures
769769 },
770770 .name = "dumpCurrentStackTrace",
771 .source =
771 .source =
772772 \\const std = @import("std");
773773 \\
774774 \\fn bar() void {
......@@ -783,7 +783,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
783783 \\}
784784 ,
785785 .Debug = .{
786 .expect =
786 .expect =
787787 \\source.zig:7:8: [address] in foo (test)
788788 \\ bar();
789789 \\ ^