Skip to content

Commit

Permalink
chore: improve error message of &T (#6633)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Nov 27, 2024
1 parent 1b910bc commit 6167b73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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 @@ impl<'a> Parser<'a> {

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

0 comments on commit 6167b73

Please sign in to comment.