Commit 2f54129087

Veikka Tuominen <git@vexu.eu>
2022-07-25 17:11:59
parser: add error for doc comment attached to comptime or test blocks
1 parent 825fc65
Changed files (3)
lib/std/zig/Ast.zig
@@ -297,6 +297,12 @@ pub fn renderError(tree: Ast, parse_error: Error, stream: anytype) !void {
         .unattached_doc_comment => {
             return stream.writeAll("unattached documentation comment");
         },
+        .test_doc_comment => {
+            return stream.writeAll("documentation comments cannot be attached to tests");
+        },
+        .comptime_doc_comment => {
+            return stream.writeAll("documentation comments cannot be attached to comptime blocks");
+        },
         .varargs_nonfinal => {
             return stream.writeAll("function prototype has parameter after varargs");
         },
@@ -2539,6 +2545,8 @@ pub const Error = struct {
         invalid_bit_range,
         same_line_doc_comment,
         unattached_doc_comment,
+        test_doc_comment,
+        comptime_doc_comment,
         varargs_nonfinal,
         expected_continue_expr,
         expected_semi_after_decl,
lib/std/zig/parse.zig
@@ -259,6 +259,9 @@ const Parser = struct {
 
             switch (p.token_tags[p.tok_i]) {
                 .keyword_test => {
+                    if (doc_comment) |some| {
+                        try p.warnMsg(.{ .tag = .test_doc_comment, .token = some });
+                    }
                     const test_decl_node = try p.expectTestDeclRecoverable();
                     if (test_decl_node != 0) {
                         if (field_state == .seen) {
@@ -317,6 +320,9 @@ const Parser = struct {
                         }
                     },
                     .l_brace => {
+                        if (doc_comment) |some| {
+                            try p.warnMsg(.{ .tag = .test_doc_comment, .token = some });
+                        }
                         const comptime_token = p.nextToken();
                         const block = p.parseBlock() catch |err| switch (err) {
                             error.OutOfMemory => return error.OutOfMemory,
lib/std/zig/parser_test.zig
@@ -184,15 +184,6 @@ test "zig fmt: file ends in comment after var decl" {
     );
 }
 
-test "zig fmt: doc comments on test" {
-    try testCanonical(
-        \\/// hello
-        \\/// world
-        \\test "" {}
-        \\
-    );
-}
-
 test "zig fmt: if statment" {
     try testCanonical(
         \\test "" {
@@ -2700,9 +2691,6 @@ test "zig fmt: comments in statements" {
 
 test "zig fmt: comments before test decl" {
     try testCanonical(
-        \\/// top level doc comment
-        \\test "hi" {}
-        \\
         \\// top level normal comment
         \\test "hi" {}
         \\