Skip to content
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
15 changes: 11 additions & 4 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,21 @@ impl StatementKind {
ParserError::with_reason(ParserErrorReason::MissingSeparatingSemi, location);

match self {
StatementKind::Let(_)
| StatementKind::Assign(_)
StatementKind::Let(_) => {
// To match rust, a let statement always requires a semicolon, even at the end of a block
if semi.is_none() {
let reason = ParserErrorReason::MissingSemicolonAfterLet;
emit_error(ParserError::with_reason(reason, location));
}
self
}
StatementKind::Assign(_)
| StatementKind::Semi(_)
| StatementKind::Break
| StatementKind::Continue
| StatementKind::Error => {
// To match rust, statements always require a semicolon, even at the end of a block
if semi.is_none() {
// These statements can omit the semicolon if they are the last statement in a block
if !last_statement_in_block && semi.is_none() {
emit_error(missing_semicolon);
}
self
Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum ParserErrorReason {
ExpectedPatternButFoundType(Token),
#[error("Expected a ; separating these two statements")]
MissingSeparatingSemi,
#[error("Expected a ; after `let` statement")]
MissingSemicolonAfterLet,
#[error("constrain keyword is deprecated")]
ConstrainDeprecated,
#[error(
Expand Down
33 changes: 33 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@
/// = bool
/// | int
/// | str
/// | rawstr

Check warning on line 655 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 @@ -1100,6 +1100,39 @@
assert_eq!(block.statements.len(), 3);
}

#[test]
fn parses_block_expression_with_a_single_assignment() {
let src = "{ x = 1 }";
let _ = parse_expression_no_errors(src);
}

#[test]
fn parses_block_expression_with_a_single_break() {
let src = "{ break }";
let _ = parse_expression_no_errors(src);
}

#[test]
fn parses_block_expression_with_a_single_continue() {
let src = "{ continue }";
let _ = parse_expression_no_errors(src);
}

#[test]
fn parses_block_expression_with_a_single_let() {
let src = "
{ let x = 1 }
^
";
let (src, span) = get_source_with_error_span(src);
let mut parser = Parser::for_str_with_dummy_file(&src);
parser.parse_expression();
let reason = get_single_error_reason(&parser.errors, span);
let ParserErrorReason::MissingSemicolonAfterLet = reason else {
panic!("Expected a different error");
};
}

#[test]
fn parses_unsafe_expression() {
let src = "
Expand Down
Loading