From a25212331463596a61d7d2922ae467c226e40ea8 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 5 Feb 2025 15:12:59 -0300 Subject: [PATCH] fix: error on trailing doc comment (#7300) --- .../noirc_frontend/src/parser/parser/item.rs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/parser/parser/item.rs b/compiler/noirc_frontend/src/parser/parser/item.rs index d928d8e82d3..43641935565 100644 --- a/compiler/noirc_frontend/src/parser/parser/item.rs +++ b/compiler/noirc_frontend/src/parser/parser/item.rs @@ -1,7 +1,7 @@ use iter_extended::vecmap; use crate::{ - parser::{labels::ParsingRuleLabel, Item, ItemKind}, + parser::{labels::ParsingRuleLabel, Item, ItemKind, ParserErrorReason}, token::{Keyword, Token}, }; @@ -94,6 +94,10 @@ impl<'a> Parser<'a> { let kinds = self.parse_item_kind(); let span = self.span_since(start_span); + if kinds.is_empty() && !doc_comments.is_empty() { + self.push_error(ParserErrorReason::DocCommentDoesNotDocumentAnything, start_span); + } + vecmap(kinds, |kind| Item { kind, span, doc_comments: doc_comments.clone() }) } @@ -260,4 +264,18 @@ mod tests { let error = get_single_error(&errors, span); assert_eq!(error.to_string(), "Expected a '}' but found end of input"); } + + #[test] + fn errors_on_trailing_doc_comment() { + let src = " + fn foo() {} + /// doc comment + ^^^^^^^^^^^^^^^ + "; + let (src, span) = get_source_with_error_span(src); + let (module, errors) = parse_program(&src); + assert_eq!(module.items.len(), 1); + let error = get_single_error(&errors, span); + assert!(error.to_string().contains("Documentation comment does not document anything")); + } }