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: better error message when keyword is found instead of type in p… #7501

Merged
merged 2 commits into from
Feb 24, 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
40 changes: 38 additions & 2 deletions compiler/noirc_frontend/src/parser/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ impl<'a> Parser<'a> {
UnresolvedType { typ: UnresolvedTypeData::Error, location: Location::dummy() };
(visibility, typ)
} else {
(self.parse_visibility(), self.parse_type_or_error())
(
self.parse_visibility(),
self.parse_type_or_error_with_recovery(&[Token::Comma, Token::RightParen]),
)
};

Param { visibility, pattern, typ, location: self.location_since(start_location) }
Expand Down Expand Up @@ -306,7 +309,10 @@ fn empty_body() -> BlockExpression {
#[cfg(test)]
mod tests {
use crate::{
ast::{ItemVisibility, NoirFunction, UnresolvedTypeData, Visibility},
ast::{
IntegerBitSize, ItemVisibility, NoirFunction, Signedness, UnresolvedTypeData,
Visibility,
},
parse_program_with_dummy_file,
parser::{
parser::tests::{
Expand Down Expand Up @@ -520,4 +526,34 @@ mod tests {
let reason = get_single_error_reason(&errors, span);
assert!(matches!(reason, ParserErrorReason::MissingParametersForFunctionDefinition));
}

#[test]
fn parse_function_with_keyword_before_type() {
let src = "
fn foo(x: mut i32, y: i64) {}
^^^
";
let (src, span) = get_source_with_error_span(src);
let (mut module, errors) = parse_program_with_dummy_file(&src);
let error = get_single_error(&errors, span);
assert_eq!(error.to_string(), "Expected a type but found 'mut'");

assert_eq!(module.items.len(), 1);
let item = module.items.remove(0);
let ItemKind::Function(noir_function) = item.kind else {
panic!("Expected function");
};

let params = noir_function.parameters();
assert_eq!(params.len(), 2);

assert_eq!(
params[0].typ.typ,
UnresolvedTypeData::Integer(Signedness::Signed, IntegerBitSize::ThirtyTwo)
);
assert_eq!(
params[1].typ.typ,
UnresolvedTypeData::Integer(Signedness::Signed, IntegerBitSize::SixtyFour)
);
}
}
20 changes: 20 additions & 0 deletions compiler/noirc_frontend/src/parser/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ impl<'a> Parser<'a> {
}
}

/// Tries to parse a type. If the current token doesn't denote a type and it's not
/// one of `stop_tokens`, try to parse a type starting from the next token (and so on).
pub(crate) fn parse_type_or_error_with_recovery(
&mut self,
stop_tokens: &[Token],
) -> UnresolvedType {
loop {
let typ = self.parse_type_or_error();
if typ.typ != UnresolvedTypeData::Error {
return typ;
}

if self.at_eof() || stop_tokens.contains(self.token.token()) {
return typ;
}

self.bump();
}
}

pub(crate) fn parse_type(&mut self) -> Option<UnresolvedType> {
let start_location = self.current_token_location;
let typ = self.parse_unresolved_type_data()?;
Expand Down
Loading