authorgravatar for raulgrell@gmail.comRaul Leal <raulgrell@gmail.com> 2019-04-19 20:27:42+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-19 15:27:42-04:00
logd44d2784e6a41ccd5549d4badea527a35169945d
tree4394555ae44541c59eec47b7b963852c714d48f4
parent3b6a4fe4cd0800b7078953d56156475f699751f1

zig-fmt: allow comptime blocks in containers (#2308)

* zig-fmt: allow comptime blocks in containers * add test for comptime block in container

2 files changed, 44 insertions(+), 0 deletions(-)

std/zig/parse.zig+29
...@@ -629,6 +629,35 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -629,6 +629,35 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
629 });629 });
630 continue;630 continue;
631 },631 },
632 Token.Id.Keyword_comptime => {
633 const block = try arena.create(ast.Node.Block);
634 block.* = ast.Node.Block{
635 .base = ast.Node{ .id = ast.Node.Id.Block },
636 .label = null,
637 .lbrace = undefined,
638 .statements = ast.Node.Block.StatementList.init(arena),
639 .rbrace = undefined,
640 };
641
642 const node = try arena.create(ast.Node.Comptime);
643 node.* = ast.Node.Comptime{
644 .base = ast.Node{ .id = ast.Node.Id.Comptime },
645 .comptime_token = token_index,
646 .expr = &block.base,
647 .doc_comments = comments,
648 };
649 try container_decl.fields_and_decls.push(&node.base);
650
651 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
652 try stack.append(State{ .Block = block });
653 try stack.append(State{
654 .ExpectTokenSave = ExpectTokenSave{
655 .id = Token.Id.LBrace,
656 .ptr = &block.lbrace,
657 },
658 });
659 continue;
660 },
632 Token.Id.RBrace => {661 Token.Id.RBrace => {
633 if (comments != null) {662 if (comments != null) {
634 ((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };663 ((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };
std/zig/parser_test.zig+15
...@@ -2118,6 +2118,21 @@ test "zig fmt: error return" {...@@ -2118,6 +2118,21 @@ test "zig fmt: error return" {
2118 );2118 );
2119}2119}
21202120
2121test "zig fmt: comptime block in container" {
2122 try testCanonical(
2123 \\pub fn container() type {
2124 \\ return struct {
2125 \\ comptime {
2126 \\ if (false) {
2127 \\ unreachable;
2128 \\ }
2129 \\ }
2130 \\ };
2131 \\}
2132 \\
2133 );
2134}
2135
2121const std = @import("std");2136const std = @import("std");
2122const mem = std.mem;2137const mem = std.mem;
2123const warn = std.debug.warn;2138const warn = std.debug.warn;