Skip to content

Commit

Permalink
Don't parse attributes in trait let statement
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Sep 24, 2024
1 parent 4206489 commit 5db563f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::ast::{
};
use crate::lexer::Lexer;
use crate::parser::{force, ignore_then_commit, statement_recovery};
use crate::token::{Keyword, SecondaryAttribute, Token, TokenKind};
use crate::token::{Keyword, Token, TokenKind};
use acvm::AcirField;

use chumsky::prelude::*;
Expand Down Expand Up @@ -535,30 +535,27 @@ where

fn let_statement<'a, P>(
expr_parser: P,
) -> impl NoirParser<(Vec<SecondaryAttribute>, Pattern, UnresolvedType, Expression)> + 'a
) -> impl NoirParser<((Pattern, UnresolvedType), Expression)> + 'a
where
P: ExprParser + 'a,
{
let p = attributes();
let p = p.then_ignore(keyword(Keyword::Let).labelled(ParsingRuleLabel::Statement));
let p = then_commit(p, pattern());
let p =
ignore_then_commit(keyword(Keyword::Let).labelled(ParsingRuleLabel::Statement), pattern());
let p = p.then(optional_type_annotation());
let p = then_commit_ignore(p, just(Token::Assign));
let p = then_commit(p, expr_parser);
p.validate(|(((attributes, pattern), typ), expr), span, emit| {
let attributes = attributes::validate_secondary_attributes(attributes, span, emit);

(attributes, pattern, typ, expr)
})
then_commit(p, expr_parser)
}

fn declaration<'a, P>(expr_parser: P) -> impl NoirParser<StatementKind> + 'a
where
P: ExprParser + 'a,
{
let_statement(expr_parser).map(|(attributes, pattern, typ, expr)| {
StatementKind::new_let(pattern, typ, expr, attributes)
})
attributes().then(let_statement(expr_parser)).validate(
|(attributes, ((pattern, typ), expr)), span, emit| {
let attributes = attributes::validate_secondary_attributes(attributes, span, emit);
StatementKind::new_let(pattern, typ, expr, attributes)
},
)
}

pub fn pattern() -> impl NoirParser<Pattern> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/parser/parser/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn trait_implementation_body() -> impl NoirParser<Vec<Documented<TraitImplItem>>
.map(|(name, alias)| TraitImplItemKind::Type { name, alias });

let let_statement = let_statement(expression()).then_ignore(just(Token::Semicolon)).try_map(
|(_attributes, pattern, typ, expr), span| match pattern {
|((pattern, typ), expr), span| match pattern {
Pattern::Identifier(ident) => Ok(TraitImplItemKind::Constant(ident, typ, expr)),
_ => Err(ParserError::with_reason(
ParserErrorReason::PatternInTraitFunctionParameter,
Expand Down

0 comments on commit 5db563f

Please sign in to comment.