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
56 changes: 54 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,21 @@ impl<'a> Parser<'a> {
)];
}

if self.eat_keyword(Keyword::Fn) {
let is_function = if self.eat_keyword(Keyword::Fn) {
true
} else if !modifiers.is_empty()
&& matches!(self.token.token(), Token::Ident(..))
&& self.next_is(Token::LeftParen)
{
// If it's something like `pub foo(` then it's likely the user forgot to put `fn` after `pub`,
// so we error but keep parsing what comes next as a function.
self.expected_token(Token::Keyword(Keyword::Fn));
true
} else {
false
};

if is_function {
self.mutable_not_applicable(modifiers);

return vec![ItemKind::Function(self.parse_function(
Expand Down Expand Up @@ -250,7 +264,7 @@ mod tests {
use crate::{
parse_program_with_dummy_file,
parser::{
ItemKind,
ItemKind, Parser,
parser::tests::{get_single_error, get_source_with_error_span},
},
};
Expand Down Expand Up @@ -323,4 +337,42 @@ mod tests {
assert_eq!(attributes[0].to_string(), "#[one]");
assert_eq!(attributes[1].to_string(), "#[two]");
}

#[test]
fn error_recovery_for_missing_fn_between_visibility_and_name() {
let src = "
pub foo() { }
^^^
";
let (src, span) = get_source_with_error_span(src);
let mut parser = Parser::for_str_with_dummy_file(&src);
let module = parser.parse_program();
assert_eq!(module.items.len(), 1);
let ItemKind::Function(noir_function) = &module.items[0].kind else {
panic!("Expected function");
};
assert_eq!(noir_function.name(), "foo");

let reason = get_single_error(&parser.errors, span);
assert_eq!(reason.to_string(), "Expected a 'fn' but found 'foo'");
}

#[test]
fn error_recovery_for_missing_fn_between_unconstrained_and_name() {
let src = "
unconstrained foo() { }
^^^
";
let (src, span) = get_source_with_error_span(src);
let mut parser = Parser::for_str_with_dummy_file(&src);
let module = parser.parse_program();
assert_eq!(module.items.len(), 1);
let ItemKind::Function(noir_function) = &module.items[0].kind else {
panic!("Expected function");
};
assert_eq!(noir_function.name(), "foo");

let reason = get_single_error(&parser.errors, span);
assert_eq!(reason.to_string(), "Expected a 'fn' but found 'foo'");
}
}
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ pub(crate) struct Modifiers {
pub(crate) mutable: Option<Location>,
}

impl Modifiers {
pub(crate) fn is_empty(&self) -> bool {
self.visibility == ItemVisibility::Private
&& self.unconstrained.is_none()
&& self.comptime.is_none()
&& self.mutable.is_none()
}
}

impl Parser<'_> {
/// Modifiers = ItemVisibility 'unconstrained'? 'comptime'? 'mut'?
///
Expand Down
Loading