| ... | @@ -376,7 +376,7 @@ pub const Parser = struct { | ... | @@ -376,7 +376,7 @@ pub const Parser = struct { |
| 376 | .body_node = &block.base, | 376 | .body_node = &block.base, |
| 377 | }); | 377 | }); |
| 378 | try root_node.decls.append(&test_node.base); | 378 | try root_node.decls.append(&test_node.base); |
| 379 | stack.append(State { .Block = block }) catch unreachable; | 379 | try stack.append(State { .Block = block }); |
| 380 | try stack.append(State { | 380 | try stack.append(State { |
| 381 | .ExpectTokenSave = ExpectTokenSave { | 381 | .ExpectTokenSave = ExpectTokenSave { |
| 382 | .id = Token.Id.LBrace, | 382 | .id = Token.Id.LBrace, |
| ... | @@ -616,14 +616,18 @@ pub const Parser = struct { | ... | @@ -616,14 +616,18 @@ pub const Parser = struct { |
| 616 | State.TopLevelExternOrField => |ctx| { | 616 | State.TopLevelExternOrField => |ctx| { |
| 617 | if (self.eatToken(Token.Id.Identifier)) |identifier| { | 617 | if (self.eatToken(Token.Id.Identifier)) |identifier| { |
| 618 | std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct); | 618 | std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct); |
| 619 | const node = try self.createAttachNode(arena, &ctx.container_decl.fields_and_decls, ast.Node.StructField, | 619 | const node = try arena.construct(ast.Node.StructField { |
| 620 | ast.Node.StructField { | 620 | .base = ast.Node { |
| 621 | .base = undefined, | 621 | .id = ast.Node.Id.StructField, |
| 622 | .visib_token = ctx.visib_token, | 622 | .before_comments = null, |
| 623 | .name_token = identifier, | 623 | .same_line_comment = null, |
| 624 | .type_expr = undefined, | 624 | }, |
| 625 | } | 625 | .visib_token = ctx.visib_token, |
| 626 | ); | 626 | .name_token = identifier, |
| | 627 | .type_expr = undefined, |
| | 628 | }); |
| | 629 | const node_ptr = try ctx.container_decl.fields_and_decls.addOne(); |
| | 630 | *node_ptr = &node.base; |
| 627 | | 631 | |
| 628 | stack.append(State { .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable; | 632 | stack.append(State { .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable; |
| 629 | try stack.append(State { .Expression = OptionalCtx { .Required = &node.type_expr } }); | 633 | try stack.append(State { .Expression = OptionalCtx { .Required = &node.type_expr } }); |
| ... | @@ -706,16 +710,20 @@ pub const Parser = struct { | ... | @@ -706,16 +710,20 @@ pub const Parser = struct { |
| 706 | Token.Id.Identifier => { | 710 | Token.Id.Identifier => { |
| 707 | switch (container_decl.kind) { | 711 | switch (container_decl.kind) { |
| 708 | ast.Node.ContainerDecl.Kind.Struct => { | 712 | ast.Node.ContainerDecl.Kind.Struct => { |
| 709 | const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.Node.StructField, | 713 | const node = try arena.construct(ast.Node.StructField { |
| 710 | ast.Node.StructField { | 714 | .base = ast.Node { |
| 711 | .base = undefined, | 715 | .id = ast.Node.Id.StructField, |
| 712 | .visib_token = null, | 716 | .before_comments = null, |
| 713 | .name_token = token, | 717 | .same_line_comment = null, |
| 714 | .type_expr = undefined, | 718 | }, |
| 715 | } | 719 | .visib_token = null, |
| 716 | ); | 720 | .name_token = token, |
| | 721 | .type_expr = undefined, |
| | 722 | }); |
| | 723 | const node_ptr = try container_decl.fields_and_decls.addOne(); |
| | 724 | *node_ptr = &node.base; |
| 717 | | 725 | |
| 718 | stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable; | 726 | try stack.append(State { .FieldListCommaOrEnd = container_decl }); |
| 719 | try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.type_expr } }); | 727 | try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.type_expr } }); |
| 720 | try stack.append(State { .ExpectToken = Token.Id.Colon }); | 728 | try stack.append(State { .ExpectToken = Token.Id.Colon }); |
| 721 | continue; | 729 | continue; |
| ... | @@ -1336,23 +1344,7 @@ pub const Parser = struct { | ... | @@ -1336,23 +1344,7 @@ pub const Parser = struct { |
| 1336 | }, | 1344 | }, |
| 1337 | | 1345 | |
| 1338 | State.LookForSameLineComment => |node_ptr| { | 1346 | State.LookForSameLineComment => |node_ptr| { |
| 1339 | const node = *node_ptr; | 1347 | try self.lookForSameLineComment(arena, *node_ptr); |
| 1340 | const node_last_token = node.lastToken(); | | |
| 1341 | | | |
| 1342 | const line_comment_token = self.getNextToken(); | | |
| 1343 | if (line_comment_token.id != Token.Id.LineComment) { | | |
| 1344 | self.putBackToken(line_comment_token); | | |
| 1345 | continue; | | |
| 1346 | } | | |
| 1347 | | | |
| 1348 | const offset_loc = self.tokenizer.getTokenLocation(node_last_token.end, line_comment_token); | | |
| 1349 | const different_line = offset_loc.line != 0; | | |
| 1350 | if (different_line) { | | |
| 1351 | self.putBackToken(line_comment_token); | | |
| 1352 | continue; | | |
| 1353 | } | | |
| 1354 | | | |
| 1355 | node.same_line_comment = try arena.construct(line_comment_token); | | |
| 1356 | continue; | 1348 | continue; |
| 1357 | }, | 1349 | }, |
| 1358 | | 1350 | |
| ... | @@ -1463,14 +1455,16 @@ pub const Parser = struct { | ... | @@ -1463,14 +1455,16 @@ pub const Parser = struct { |
| 1463 | continue; | 1455 | continue; |
| 1464 | } | 1456 | } |
| 1465 | | 1457 | |
| 1466 | const node = try self.createNode(arena, ast.Node.FieldInitializer, | 1458 | const node = try arena.construct(ast.Node.FieldInitializer { |
| 1467 | ast.Node.FieldInitializer { | 1459 | .base = ast.Node { |
| 1468 | .base = undefined, | 1460 | .id = ast.Node.Id.FieldInitializer, |
| 1469 | .period_token = undefined, | 1461 | .before_comments = null, |
| 1470 | .name_token = undefined, | 1462 | .same_line_comment = null, |
| 1471 | .expr = undefined, | 1463 | }, |
| 1472 | } | 1464 | .period_token = undefined, |
| 1473 | ); | 1465 | .name_token = undefined, |
| | 1466 | .expr = undefined, |
| | 1467 | }); |
| 1474 | try list_state.list.append(node); | 1468 | try list_state.list.append(node); |
| 1475 | | 1469 | |
| 1476 | stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable; | 1470 | stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable; |
| ... | @@ -1504,7 +1498,8 @@ pub const Parser = struct { | ... | @@ -1504,7 +1498,8 @@ pub const Parser = struct { |
| 1504 | container_decl.rbrace_token = end; | 1498 | container_decl.rbrace_token = end; |
| 1505 | continue; | 1499 | continue; |
| 1506 | } else { | 1500 | } else { |
| 1507 | stack.append(State { .ContainerDecl = container_decl }) catch unreachable; | 1501 | try self.lookForSameLineComment(arena, container_decl.fields_and_decls.toSlice()[container_decl.fields_and_decls.len - 1]); |
| | 1502 | try stack.append(State { .ContainerDecl = container_decl }); |
| 1508 | continue; | 1503 | continue; |
| 1509 | } | 1504 | } |
| 1510 | }, | 1505 | }, |
| ... | @@ -2908,6 +2903,25 @@ pub const Parser = struct { | ... | @@ -2908,6 +2903,25 @@ pub const Parser = struct { |
| 2908 | } | 2903 | } |
| 2909 | } | 2904 | } |
| 2910 | | 2905 | |
| | 2906 | fn lookForSameLineComment(self: &Parser, arena: &mem.Allocator, node: &ast.Node) !void { |
| | 2907 | const node_last_token = node.lastToken(); |
| | 2908 | |
| | 2909 | const line_comment_token = self.getNextToken(); |
| | 2910 | if (line_comment_token.id != Token.Id.LineComment) { |
| | 2911 | self.putBackToken(line_comment_token); |
| | 2912 | return; |
| | 2913 | } |
| | 2914 | |
| | 2915 | const offset_loc = self.tokenizer.getTokenLocation(node_last_token.end, line_comment_token); |
| | 2916 | const different_line = offset_loc.line != 0; |
| | 2917 | if (different_line) { |
| | 2918 | self.putBackToken(line_comment_token); |
| | 2919 | return; |
| | 2920 | } |
| | 2921 | |
| | 2922 | node.same_line_comment = try arena.construct(line_comment_token); |
| | 2923 | } |
| | 2924 | |
| 2911 | fn parseStringLiteral(self: &Parser, arena: &mem.Allocator, token: &const Token) !?&ast.Node { | 2925 | fn parseStringLiteral(self: &Parser, arena: &mem.Allocator, token: &const Token) !?&ast.Node { |
| 2912 | switch (token.id) { | 2926 | switch (token.id) { |
| 2913 | Token.Id.StringLiteral => { | 2927 | Token.Id.StringLiteral => { |
| ... | @@ -3341,6 +3355,7 @@ pub const Parser = struct { | ... | @@ -3341,6 +3355,7 @@ pub const Parser = struct { |
| 3341 | while (stack.popOrNull()) |state| { | 3355 | while (stack.popOrNull()) |state| { |
| 3342 | switch (state) { | 3356 | switch (state) { |
| 3343 | RenderState.TopLevelDecl => |decl| { | 3357 | RenderState.TopLevelDecl => |decl| { |
| | 3358 | try stack.append(RenderState { .PrintSameLineComment = decl.same_line_comment } ); |
| 3344 | switch (decl.id) { | 3359 | switch (decl.id) { |
| 3345 | ast.Node.Id.FnProto => { | 3360 | ast.Node.Id.FnProto => { |
| 3346 | const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl); | 3361 | const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl); |
| ... | @@ -3383,12 +3398,14 @@ pub const Parser = struct { | ... | @@ -3383,12 +3398,14 @@ pub const Parser = struct { |
| 3383 | try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); | 3398 | try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); |
| 3384 | } | 3399 | } |
| 3385 | try stream.print("{}: ", self.tokenizer.getTokenSlice(field.name_token)); | 3400 | try stream.print("{}: ", self.tokenizer.getTokenSlice(field.name_token)); |
| | 3401 | try stack.append(RenderState { .Text = "," }); |
| 3386 | try stack.append(RenderState { .Expression = field.type_expr}); | 3402 | try stack.append(RenderState { .Expression = field.type_expr}); |
| 3387 | }, | 3403 | }, |
| 3388 | ast.Node.Id.UnionTag => { | 3404 | ast.Node.Id.UnionTag => { |
| 3389 | const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl); | 3405 | const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl); |
| 3390 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); | 3406 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); |
| 3391 | | 3407 | |
| | 3408 | try stack.append(RenderState { .Text = "," }); |
| 3392 | if (tag.type_expr) |type_expr| { | 3409 | if (tag.type_expr) |type_expr| { |
| 3393 | try stream.print(": "); | 3410 | try stream.print(": "); |
| 3394 | try stack.append(RenderState { .Expression = type_expr}); | 3411 | try stack.append(RenderState { .Expression = type_expr}); |
| ... | @@ -3398,6 +3415,7 @@ pub const Parser = struct { | ... | @@ -3398,6 +3415,7 @@ pub const Parser = struct { |
| 3398 | const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl); | 3415 | const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl); |
| 3399 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); | 3416 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); |
| 3400 | | 3417 | |
| | 3418 | try stack.append(RenderState { .Text = "," }); |
| 3401 | if (tag.value) |value| { | 3419 | if (tag.value) |value| { |
| 3402 | try stream.print(" = "); | 3420 | try stream.print(" = "); |
| 3403 | try stack.append(RenderState { .Expression = value}); | 3421 | try stack.append(RenderState { .Expression = value}); |
| ... | @@ -3899,14 +3917,6 @@ pub const Parser = struct { | ... | @@ -3899,14 +3917,6 @@ pub const Parser = struct { |
| 3899 | while (i != 0) { | 3917 | while (i != 0) { |
| 3900 | i -= 1; | 3918 | i -= 1; |
| 3901 | const node = fields_and_decls[i]; | 3919 | const node = fields_and_decls[i]; |
| 3902 | switch (node.id) { | | |
| 3903 | ast.Node.Id.StructField, | | |
| 3904 | ast.Node.Id.UnionTag, | | |
| 3905 | ast.Node.Id.EnumTag => { | | |
| 3906 | try stack.append(RenderState { .Text = "," }); | | |
| 3907 | }, | | |
| 3908 | else => { } | | |
| 3909 | } | | |
| 3910 | try stack.append(RenderState { .TopLevelDecl = node}); | 3920 | try stack.append(RenderState { .TopLevelDecl = node}); |
| 3911 | try stack.append(RenderState.PrintIndent); | 3921 | try stack.append(RenderState.PrintIndent); |
| 3912 | try stack.append(RenderState { | 3922 | try stack.append(RenderState { |
| ... | @@ -4466,957 +4476,6 @@ pub const Parser = struct { | ... | @@ -4466,957 +4476,6 @@ pub const Parser = struct { |
| 4466 | | 4476 | |
| 4467 | }; | 4477 | }; |
| 4468 | | 4478 | |
| 4469 | var fixed_buffer_mem: [100 * 1024]u8 = undefined; | 4479 | test "std.zig.parser" { |
| 4470 | | 4480 | _ = @import("parser_test.zig"); |
| 4471 | fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 { | | |
| 4472 | var tokenizer = Tokenizer.init(source); | | |
| 4473 | var parser = Parser.init(&tokenizer, allocator, "(memory buffer)"); | | |
| 4474 | defer parser.deinit(); | | |
| 4475 | | | |
| 4476 | var tree = try parser.parse(); | | |
| 4477 | defer tree.deinit(); | | |
| 4478 | | | |
| 4479 | var buffer = try std.Buffer.initSize(allocator, 0); | | |
| 4480 | errdefer buffer.deinit(); | | |
| 4481 | | | |
| 4482 | var buffer_out_stream = io.BufferOutStream.init(&buffer); | | |
| 4483 | try parser.renderSource(&buffer_out_stream.stream, tree.root_node); | | |
| 4484 | return buffer.toOwnedSlice(); | | |
| 4485 | } | | |
| 4486 | | | |
| 4487 | fn testCanonical(source: []const u8) !void { | | |
| 4488 | const needed_alloc_count = x: { | | |
| 4489 | // Try it once with unlimited memory, make sure it works | | |
| 4490 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | | |
| 4491 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize)); | | |
| 4492 | const result_source = try testParse(source, &failing_allocator.allocator); | | |
| 4493 | if (!mem.eql(u8, result_source, source)) { | | |
| 4494 | warn("\n====== expected this output: =========\n"); | | |
| 4495 | warn("{}", source); | | |
| 4496 | warn("\n======== instead found this: =========\n"); | | |
| 4497 | warn("{}", result_source); | | |
| 4498 | warn("\n======================================\n"); | | |
| 4499 | return error.TestFailed; | | |
| 4500 | } | | |
| 4501 | failing_allocator.allocator.free(result_source); | | |
| 4502 | break :x failing_allocator.index; | | |
| 4503 | }; | | |
| 4504 | | | |
| 4505 | var fail_index: usize = 0; | | |
| 4506 | while (fail_index < needed_alloc_count) : (fail_index += 1) { | | |
| 4507 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | | |
| 4508 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index); | | |
| 4509 | if (testParse(source, &failing_allocator.allocator)) |_| { | | |
| 4510 | return error.NondeterministicMemoryUsage; | | |
| 4511 | } else |err| switch (err) { | | |
| 4512 | error.OutOfMemory => { | | |
| 4513 | if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) { | | |
| 4514 | warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n", | | |
| 4515 | fail_index, needed_alloc_count, | | |
| 4516 | failing_allocator.allocated_bytes, failing_allocator.freed_bytes, | | |
| 4517 | failing_allocator.index, failing_allocator.deallocations); | | |
| 4518 | return error.MemoryLeakDetected; | | |
| 4519 | } | | |
| 4520 | }, | | |
| 4521 | error.ParseError => @panic("test failed"), | | |
| 4522 | } | | |
| 4523 | } | | |
| 4524 | } | | |
| 4525 | | | |
| 4526 | test "zig fmt: array literal with 1 item on 1 line" { | | |
| 4527 | try testCanonical( | | |
| 4528 | \\var s = []const u64 {0} ** 25; | | |
| 4529 | \\ | | |
| 4530 | ); | | |
| 4531 | } | | |
| 4532 | | | |
| 4533 | test "zig fmt: preserve same-line comment after a statement" { | | |
| 4534 | try testCanonical( | | |
| 4535 | \\test "" { | | |
| 4536 | \\ a = b; | | |
| 4537 | \\ debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption | | |
| 4538 | \\ a = b; | | |
| 4539 | \\} | | |
| 4540 | \\ | | |
| 4541 | ); | | |
| 4542 | } | | |
| 4543 | | | |
| 4544 | test "zig fmt: preserve comments before global variables" { | | |
| 4545 | try testCanonical( | | |
| 4546 | \\/// Foo copies keys and values before they go into the map, and | | |
| 4547 | \\/// frees them when they get removed. | | |
| 4548 | \\pub const Foo = struct {}; | | |
| 4549 | \\ | | |
| 4550 | ); | | |
| 4551 | } | | |
| 4552 | | | |
| 4553 | test "zig fmt: preserve comments before statements" { | | |
| 4554 | try testCanonical( | | |
| 4555 | \\test "std" { | | |
| 4556 | \\ // statement comment | | |
| 4557 | \\ _ = @import("foo/bar.zig"); | | |
| 4558 | \\} | | |
| 4559 | \\ | | |
| 4560 | ); | | |
| 4561 | } | | |
| 4562 | | | |
| 4563 | test "zig fmt: preserve top level comments" { | | |
| 4564 | try testCanonical( | | |
| 4565 | \\// top level comment | | |
| 4566 | \\test "hi" {} | | |
| 4567 | \\ | | |
| 4568 | ); | | |
| 4569 | } | | |
| 4570 | | | |
| 4571 | test "zig fmt: get stdout or fail" { | | |
| 4572 | try testCanonical( | | |
| 4573 | \\const std = @import("std"); | | |
| 4574 | \\ | | |
| 4575 | \\pub fn main() !void { | | |
| 4576 | \\ // If this program is run without stdout attached, exit with an error. | | |
| 4577 | \\ // another comment | | |
| 4578 | \\ var stdout_file = try std.io.getStdOut; | | |
| 4579 | \\} | | |
| 4580 | \\ | | |
| 4581 | ); | | |
| 4582 | } | | |
| 4583 | | | |
| 4584 | test "zig fmt: preserve spacing" { | | |
| 4585 | try testCanonical( | | |
| 4586 | \\const std = @import("std"); | | |
| 4587 | \\ | | |
| 4588 | \\pub fn main() !void { | | |
| 4589 | \\ var stdout_file = try std.io.getStdOut; | | |
| 4590 | \\ var stdout_file = try std.io.getStdOut; | | |
| 4591 | \\ | | |
| 4592 | \\ var stdout_file = try std.io.getStdOut; | | |
| 4593 | \\ var stdout_file = try std.io.getStdOut; | | |
| 4594 | \\} | | |
| 4595 | \\ | | |
| 4596 | ); | | |
| 4597 | } | | |
| 4598 | | | |
| 4599 | test "zig fmt: return types" { | | |
| 4600 | try testCanonical( | | |
| 4601 | \\pub fn main() !void {} | | |
| 4602 | \\pub fn main() var {} | | |
| 4603 | \\pub fn main() i32 {} | | |
| 4604 | \\ | | |
| 4605 | ); | | |
| 4606 | } | | |
| 4607 | | | |
| 4608 | test "zig fmt: imports" { | | |
| 4609 | try testCanonical( | | |
| 4610 | \\const std = @import("std"); | | |
| 4611 | \\const std = @import(); | | |
| 4612 | \\ | | |
| 4613 | ); | | |
| 4614 | } | | |
| 4615 | | | |
| 4616 | test "zig fmt: global declarations" { | | |
| 4617 | try testCanonical( | | |
| 4618 | \\const a = b; | | |
| 4619 | \\pub const a = b; | | |
| 4620 | \\var a = b; | | |
| 4621 | \\pub var a = b; | | |
| 4622 | \\const a: i32 = b; | | |
| 4623 | \\pub const a: i32 = b; | | |
| 4624 | \\var a: i32 = b; | | |
| 4625 | \\pub var a: i32 = b; | | |
| 4626 | \\extern const a: i32 = b; | | |
| 4627 | \\pub extern const a: i32 = b; | | |
| 4628 | \\extern var a: i32 = b; | | |
| 4629 | \\pub extern var a: i32 = b; | | |
| 4630 | \\extern "a" const a: i32 = b; | | |
| 4631 | \\pub extern "a" const a: i32 = b; | | |
| 4632 | \\extern "a" var a: i32 = b; | | |
| 4633 | \\pub extern "a" var a: i32 = b; | | |
| 4634 | \\ | | |
| 4635 | ); | | |
| 4636 | } | | |
| 4637 | | | |
| 4638 | test "zig fmt: extern declaration" { | | |
| 4639 | try testCanonical( | | |
| 4640 | \\extern var foo: c_int; | | |
| 4641 | \\ | | |
| 4642 | ); | | |
| 4643 | } | | |
| 4644 | | | |
| 4645 | test "zig fmt: alignment" { | | |
| 4646 | try testCanonical( | | |
| 4647 | \\var foo: c_int align(1); | | |
| 4648 | \\ | | |
| 4649 | ); | | |
| 4650 | } | | |
| 4651 | | | |
| 4652 | test "zig fmt: C main" { | | |
| 4653 | try testCanonical( | | |
| 4654 | \\fn main(argc: c_int, argv: &&u8) c_int { | | |
| 4655 | \\ const a = b; | | |
| 4656 | \\} | | |
| 4657 | \\ | | |
| 4658 | ); | | |
| 4659 | } | | |
| 4660 | | | |
| 4661 | test "zig fmt: return" { | | |
| 4662 | try testCanonical( | | |
| 4663 | \\fn foo(argc: c_int, argv: &&u8) c_int { | | |
| 4664 | \\ return 0; | | |
| 4665 | \\} | | |
| 4666 | \\ | | |
| 4667 | \\fn bar() void { | | |
| 4668 | \\ return; | | |
| 4669 | \\} | | |
| 4670 | \\ | | |
| 4671 | ); | | |
| 4672 | } | | |
| 4673 | | | |
| 4674 | test "zig fmt: pointer attributes" { | | |
| 4675 | try testCanonical( | | |
| 4676 | \\extern fn f1(s: &align(&u8) u8) c_int; | | |
| 4677 | \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; | | |
| 4678 | \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; | | |
| 4679 | \\extern fn f4(s: &align(1) const volatile u8) c_int; | | |
| 4680 | \\ | | |
| 4681 | ); | | |
| 4682 | } | | |
| 4683 | | | |
| 4684 | test "zig fmt: slice attributes" { | | |
| 4685 | try testCanonical( | | |
| 4686 | \\extern fn f1(s: &align(&u8) u8) c_int; | | |
| 4687 | \\extern fn f2(s: &&align(1) &const &volatile u8) c_int; | | |
| 4688 | \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int; | | |
| 4689 | \\extern fn f4(s: &align(1) const volatile u8) c_int; | | |
| 4690 | \\ | | |
| 4691 | ); | | |
| 4692 | } | | |
| 4693 | | | |
| 4694 | test "zig fmt: test declaration" { | | |
| 4695 | try testCanonical( | | |
| 4696 | \\test "test name" { | | |
| 4697 | \\ const a = 1; | | |
| 4698 | \\ var b = 1; | | |
| 4699 | \\} | | |
| 4700 | \\ | | |
| 4701 | ); | | |
| 4702 | } | | |
| 4703 | | | |
| 4704 | test "zig fmt: infix operators" { | | |
| 4705 | try testCanonical( | | |
| 4706 | \\test "infix operators" { | | |
| 4707 | \\ var i = undefined; | | |
| 4708 | \\ i = 2; | | |
| 4709 | \\ i *= 2; | | |
| 4710 | \\ i |= 2; | | |
| 4711 | \\ i ^= 2; | | |
| 4712 | \\ i <<= 2; | | |
| 4713 | \\ i >>= 2; | | |
| 4714 | \\ i &= 2; | | |
| 4715 | \\ i *= 2; | | |
| 4716 | \\ i *%= 2; | | |
| 4717 | \\ i -= 2; | | |
| 4718 | \\ i -%= 2; | | |
| 4719 | \\ i += 2; | | |
| 4720 | \\ i +%= 2; | | |
| 4721 | \\ i /= 2; | | |
| 4722 | \\ i %= 2; | | |
| 4723 | \\ _ = i == i; | | |
| 4724 | \\ _ = i != i; | | |
| 4725 | \\ _ = i != i; | | |
| 4726 | \\ _ = i.i; | | |
| 4727 | \\ _ = i || i; | | |
| 4728 | \\ _ = i!i; | | |
| 4729 | \\ _ = i ** i; | | |
| 4730 | \\ _ = i ++ i; | | |
| 4731 | \\ _ = i ?? i; | | |
| 4732 | \\ _ = i % i; | | |
| 4733 | \\ _ = i / i; | | |
| 4734 | \\ _ = i *% i; | | |
| 4735 | \\ _ = i * i; | | |
| 4736 | \\ _ = i -% i; | | |
| 4737 | \\ _ = i - i; | | |
| 4738 | \\ _ = i +% i; | | |
| 4739 | \\ _ = i + i; | | |
| 4740 | \\ _ = i << i; | | |
| 4741 | \\ _ = i >> i; | | |
| 4742 | \\ _ = i & i; | | |
| 4743 | \\ _ = i ^ i; | | |
| 4744 | \\ _ = i | i; | | |
| 4745 | \\ _ = i >= i; | | |
| 4746 | \\ _ = i <= i; | | |
| 4747 | \\ _ = i > i; | | |
| 4748 | \\ _ = i < i; | | |
| 4749 | \\ _ = i and i; | | |
| 4750 | \\ _ = i or i; | | |
| 4751 | \\} | | |
| 4752 | \\ | | |
| 4753 | ); | | |
| 4754 | } | | |
| 4755 | | | |
| 4756 | test "zig fmt: precedence" { | | |
| 4757 | try testCanonical( | | |
| 4758 | \\test "precedence" { | | |
| 4759 | \\ a!b(); | | |
| 4760 | \\ (a!b)(); | | |
| 4761 | \\ !a!b; | | |
| 4762 | \\ !(a!b); | | |
| 4763 | \\ !a{}; | | |
| 4764 | \\ !(a{}); | | |
| 4765 | \\ a + b{}; | | |
| 4766 | \\ (a + b){}; | | |
| 4767 | \\ a << b + c; | | |
| 4768 | \\ (a << b) + c; | | |
| 4769 | \\ a & b << c; | | |
| 4770 | \\ (a & b) << c; | | |
| 4771 | \\ a ^ b & c; | | |
| 4772 | \\ (a ^ b) & c; | | |
| 4773 | \\ a | b ^ c; | | |
| 4774 | \\ (a | b) ^ c; | | |
| 4775 | \\ a == b | c; | | |
| 4776 | \\ (a == b) | c; | | |
| 4777 | \\ a and b == c; | | |
| 4778 | \\ (a and b) == c; | | |
| 4779 | \\ a or b and c; | | |
| 4780 | \\ (a or b) and c; | | |
| 4781 | \\ (a or b) and c; | | |
| 4782 | \\} | | |
| 4783 | \\ | | |
| 4784 | ); | | |
| 4785 | } | | |
| 4786 | | | |
| 4787 | test "zig fmt: prefix operators" { | | |
| 4788 | try testCanonical( | | |
| 4789 | \\test "prefix operators" { | | |
| 4790 | \\ try return --%~??!*&0; | | |
| 4791 | \\} | | |
| 4792 | \\ | | |
| 4793 | ); | | |
| 4794 | } | | |
| 4795 | | | |
| 4796 | test "zig fmt: call expression" { | | |
| 4797 | try testCanonical( | | |
| 4798 | \\test "test calls" { | | |
| 4799 | \\ a(); | | |
| 4800 | \\ a(1); | | |
| 4801 | \\ a(1, 2); | | |
| 4802 | \\ a(1, 2) + a(1, 2); | | |
| 4803 | \\} | | |
| 4804 | \\ | | |
| 4805 | ); | | |
| 4806 | } | | |
| 4807 | | | |
| 4808 | test "zig fmt: var args" { | | |
| 4809 | try testCanonical( | | |
| 4810 | \\fn print(args: ...) void {} | | |
| 4811 | \\ | | |
| 4812 | ); | | |
| 4813 | } | | |
| 4814 | | | |
| 4815 | test "zig fmt: var type" { | | |
| 4816 | try testCanonical( | | |
| 4817 | \\fn print(args: var) var {} | | |
| 4818 | \\const Var = var; | | |
| 4819 | \\const i: var = 0; | | |
| 4820 | \\ | | |
| 4821 | ); | | |
| 4822 | } | | |
| 4823 | | | |
| 4824 | test "zig fmt: functions" { | | |
| 4825 | try testCanonical( | | |
| 4826 | \\extern fn puts(s: &const u8) c_int; | | |
| 4827 | \\extern "c" fn puts(s: &const u8) c_int; | | |
| 4828 | \\export fn puts(s: &const u8) c_int; | | |
| 4829 | \\inline fn puts(s: &const u8) c_int; | | |
| 4830 | \\pub extern fn puts(s: &const u8) c_int; | | |
| 4831 | \\pub extern "c" fn puts(s: &const u8) c_int; | | |
| 4832 | \\pub export fn puts(s: &const u8) c_int; | | |
| 4833 | \\pub inline fn puts(s: &const u8) c_int; | | |
| 4834 | \\pub extern fn puts(s: &const u8) align(2 + 2) c_int; | | |
| 4835 | \\pub extern "c" fn puts(s: &const u8) align(2 + 2) c_int; | | |
| 4836 | \\pub export fn puts(s: &const u8) align(2 + 2) c_int; | | |
| 4837 | \\pub inline fn puts(s: &const u8) align(2 + 2) c_int; | | |
| 4838 | \\ | | |
| 4839 | ); | | |
| 4840 | } | | |
| 4841 | | | |
| 4842 | test "zig fmt: multiline string" { | | |
| 4843 | try testCanonical( | | |
| 4844 | \\const s = | | |
| 4845 | \\ \\ something | | |
| 4846 | \\ \\ something else | | |
| 4847 | \\ ; | | |
| 4848 | \\ | | |
| 4849 | ); | | |
| 4850 | } | | |
| 4851 | | | |
| 4852 | test "zig fmt: values" { | | |
| 4853 | try testCanonical( | | |
| 4854 | \\test "values" { | | |
| 4855 | \\ 1; | | |
| 4856 | \\ 1.0; | | |
| 4857 | \\ "string"; | | |
| 4858 | \\ c"cstring"; | | |
| 4859 | \\ 'c'; | | |
| 4860 | \\ true; | | |
| 4861 | \\ false; | | |
| 4862 | \\ null; | | |
| 4863 | \\ undefined; | | |
| 4864 | \\ error; | | |
| 4865 | \\ this; | | |
| 4866 | \\ unreachable; | | |
| 4867 | \\} | | |
| 4868 | \\ | | |
| 4869 | ); | | |
| 4870 | } | | |
| 4871 | | | |
| 4872 | test "zig fmt: indexing" { | | |
| 4873 | try testCanonical( | | |
| 4874 | \\test "test index" { | | |
| 4875 | \\ a[0]; | | |
| 4876 | \\ a[0 + 5]; | | |
| 4877 | \\ a[0..]; | | |
| 4878 | \\ a[0..5]; | | |
| 4879 | \\ a[a[0]]; | | |
| 4880 | \\ a[a[0..]]; | | |
| 4881 | \\ a[a[0..5]]; | | |
| 4882 | \\ a[a[0]..]; | | |
| 4883 | \\ a[a[0..5]..]; | | |
| 4884 | \\ a[a[0]..a[0]]; | | |
| 4885 | \\ a[a[0..5]..a[0]]; | | |
| 4886 | \\ a[a[0..5]..a[0..5]]; | | |
| 4887 | \\} | | |
| 4888 | \\ | | |
| 4889 | ); | | |
| 4890 | } | | |
| 4891 | | | |
| 4892 | test "zig fmt: struct declaration" { | | |
| 4893 | try testCanonical( | | |
| 4894 | \\const S = struct { | | |
| 4895 | \\ const Self = this; | | |
| 4896 | \\ f1: u8, | | |
| 4897 | \\ pub f3: u8, | | |
| 4898 | \\ | | |
| 4899 | \\ fn method(self: &Self) Self { | | |
| 4900 | \\ return *self; | | |
| 4901 | \\ } | | |
| 4902 | \\ | | |
| 4903 | \\ f2: u8, | | |
| 4904 | \\}; | | |
| 4905 | \\ | | |
| 4906 | \\const Ps = packed struct { | | |
| 4907 | \\ a: u8, | | |
| 4908 | \\ pub b: u8, | | |
| 4909 | \\ | | |
| 4910 | \\ c: u8, | | |
| 4911 | \\}; | | |
| 4912 | \\ | | |
| 4913 | \\const Es = extern struct { | | |
| 4914 | \\ a: u8, | | |
| 4915 | \\ pub b: u8, | | |
| 4916 | \\ | | |
| 4917 | \\ c: u8, | | |
| 4918 | \\}; | | |
| 4919 | \\ | | |
| 4920 | ); | | |
| 4921 | } | | |
| 4922 | | | |
| 4923 | test "zig fmt: enum declaration" { | | |
| 4924 | try testCanonical( | | |
| 4925 | \\const E = enum { | | |
| 4926 | \\ Ok, | | |
| 4927 | \\ SomethingElse = 0, | | |
| 4928 | \\}; | | |
| 4929 | \\ | | |
| 4930 | \\const E2 = enum(u8) { | | |
| 4931 | \\ Ok, | | |
| 4932 | \\ SomethingElse = 255, | | |
| 4933 | \\ SomethingThird, | | |
| 4934 | \\}; | | |
| 4935 | \\ | | |
| 4936 | \\const Ee = extern enum { | | |
| 4937 | \\ Ok, | | |
| 4938 | \\ SomethingElse, | | |
| 4939 | \\ SomethingThird, | | |
| 4940 | \\}; | | |
| 4941 | \\ | | |
| 4942 | \\const Ep = packed enum { | | |
| 4943 | \\ Ok, | | |
| 4944 | \\ SomethingElse, | | |
| 4945 | \\ SomethingThird, | | |
| 4946 | \\}; | | |
| 4947 | \\ | | |
| 4948 | ); | | |
| 4949 | } | | |
| 4950 | | | |
| 4951 | test "zig fmt: union declaration" { | | |
| 4952 | try testCanonical( | | |
| 4953 | \\const U = union { | | |
| 4954 | \\ Int: u8, | | |
| 4955 | \\ Float: f32, | | |
| 4956 | \\ None, | | |
| 4957 | \\ Bool: bool, | | |
| 4958 | \\}; | | |
| 4959 | \\ | | |
| 4960 | \\const Ue = union(enum) { | | |
| 4961 | \\ Int: u8, | | |
| 4962 | \\ Float: f32, | | |
| 4963 | \\ None, | | |
| 4964 | \\ Bool: bool, | | |
| 4965 | \\}; | | |
| 4966 | \\ | | |
| 4967 | \\const E = enum { | | |
| 4968 | \\ Int, | | |
| 4969 | \\ Float, | | |
| 4970 | \\ None, | | |
| 4971 | \\ Bool, | | |
| 4972 | \\}; | | |
| 4973 | \\ | | |
| 4974 | \\const Ue2 = union(E) { | | |
| 4975 | \\ Int: u8, | | |
| 4976 | \\ Float: f32, | | |
| 4977 | \\ None, | | |
| 4978 | \\ Bool: bool, | | |
| 4979 | \\}; | | |
| 4980 | \\ | | |
| 4981 | \\const Eu = extern union { | | |
| 4982 | \\ Int: u8, | | |
| 4983 | \\ Float: f32, | | |
| 4984 | \\ None, | | |
| 4985 | \\ Bool: bool, | | |
| 4986 | \\}; | | |
| 4987 | \\ | | |
| 4988 | ); | | |
| 4989 | } | | |
| 4990 | | | |
| 4991 | test "zig fmt: error set declaration" { | | |
| 4992 | try testCanonical( | | |
| 4993 | \\const E = error { | | |
| 4994 | \\ A, | | |
| 4995 | \\ B, | | |
| 4996 | \\ | | |
| 4997 | \\ C, | | |
| 4998 | \\}; | | |
| 4999 | \\ | | |
| 5000 | ); | | |
| 5001 | } | | |
| 5002 | | | |
| 5003 | test "zig fmt: arrays" { | | |
| 5004 | try testCanonical( | | |
| 5005 | \\test "test array" { | | |
| 5006 | \\ const a: [2]u8 = [2]u8 { | | |
| 5007 | \\ 1, | | |
| 5008 | \\ 2, | | |
| 5009 | \\ }; | | |
| 5010 | \\ const a: [2]u8 = []u8 { | | |
| 5011 | \\ 1, | | |
| 5012 | \\ 2, | | |
| 5013 | \\ }; | | |
| 5014 | \\ const a: [0]u8 = []u8{}; | | |
| 5015 | \\} | | |
| 5016 | \\ | | |
| 5017 | ); | | |
| 5018 | } | | |
| 5019 | | | |
| 5020 | test "zig fmt: container initializers" { | | |
| 5021 | try testCanonical( | | |
| 5022 | \\const a1 = []u8{}; | | |
| 5023 | \\const a2 = []u8 { | | |
| 5024 | \\ 1, | | |
| 5025 | \\ 2, | | |
| 5026 | \\ 3, | | |
| 5027 | \\ 4, | | |
| 5028 | \\}; | | |
| 5029 | \\const s1 = S{}; | | |
| 5030 | \\const s2 = S { | | |
| 5031 | \\ .a = 1, | | |
| 5032 | \\ .b = 2, | | |
| 5033 | \\}; | | |
| 5034 | \\ | | |
| 5035 | ); | | |
| 5036 | } | | |
| 5037 | | | |
| 5038 | test "zig fmt: catch" { | | |
| 5039 | try testCanonical( | | |
| 5040 | \\test "catch" { | | |
| 5041 | \\ const a: error!u8 = 0; | | |
| 5042 | \\ _ = a catch return; | | |
| 5043 | \\ _ = a catch |err| return; | | |
| 5044 | \\} | | |
| 5045 | \\ | | |
| 5046 | ); | | |
| 5047 | } | | |
| 5048 | | | |
| 5049 | test "zig fmt: blocks" { | | |
| 5050 | try testCanonical( | | |
| 5051 | \\test "blocks" { | | |
| 5052 | \\ { | | |
| 5053 | \\ const a = 0; | | |
| 5054 | \\ const b = 0; | | |
| 5055 | \\ } | | |
| 5056 | \\ | | |
| 5057 | \\ blk: { | | |
| 5058 | \\ const a = 0; | | |
| 5059 | \\ const b = 0; | | |
| 5060 | \\ } | | |
| 5061 | \\ | | |
| 5062 | \\ const r = blk: { | | |
| 5063 | \\ const a = 0; | | |
| 5064 | \\ const b = 0; | | |
| 5065 | \\ }; | | |
| 5066 | \\} | | |
| 5067 | \\ | | |
| 5068 | ); | | |
| 5069 | } | | |
| 5070 | | | |
| 5071 | test "zig fmt: switch" { | | |
| 5072 | try testCanonical( | | |
| 5073 | \\test "switch" { | | |
| 5074 | \\ switch (0) { | | |
| 5075 | \\ 0 => {}, | | |
| 5076 | \\ 1 => unreachable, | | |
| 5077 | \\ 2, | | |
| 5078 | \\ 3 => {}, | | |
| 5079 | \\ 4 ... 7 => {}, | | |
| 5080 | \\ 1 + 4 * 3 + 22 => {}, | | |
| 5081 | \\ else => { | | |
| 5082 | \\ const a = 1; | | |
| 5083 | \\ const b = a; | | |
| 5084 | \\ }, | | |
| 5085 | \\ } | | |
| 5086 | \\ | | |
| 5087 | \\ const res = switch (0) { | | |
| 5088 | \\ 0 => 0, | | |
| 5089 | \\ 1 => 2, | | |
| 5090 | \\ 1 => a = 4, | | |
| 5091 | \\ else => 4, | | |
| 5092 | \\ }; | | |
| 5093 | \\ | | |
| 5094 | \\ const Union = union(enum) { | | |
| 5095 | \\ Int: i64, | | |
| 5096 | \\ Float: f64, | | |
| 5097 | \\ }; | | |
| 5098 | \\ | | |
| 5099 | \\ const u = Union { | | |
| 5100 | \\ .Int = 0, | | |
| 5101 | \\ }; | | |
| 5102 | \\ switch (u) { | | |
| 5103 | \\ Union.Int => |int| {}, | | |
| 5104 | \\ Union.Float => |*float| unreachable, | | |
| 5105 | \\ } | | |
| 5106 | \\} | | |
| 5107 | \\ | | |
| 5108 | ); | | |
| 5109 | } | | |
| 5110 | | | |
| 5111 | test "zig fmt: while" { | | |
| 5112 | try testCanonical( | | |
| 5113 | \\test "while" { | | |
| 5114 | \\ while (10 < 1) { | | |
| 5115 | \\ unreachable; | | |
| 5116 | \\ } | | |
| 5117 | \\ | | |
| 5118 | \\ while (10 < 1) | | |
| 5119 | \\ unreachable; | | |
| 5120 | \\ | | |
| 5121 | \\ var i: usize = 0; | | |
| 5122 | \\ while (i < 10) : (i += 1) { | | |
| 5123 | \\ continue; | | |
| 5124 | \\ } | | |
| 5125 | \\ | | |
| 5126 | \\ i = 0; | | |
| 5127 | \\ while (i < 10) : (i += 1) | | |
| 5128 | \\ continue; | | |
| 5129 | \\ | | |
| 5130 | \\ i = 0; | | |
| 5131 | \\ var j: usize = 0; | | |
| 5132 | \\ while (i < 10) : ({ | | |
| 5133 | \\ i += 1; | | |
| 5134 | \\ j += 1; | | |
| 5135 | \\ }) { | | |
| 5136 | \\ continue; | | |
| 5137 | \\ } | | |
| 5138 | \\ | | |
| 5139 | \\ var a: ?u8 = 2; | | |
| 5140 | \\ while (a) |v| : (a = null) { | | |
| 5141 | \\ continue; | | |
| 5142 | \\ } | | |
| 5143 | \\ | | |
| 5144 | \\ while (a) |v| : (a = null) | | |
| 5145 | \\ unreachable; | | |
| 5146 | \\ | | |
| 5147 | \\ label: while (10 < 0) { | | |
| 5148 | \\ unreachable; | | |
| 5149 | \\ } | | |
| 5150 | \\ | | |
| 5151 | \\ const res = while (0 < 10) { | | |
| 5152 | \\ break 7; | | |
| 5153 | \\ } else { | | |
| 5154 | \\ unreachable; | | |
| 5155 | \\ }; | | |
| 5156 | \\ | | |
| 5157 | \\ const res = while (0 < 10) | | |
| 5158 | \\ break 7 | | |
| 5159 | \\ else | | |
| 5160 | \\ unreachable; | | |
| 5161 | \\ | | |
| 5162 | \\ var a: error!u8 = 0; | | |
| 5163 | \\ while (a) |v| { | | |
| 5164 | \\ a = error.Err; | | |
| 5165 | \\ } else |err| { | | |
| 5166 | \\ i = 1; | | |
| 5167 | \\ } | | |
| 5168 | \\ | | |
| 5169 | \\ comptime var k: usize = 0; | | |
| 5170 | \\ inline while (i < 10) : (i += 1) | | |
| 5171 | \\ j += 2; | | |
| 5172 | \\} | | |
| 5173 | \\ | | |
| 5174 | ); | | |
| 5175 | } | | |
| 5176 | | | |
| 5177 | test "zig fmt: for" { | | |
| 5178 | try testCanonical( | | |
| 5179 | \\test "for" { | | |
| 5180 | \\ const a = []u8 { | | |
| 5181 | \\ 1, | | |
| 5182 | \\ 2, | | |
| 5183 | \\ 3, | | |
| 5184 | \\ }; | | |
| 5185 | \\ for (a) |v| { | | |
| 5186 | \\ continue; | | |
| 5187 | \\ } | | |
| 5188 | \\ | | |
| 5189 | \\ for (a) |v| | | |
| 5190 | \\ continue; | | |
| 5191 | \\ | | |
| 5192 | \\ for (a) |*v| | | |
| 5193 | \\ continue; | | |
| 5194 | \\ | | |
| 5195 | \\ for (a) |v, i| { | | |
| 5196 | \\ continue; | | |
| 5197 | \\ } | | |
| 5198 | \\ | | |
| 5199 | \\ for (a) |v, i| | | |
| 5200 | \\ continue; | | |
| 5201 | \\ | | |
| 5202 | \\ const res = for (a) |v, i| { | | |
| 5203 | \\ break v; | | |
| 5204 | \\ } else { | | |
| 5205 | \\ unreachable; | | |
| 5206 | \\ }; | | |
| 5207 | \\ | | |
| 5208 | \\ var num: usize = 0; | | |
| 5209 | \\ inline for (a) |v, i| { | | |
| 5210 | \\ num += v; | | |
| 5211 | \\ num += i; | | |
| 5212 | \\ } | | |
| 5213 | \\} | | |
| 5214 | \\ | | |
| 5215 | ); | | |
| 5216 | } | | |
| 5217 | | | |
| 5218 | test "zig fmt: if" { | | |
| 5219 | try testCanonical( | | |
| 5220 | \\test "if" { | | |
| 5221 | \\ if (10 < 0) { | | |
| 5222 | \\ unreachable; | | |
| 5223 | \\ } | | |
| 5224 | \\ | | |
| 5225 | \\ if (10 < 0) unreachable; | | |
| 5226 | \\ | | |
| 5227 | \\ if (10 < 0) { | | |
| 5228 | \\ unreachable; | | |
| 5229 | \\ } else { | | |
| 5230 | \\ const a = 20; | | |
| 5231 | \\ } | | |
| 5232 | \\ | | |
| 5233 | \\ if (10 < 0) { | | |
| 5234 | \\ unreachable; | | |
| 5235 | \\ } else if (5 < 0) { | | |
| 5236 | \\ unreachable; | | |
| 5237 | \\ } else { | | |
| 5238 | \\ const a = 20; | | |
| 5239 | \\ } | | |
| 5240 | \\ | | |
| 5241 | \\ const is_world_broken = if (10 < 0) true else false; | | |
| 5242 | \\ const some_number = 1 + if (10 < 0) 2 else 3; | | |
| 5243 | \\ | | |
| 5244 | \\ const a: ?u8 = 10; | | |
| 5245 | \\ const b: ?u8 = null; | | |
| 5246 | \\ if (a) |v| { | | |
| 5247 | \\ const some = v; | | |
| 5248 | \\ } else if (b) |*v| { | | |
| 5249 | \\ unreachable; | | |
| 5250 | \\ } else { | | |
| 5251 | \\ const some = 10; | | |
| 5252 | \\ } | | |
| 5253 | \\ | | |
| 5254 | \\ const non_null_a = if (a) |v| v else 0; | | |
| 5255 | \\ | | |
| 5256 | \\ const a_err: error!u8 = 0; | | |
| 5257 | \\ if (a_err) |v| { | | |
| 5258 | \\ const p = v; | | |
| 5259 | \\ } else |err| { | | |
| 5260 | \\ unreachable; | | |
| 5261 | \\ } | | |
| 5262 | \\} | | |
| 5263 | \\ | | |
| 5264 | ); | | |
| 5265 | } | | |
| 5266 | | | |
| 5267 | test "zig fmt: defer" { | | |
| 5268 | try testCanonical( | | |
| 5269 | \\test "defer" { | | |
| 5270 | \\ var i: usize = 0; | | |
| 5271 | \\ defer i = 1; | | |
| 5272 | \\ defer { | | |
| 5273 | \\ i += 2; | | |
| 5274 | \\ i *= i; | | |
| 5275 | \\ } | | |
| 5276 | \\ | | |
| 5277 | \\ errdefer i += 3; | | |
| 5278 | \\ errdefer { | | |
| 5279 | \\ i += 2; | | |
| 5280 | \\ i /= i; | | |
| 5281 | \\ } | | |
| 5282 | \\} | | |
| 5283 | \\ | | |
| 5284 | ); | | |
| 5285 | } | | |
| 5286 | | | |
| 5287 | test "zig fmt: comptime" { | | |
| 5288 | try testCanonical( | | |
| 5289 | \\fn a() u8 { | | |
| 5290 | \\ return 5; | | |
| 5291 | \\} | | |
| 5292 | \\ | | |
| 5293 | \\fn b(comptime i: u8) u8 { | | |
| 5294 | \\ return i; | | |
| 5295 | \\} | | |
| 5296 | \\ | | |
| 5297 | \\const av = comptime a(); | | |
| 5298 | \\const av2 = comptime blk: { | | |
| 5299 | \\ var res = a(); | | |
| 5300 | \\ res *= b(2); | | |
| 5301 | \\ break :blk res; | | |
| 5302 | \\}; | | |
| 5303 | \\ | | |
| 5304 | \\comptime { | | |
| 5305 | \\ _ = a(); | | |
| 5306 | \\} | | |
| 5307 | \\ | | |
| 5308 | \\test "comptime" { | | |
| 5309 | \\ const av3 = comptime a(); | | |
| 5310 | \\ const av4 = comptime blk: { | | |
| 5311 | \\ var res = a(); | | |
| 5312 | \\ res *= a(); | | |
| 5313 | \\ break :blk res; | | |
| 5314 | \\ }; | | |
| 5315 | \\ | | |
| 5316 | \\ comptime var i = 0; | | |
| 5317 | \\ comptime { | | |
| 5318 | \\ i = a(); | | |
| 5319 | \\ i += b(i); | | |
| 5320 | \\ } | | |
| 5321 | \\} | | |
| 5322 | \\ | | |
| 5323 | ); | | |
| 5324 | } | | |
| 5325 | | | |
| 5326 | test "zig fmt: fn type" { | | |
| 5327 | try testCanonical( | | |
| 5328 | \\fn a(i: u8) u8 { | | |
| 5329 | \\ return i + 1; | | |
| 5330 | \\} | | |
| 5331 | \\ | | |
| 5332 | \\const a: fn(u8) u8 = undefined; | | |
| 5333 | \\const b: extern fn(u8) u8 = undefined; | | |
| 5334 | \\const c: nakedcc fn(u8) u8 = undefined; | | |
| 5335 | \\const ap: fn(u8) u8 = a; | | |
| 5336 | \\ | | |
| 5337 | ); | | |
| 5338 | } | | |
| 5339 | | | |
| 5340 | test "zig fmt: inline asm" { | | |
| 5341 | try testCanonical( | | |
| 5342 | \\pub fn syscall1(number: usize, arg1: usize) usize { | | |
| 5343 | \\ return asm volatile ("syscall" | | |
| 5344 | \\ : [ret] "={rax}" (-> usize) | | |
| 5345 | \\ : [number] "{rax}" (number), | | |
| 5346 | \\ [arg1] "{rdi}" (arg1) | | |
| 5347 | \\ : "rcx", "r11"); | | |
| 5348 | \\} | | |
| 5349 | \\ | | |
| 5350 | ); | | |
| 5351 | } | | |
| 5352 | | | |
| 5353 | test "zig fmt: coroutines" { | | |
| 5354 | try testCanonical( | | |
| 5355 | \\async fn simpleAsyncFn() void { | | |
| 5356 | \\ const a = async a.b(); | | |
| 5357 | \\ x += 1; | | |
| 5358 | \\ suspend; | | |
| 5359 | \\ x += 1; | | |
| 5360 | \\ suspend |p| {} | | |
| 5361 | \\ const p = async simpleAsyncFn() catch unreachable; | | |
| 5362 | \\ await p; | | |
| 5363 | \\} | | |
| 5364 | \\ | | |
| 5365 | \\test "coroutine suspend, resume, cancel" { | | |
| 5366 | \\ const p = try async<std.debug.global_allocator> testAsyncSeq(); | | |
| 5367 | \\ resume p; | | |
| 5368 | \\ cancel p; | | |
| 5369 | \\} | | |
| 5370 | \\ | | |
| 5371 | ); | | |
| 5372 | } | | |
| 5373 | | | |
| 5374 | test "zig fmt: Block after if" { | | |
| 5375 | try testCanonical( | | |
| 5376 | \\test "Block after if" { | | |
| 5377 | \\ if (true) { | | |
| 5378 | \\ const a = 0; | | |
| 5379 | \\ } | | |
| 5380 | \\ | | |
| 5381 | \\ { | | |
| 5382 | \\ const a = 0; | | |
| 5383 | \\ } | | |
| 5384 | \\} | | |
| 5385 | \\ | | |
| 5386 | ); | | |
| 5387 | } | | |
| 5388 | | | |
| 5389 | test "zig fmt: use" { | | |
| 5390 | try testCanonical( | | |
| 5391 | \\use @import("std"); | | |
| 5392 | \\pub use @import("std"); | | |
| 5393 | \\ | | |
| 5394 | ); | | |
| 5395 | } | | |
| 5396 | | | |
| 5397 | test "zig fmt: string identifier" { | | |
| 5398 | try testCanonical( | | |
| 5399 | \\const @"a b" = @"c d".@"e f"; | | |
| 5400 | \\fn @"g h"() void {} | | |
| 5401 | \\ | | |
| 5402 | ); | | |
| 5403 | } | | |
| 5404 | | | |
| 5405 | test "zig fmt: error return" { | | |
| 5406 | try testCanonical( | | |
| 5407 | \\fn err() error { | | |
| 5408 | \\ call(); | | |
| 5409 | \\ return error.InvalidArgs; | | |
| 5410 | \\} | | |
| 5411 | \\ | | |
| 5412 | ); | | |
| 5413 | } | | |
| 5414 | | | |
| 5415 | test "zig fmt: struct literals with fields on each line" { | | |
| 5416 | try testCanonical( | | |
| 5417 | \\var self = BufSet { | | |
| 5418 | \\ .hash_map = BufSetHashMap.init(a), | | |
| 5419 | \\}; | | |
| 5420 | \\ | | |
| 5421 | ); | | |
| 5422 | } | 4481 | } |