Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix a few cases where safety comment wasn't correctly identified #7548

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ impl<'a> Parser<'a> {
match self.tokens.next() {
Some(Ok(token)) => match token.token() {
Token::LineComment(comment, None) | Token::BlockComment(comment, None) => {
if !last_comments.is_empty() {
last_comments.push('\n');
}
last_comments.push_str(comment);
continue;
}
Expand Down
21 changes: 13 additions & 8 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,23 @@
/// UnsafeExpression = 'unsafe' Block
fn parse_unsafe_expr(&mut self) -> Option<ExpressionKind> {
let start_location = self.current_token_location;
let comments_before_unsafe = self.current_token_comments.clone();

if !self.eat_keyword(Keyword::Unsafe) {
return None;
}

if self.current_token_comments.is_empty() {
if let Some(statement_comments) = &mut self.statement_comments {
if !statement_comments.trim().to_lowercase().starts_with("safety:") {
self.push_error(ParserErrorReason::MissingSafetyComment, start_location);
}
let comments: &str = if comments_before_unsafe.is_empty() {
if let Some(statement_comments) = &self.statement_comments {
statement_comments
} else {
self.push_error(ParserErrorReason::MissingSafetyComment, start_location);
""
}
} else if !self.current_token_comments.trim().to_lowercase().starts_with("safety:") {
} else {
&comments_before_unsafe
};

if !comments.lines().any(|line| line.trim().to_lowercase().starts_with("safety:")) {
self.push_error(ParserErrorReason::MissingSafetyComment, start_location);
}

Expand Down Expand Up @@ -626,7 +629,7 @@
/// = bool
/// | int
/// | str
/// | rawstr

Check warning on line 632 in compiler/noirc_frontend/src/parser/parser/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (rawstr)
/// | fmtstr
/// | QuoteExpression
/// | ArrayExpression
Expand Down Expand Up @@ -1079,7 +1082,9 @@
let src = "
// Safety: test
unsafe { 1 }";
let expr = parse_expression_no_errors(src);
let mut parser = Parser::for_str_with_dummy_file(src);
let expr = parser.parse_expression_or_error();
assert!(parser.errors.is_empty());
let ExpressionKind::Unsafe(unsafe_expression) = expr.kind else {
panic!("Expected unsafe expression");
};
Expand Down
18 changes: 17 additions & 1 deletion compiler/noirc_frontend/src/parser/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ mod tests {
fn parses_let_statement_with_unsafe() {
let src = "// Safety: comment
let x = unsafe { 1 };";
let statement = parse_statement_no_errors(src);
let mut parser = Parser::for_str_with_dummy_file(src);
let statement = parser.parse_statement_or_error();
assert!(parser.errors.is_empty());
let StatementKind::Let(let_statement) = statement.kind else {
panic!("Expected let statement");
};
Expand All @@ -540,6 +542,20 @@ mod tests {
assert_eq!(let_statement.pattern.to_string(), "x");
}

#[test]
fn parses_let_statement_with_unsafe_after_some_other_comment() {
let src = "// Top comment
// Safety: comment
let x = unsafe { 1 };";
let mut parser = Parser::for_str_with_dummy_file(src);
let statement = parser.parse_statement_or_error();
assert!(parser.errors.is_empty());
let StatementKind::Let(let_statement) = statement.kind else {
panic!("Expected let statement");
};
assert_eq!(let_statement.pattern.to_string(), "x");
}

#[test]
fn parses_comptime_block() {
let src = "comptime { 1 }";
Expand Down
Loading