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(parser): return statements unsupported #1297

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions crates/noirc_frontend/src/parser/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum ParserErrorReason {
MissingSeparatingSemi,
#[error("constrain keyword is deprecated")]
ConstrainDeprecated,
#[error("early return unsupported")]
EarlyReturnUnsupported,
#[error("Expression is invalid in an array-length type: '{0}'. Only unsigned integer constants, globals, generics, +, -, *, /, and % may be used in this context.")]
InvalidArrayLengthExpression(Expression),
}
Expand Down Expand Up @@ -96,6 +98,7 @@ impl From<ParserError> for Diagnostic {
"The 'constrain' keyword has been deprecated. Please use the 'assert' function instead.".into(),
error.span,
),
ParserErrorReason::EarlyReturnUnsupported =>Diagnostic::simple_error("early return unsupported".into(), "Noir doesn't support return statements. The return value of a function is instead inferred from the function body's final expression.".into(), error.span),
other => {

Diagnostic::simple_error(format!("{other}"), String::new(), error.span)
Expand Down
13 changes: 13 additions & 0 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ where
assertion(expr_parser.clone()),
declaration(expr_parser.clone()),
assignment(expr_parser.clone()),
early_return(expr_parser.clone()),
expr_parser.map(Statement::Expression),
))
}
Expand All @@ -463,6 +464,18 @@ where
.map(|expr| Statement::Constrain(ConstrainStatement(expr)))
}

fn early_return<'a, P>(expr_parser: P) -> impl NoirParser<Statement> + 'a
where
P: ExprParser + 'a,
{
ignore_then_commit(keyword(Keyword::Return), expr_parser).labelled("statement").validate(
|_, span, emit| {
emit(ParserError::with_reason(ParserErrorReason::EarlyReturnUnsupported, span));
Statement::Error
},
)
}

fn declaration<'a, P>(expr_parser: P) -> impl NoirParser<Statement> + 'a
where
P: ExprParser + 'a,
Expand Down