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

chore: improve error message of &T #6633

Merged
merged 1 commit into from
Nov 27, 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
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum ParserErrorReason {
UnexpectedComma,
#[error("Expected a `{token}` separating these two {items}")]
ExpectedTokenSeparatingTwoItems { token: Token, items: &'static str },
#[error("Expected `mut` after `&`, found `{found}`")]
ExpectedMutAfterAmpersand { found: Token },
#[error("Invalid left-hand side of assignment")]
InvalidLeftHandSideOfAssignment,
#[error("Expected trait, found {found}")]
Expand Down Expand Up @@ -265,6 +267,11 @@ impl<'a> From<&'a ParserError> for Diagnostic {
error.span,
),
ParserErrorReason::Lexer(error) => error.into(),
ParserErrorReason::ExpectedMutAfterAmpersand { found } => Diagnostic::simple_error(
format!("Expected `mut` after `&`, found `{found}`"),
"Noir doesn't have immutable references, only mutable references".to_string(),
error.span,
),
other => Diagnostic::simple_error(format!("{other}"), String::new(), error.span),
},
None => {
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,13 @@ impl<'a> Parser<'a> {
self.push_error(ParserErrorReason::ExpectedTokenSeparatingTwoItems { token, items }, span);
}

fn expected_mut_after_ampersand(&mut self) {
self.push_error(
ParserErrorReason::ExpectedMutAfterAmpersand { found: self.token.token().clone() },
self.current_token_span,
);
}

fn modifiers_not_followed_by_an_item(&mut self, modifiers: Modifiers) {
self.visibility_not_followed_by_an_item(modifiers);
self.unconstrained_not_followed_by_an_item(modifiers);
Expand Down
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/parser/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@

fn parses_mutable_reference_type(&mut self) -> Option<UnresolvedTypeData> {
if self.eat(Token::Ampersand) {
self.eat_keyword_or_error(Keyword::Mut);
if !self.eat_keyword(Keyword::Mut) {
self.expected_mut_after_ampersand();
}

return Some(UnresolvedTypeData::MutableReference(Box::new(
self.parse_type_or_error(),
)));
Expand Down Expand Up @@ -495,10 +498,10 @@
for quoted_type in QuotedType::iter() {
let src = quoted_type.to_string();
let typ = parse_type_no_errors(&src);
let UnresolvedTypeData::Quoted(parsed_qouted_type) = typ.typ else {

Check warning on line 501 in compiler/noirc_frontend/src/parser/parser/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (qouted)
panic!("Expected a quoted type for {}", quoted_type)
};
assert_eq!(parsed_qouted_type, quoted_type);

Check warning on line 504 in compiler/noirc_frontend/src/parser/parser/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (qouted)
}
}

Expand Down