diff --git a/crates/noirc_frontend/src/parser/errors.rs b/crates/noirc_frontend/src/parser/errors.rs index 7012c0fbda5..0295d6063d5 100644 --- a/crates/noirc_frontend/src/parser/errors.rs +++ b/crates/noirc_frontend/src/parser/errors.rs @@ -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), } @@ -96,6 +98,7 @@ impl From 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) diff --git a/crates/noirc_frontend/src/parser/parser.rs b/crates/noirc_frontend/src/parser/parser.rs index 4f7c73e609b..701d6a2f2f5 100644 --- a/crates/noirc_frontend/src/parser/parser.rs +++ b/crates/noirc_frontend/src/parser/parser.rs @@ -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), )) } @@ -463,6 +464,18 @@ where .map(|expr| Statement::Constrain(ConstrainStatement(expr))) } +fn early_return<'a, P>(expr_parser: P) -> impl NoirParser + '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 + 'a where P: ExprParser + 'a,