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: Be more lenient with semicolons on interned expressions #6062

Merged
merged 2 commits into from
Sep 17, 2024
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
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ impl StatementKind {
// Semicolons are optional for these expressions
(ExpressionKind::Block(_), semi, _)
| (ExpressionKind::Unsafe(..), semi, _)
| (ExpressionKind::Interned(..), semi, _)
| (ExpressionKind::InternedStatement(..), semi, _)
| (ExpressionKind::If(_), semi, _) => {
if semi.is_some() {
StatementKind::Semi(expr)
Expand Down Expand Up @@ -865,7 +867,11 @@ impl Display for StatementKind {

impl Display for LetStatement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "let {}: {} = {}", self.pattern, self.r#type, self.expression)
if matches!(&self.r#type.typ, UnresolvedTypeData::Unspecified) {
write!(f, "let {} = {}", self.pattern, self.expression)
} else {
write!(f, "let {}: {} = {}", self.pattern, self.r#type, self.expression)
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,11 +1621,11 @@ mod test {
#[test]
fn statement_recovery() {
let cases = vec![
Case { source: "let a = 4 + 3", expect: "let a: unspecified = (4 + 3)", errors: 0 },
Case { source: "let a = 4 + 3", expect: "let a = (4 + 3)", errors: 0 },
Case { source: "let a: = 4 + 3", expect: "let a: error = (4 + 3)", errors: 1 },
Case { source: "let = 4 + 3", expect: "let $error: unspecified = (4 + 3)", errors: 1 },
Case { source: "let = ", expect: "let $error: unspecified = Error", errors: 2 },
Case { source: "let", expect: "let $error: unspecified = Error", errors: 3 },
Case { source: "let = 4 + 3", expect: "let $error = (4 + 3)", errors: 1 },
Case { source: "let = ", expect: "let $error = Error", errors: 2 },
Case { source: "let", expect: "let $error = Error", errors: 3 },
Case { source: "foo = one two three", expect: "foo = one", errors: 1 },
Case { source: "constrain", expect: "constrain Error", errors: 2 },
Case { source: "assert", expect: "constrain Error", errors: 1 },
Expand Down Expand Up @@ -1659,7 +1659,7 @@ mod test {
},
Case {
source: "{ return 123; let foo = 4 + 3; }",
expect: concat!("{\n", " Error\n", " let foo: unspecified = (4 + 3)\n", "}"),
expect: concat!("{\n", " Error\n", " let foo = (4 + 3)\n", "}"),
errors: 1,
},
Case {
Expand Down Expand Up @@ -1709,7 +1709,7 @@ mod test {
expect: concat!(
"{\n",
" if ({\n",
" let foo: unspecified = (bar { baz: 42 })\n",
" let foo = (bar { baz: 42 })\n",
" (foo == (bar { baz: 42 }))\n",
" }) {\n",
" }\n",
Expand Down
Loading