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

feat: improve parser recovery of constructor field with '::' instead of ':' #6701

Merged
Merged
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
32 changes: 30 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@
Some(if self.eat_colon() {
let expression = self.parse_expression_or_error();
(ident, expression)
} else if self.at(Token::Assign) {
// If we find '=' instead of ':', assume the user meant ':`, error and continue
} else if self.at(Token::DoubleColon) || self.at(Token::Assign) {
// If we find '=' or '::' instead of ':', assume the user meant ':`, error and continue
self.expected_token(Token::Colon);
self.bump();
let expression = self.parse_expression_or_error();
Expand Down Expand Up @@ -563,7 +563,7 @@
/// = bool
/// | int
/// | str
/// | rawstr

Check warning on line 566 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 @@ -1369,6 +1369,34 @@
assert_eq!(expr.to_string(), "y");
}

#[test]
fn parses_constructor_recovers_if_double_colon_instead_of_colon() {
let src = "
Foo { x: 1, y:: z }
^^
";
let (src, span) = get_source_with_error_span(src);
let mut parser = Parser::for_str(&src);
let expr = parser.parse_expression_or_error();

let error = get_single_error(&parser.errors, span);
assert_eq!(error.to_string(), "Expected a ':' but found '::'");

let ExpressionKind::Constructor(mut constructor) = expr.kind else {
panic!("Expected constructor");
};
assert_eq!(constructor.typ.to_string(), "Foo");
assert_eq!(constructor.fields.len(), 2);

let (name, expr) = constructor.fields.remove(0);
assert_eq!(name.to_string(), "x");
assert_eq!(expr.to_string(), "1");

let (name, expr) = constructor.fields.remove(0);
assert_eq!(name.to_string(), "y");
assert_eq!(expr.to_string(), "z");
}

#[test]
fn parses_parses_if_true() {
let src = "if true { 1 }";
Expand Down
Loading