authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-25 18:11:59+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-26 12:14:59+03:00
log2f54129087859fbd9a437d0cee33f16df523dd0b
tree74bd3628de297ced8104e7240c20d4abbdfe0bd3
parent825fc654b6d0d232d7a46610b339d9c58871185e

parser: add error for doc comment attached to comptime or test blocks


3 files changed, 14 insertions(+), 12 deletions(-)

lib/std/zig/Ast.zig+8
......@@ -297,6 +297,12 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
297297 .unattached_doc_comment => {
298298 return stream.writeAll("unattached documentation comment");
299299 },
300 .test_doc_comment => {
301 return stream.writeAll("documentation comments cannot be attached to tests");
302 },
303 .comptime_doc_comment => {
304 return stream.writeAll("documentation comments cannot be attached to comptime blocks");
305 },
300306 .varargs_nonfinal => {
301307 return stream.writeAll("function prototype has parameter after varargs");
302308 },
......@@ -2539,6 +2545,8 @@ pub const Error = struct {
25392545 invalid_bit_range,
25402546 same_line_doc_comment,
25412547 unattached_doc_comment,
2548 test_doc_comment,
2549 comptime_doc_comment,
25422550 varargs_nonfinal,
25432551 expected_continue_expr,
25442552 expected_semi_after_decl,
lib/std/zig/parse.zig+6
......@@ -259,6 +259,9 @@ const Parser = struct {
259259
260260 switch (p.token_tags[p.tok_i]) {
261261 .keyword_test => {
262 if (doc_comment) |some| {
263 try p.warnMsg(.{ .tag = .test_doc_comment, .token = some });
264 }
262265 const test_decl_node = try p.expectTestDeclRecoverable();
263266 if (test_decl_node != 0) {
264267 if (field_state == .seen) {
......@@ -317,6 +320,9 @@ const Parser = struct {
317320 }
318321 },
319322 .l_brace => {
323 if (doc_comment) |some| {
324 try p.warnMsg(.{ .tag = .test_doc_comment, .token = some });
325 }
320326 const comptime_token = p.nextToken();
321327 const block = p.parseBlock() catch |err| switch (err) {
322328 error.OutOfMemory => return error.OutOfMemory,
lib/std/zig/parser_test.zig-12
......@@ -184,15 +184,6 @@ test "zig fmt: file ends in comment after var decl" {
184184 );
185185}
186186
187test "zig fmt: doc comments on test" {
188 try testCanonical(
189 \\/// hello
190 \\/// world
191 \\test "" {}
192 \\
193 );
194}
195
196187test "zig fmt: if statment" {
197188 try testCanonical(
198189 \\test "" {
......@@ -2700,9 +2691,6 @@ test "zig fmt: comments in statements" {
27002691
27012692test "zig fmt: comments before test decl" {
27022693 try testCanonical(
2703 \\/// top level doc comment
2704 \\test "hi" {}
2705 \\
27062694 \\// top level normal comment
27072695 \\test "hi" {}
27082696 \\