zig-fmt: allow comptime blocks in containers (#2308)
* zig-fmt: allow comptime blocks in containers * add test for comptime block in container
This commit is contained in:
parent
3b6a4fe4cd
commit
d44d2784e6
@ -629,6 +629,35 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
|
|||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
|
Token.Id.Keyword_comptime => {
|
||||||
|
const block = try arena.create(ast.Node.Block);
|
||||||
|
block.* = ast.Node.Block{
|
||||||
|
.base = ast.Node{ .id = ast.Node.Id.Block },
|
||||||
|
.label = null,
|
||||||
|
.lbrace = undefined,
|
||||||
|
.statements = ast.Node.Block.StatementList.init(arena),
|
||||||
|
.rbrace = undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const node = try arena.create(ast.Node.Comptime);
|
||||||
|
node.* = ast.Node.Comptime{
|
||||||
|
.base = ast.Node{ .id = ast.Node.Id.Comptime },
|
||||||
|
.comptime_token = token_index,
|
||||||
|
.expr = &block.base,
|
||||||
|
.doc_comments = comments,
|
||||||
|
};
|
||||||
|
try container_decl.fields_and_decls.push(&node.base);
|
||||||
|
|
||||||
|
stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
|
||||||
|
try stack.append(State{ .Block = block });
|
||||||
|
try stack.append(State{
|
||||||
|
.ExpectTokenSave = ExpectTokenSave{
|
||||||
|
.id = Token.Id.LBrace,
|
||||||
|
.ptr = &block.lbrace,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
},
|
||||||
Token.Id.RBrace => {
|
Token.Id.RBrace => {
|
||||||
if (comments != null) {
|
if (comments != null) {
|
||||||
((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };
|
((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };
|
||||||
|
@ -2118,6 +2118,21 @@ test "zig fmt: error return" {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "zig fmt: comptime block in container" {
|
||||||
|
try testCanonical(
|
||||||
|
\\pub fn container() type {
|
||||||
|
\\ return struct {
|
||||||
|
\\ comptime {
|
||||||
|
\\ if (false) {
|
||||||
|
\\ unreachable;
|
||||||
|
\\ }
|
||||||
|
\\ }
|
||||||
|
\\ };
|
||||||
|
\\}
|
||||||
|
\\
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
const warn = std.debug.warn;
|
const warn = std.debug.warn;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user