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: use new_let more widely #5882

Merged
merged 1 commit into from
Sep 2, 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
26 changes: 9 additions & 17 deletions aztec_macros/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use noirc_errors::{Span, Spanned};
use noirc_frontend::ast::{
BinaryOpKind, CallExpression, CastExpression, Expression, ExpressionKind, FunctionReturnType,
Ident, IndexExpression, InfixExpression, Lambda, LetStatement, MemberAccessExpression,
MethodCallExpression, NoirTraitImpl, Path, PathSegment, Pattern, PrefixExpression, Statement,
StatementKind, TraitImplItem, UnaryOp, UnresolvedType, UnresolvedTypeData,
Ident, IndexExpression, InfixExpression, Lambda, MemberAccessExpression, MethodCallExpression,
NoirTraitImpl, Path, PathSegment, Pattern, PrefixExpression, Statement, StatementKind,
TraitImplItem, UnaryOp, UnresolvedType, UnresolvedTypeData,
};
use noirc_frontend::token::SecondaryAttribute;

Expand Down Expand Up @@ -73,13 +73,11 @@ pub fn mutable(name: &str) -> Pattern {
}

pub fn mutable_assignment(name: &str, assigned_to: Expression) -> Statement {
make_statement(StatementKind::Let(LetStatement {
pattern: mutable(name),
r#type: make_type(UnresolvedTypeData::Unspecified),
expression: assigned_to,
comptime: false,
attributes: vec![],
}))
make_statement(StatementKind::new_let(
mutable(name),
make_type(UnresolvedTypeData::Unspecified),
assigned_to,
))
}

pub fn mutable_reference(variable_name: &str) -> Expression {
Expand All @@ -98,13 +96,7 @@ pub fn assignment_with_type(
typ: UnresolvedTypeData,
assigned_to: Expression,
) -> Statement {
make_statement(StatementKind::Let(LetStatement {
pattern: pattern(name),
r#type: make_type(typ),
expression: assigned_to,
comptime: false,
attributes: vec![],
}))
make_statement(StatementKind::new_let(pattern(name), make_type(typ), assigned_to))
}

pub fn return_type(path: Path) -> FunctionReturnType {
Expand Down
28 changes: 13 additions & 15 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ impl Recoverable for StatementKind {

impl StatementKind {
pub fn new_let(
((pattern, r#type), expression): ((Pattern, UnresolvedType), Expression),
pattern: Pattern,
r#type: UnresolvedType,
expression: Expression,
) -> StatementKind {
StatementKind::Let(LetStatement {
pattern,
Expand Down Expand Up @@ -715,13 +717,11 @@ impl ForRange {

// let fresh1 = array;
let let_array = Statement {
kind: StatementKind::Let(LetStatement {
pattern: Pattern::Identifier(array_ident.clone()),
r#type: UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: array,
comptime: false,
attributes: vec![],
}),
kind: StatementKind::new_let(
Pattern::Identifier(array_ident.clone()),
UnresolvedTypeData::Unspecified.with_span(Default::default()),
array,
),
span: array_span,
};

Expand Down Expand Up @@ -761,13 +761,11 @@ impl ForRange {

// let elem = array[i];
let let_elem = Statement {
kind: StatementKind::Let(LetStatement {
pattern: Pattern::Identifier(identifier),
r#type: UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: Expression::new(loop_element, array_span),
comptime: false,
attributes: vec![],
}),
kind: StatementKind::new_let(
Pattern::Identifier(identifier),
UnresolvedTypeData::Unspecified.with_span(Default::default()),
Expression::new(loop_element, array_span),
),
span: array_span,
};

Expand Down
36 changes: 15 additions & 21 deletions compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@
None => false,
Some(ast::Statement { kind: ast::StatementKind::Expression(ret_expr), .. }) => {
let save_ret_expr = ast::Statement {
kind: ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Identifier(ident("__debug_expr", ret_expr.span)),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: ret_expr.clone(),
comptime: false,
attributes: vec![],
}),
kind: ast::StatementKind::new_let(
ast::Pattern::Identifier(ident("__debug_expr", ret_expr.span)),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
ret_expr.clone(),
),
span: ret_expr.span,
};
statements.push(save_ret_expr);
Expand Down Expand Up @@ -242,18 +240,16 @@
});

ast::Statement {
kind: ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Tuple(vars_pattern, let_stmt.pattern.span()),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
comptime: false,
expression: ast::Expression {
kind: ast::StatementKind::new_let(
ast::Pattern::Tuple(vars_pattern, let_stmt.pattern.span()),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
ast::Expression {
kind: ast::ExpressionKind::Block(ast::BlockExpression {
statements: block_stmts,
}),
span: let_stmt.expression.span,
},
attributes: vec![],
}),
),
span: *span,
}
}
Expand All @@ -274,13 +270,11 @@
// __debug_expr
// };

let let_kind = ast::StatementKind::Let(ast::LetStatement {
pattern: ast::Pattern::Identifier(ident("__debug_expr", assign_stmt.expression.span)),
r#type: ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
expression: assign_stmt.expression.clone(),
comptime: false,
attributes: vec![],
});
let let_kind = ast::StatementKind::new_let(
ast::Pattern::Identifier(ident("__debug_expr", assign_stmt.expression.span)),
ast::UnresolvedTypeData::Unspecified.with_span(Default::default()),
assign_stmt.expression.clone(),
);
let expression_span = assign_stmt.expression.span;
let new_assign_stmt = match &assign_stmt.lvalue {
ast::LValue::Ident(id) => {
Expand All @@ -291,7 +285,7 @@
}
ast::LValue::Dereference(_lv, span) => {
// TODO: this is a dummy statement for now, but we should
// somehow track the derefence and update the pointed to

Check warning on line 288 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (derefence)
// variable
ast::Statement {
kind: ast::StatementKind::Expression(uint_expr(0, *span)),
Expand Down Expand Up @@ -677,9 +671,9 @@
ast::Pattern::Tuple(patterns, _) => {
stack.extend(patterns.iter().map(|pattern| (pattern, false)));
}
ast::Pattern::Struct(_, pids, _) => {

Check warning on line 674 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
stack.extend(pids.iter().map(|(_, pattern)| (pattern, is_mut)));

Check warning on line 675 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
vars.extend(pids.iter().map(|(id, _)| (id.clone(), false)));

Check warning on line 676 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pids)
}
}
}
Expand All @@ -689,7 +683,7 @@
fn pattern_to_string(pattern: &ast::Pattern) -> String {
match pattern {
ast::Pattern::Identifier(id) => id.0.contents.clone(),
ast::Pattern::Mutable(mpat, _, _) => format!("mut {}", pattern_to_string(mpat.as_ref())),

Check warning on line 686 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (mpat)

Check warning on line 686 in compiler/noirc_frontend/src/debug/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (mpat)
ast::Pattern::Tuple(elements, _) => format!(
"({})",
elements.iter().map(pattern_to_string).collect::<Vec<String>>().join(", ")
Expand Down
10 changes: 2 additions & 8 deletions compiler/noirc_frontend/src/hir/comptime/hir_to_display_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use noirc_errors::{Span, Spanned};
use crate::ast::{
ArrayLiteral, AssignStatement, BlockExpression, CallExpression, CastExpression, ConstrainKind,
ConstructorExpression, ExpressionKind, ForLoopStatement, ForRange, GenericTypeArgs, Ident,
IfExpression, IndexExpression, InfixExpression, LValue, Lambda, LetStatement, Literal,
IfExpression, IndexExpression, InfixExpression, LValue, Lambda, Literal,
MemberAccessExpression, MethodCallExpression, Path, PathSegment, Pattern, PrefixExpression,
UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
};
Expand All @@ -29,13 +29,7 @@ impl HirStatement {
let pattern = let_stmt.pattern.to_display_ast(interner);
let r#type = interner.id_type(let_stmt.expression).to_display_ast();
let expression = let_stmt.expression.to_display_ast(interner);
StatementKind::Let(LetStatement {
pattern,
r#type,
expression,
comptime: false,
attributes: Vec::new(),
})
StatementKind::new_let(pattern, r#type, expression)
}
HirStatement::Constrain(constrain) => {
let expr = constrain.0.to_display_ast(interner);
Expand Down
3 changes: 2 additions & 1 deletion compiler/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

use chumsky::prelude::*;
use iter_extended::vecmap;
use lalrpop_util::lalrpop_mod;

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)
use noirc_errors::{Span, Spanned};

mod assertion;
Expand All @@ -65,8 +65,8 @@
pub(super) mod traits;
mod types;

// synthesized by LALRPOP

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (LALRPOP)
lalrpop_mod!(pub noir_parser);

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lalrpop)

#[cfg(test)]
mod test_helpers;
Expand Down Expand Up @@ -574,7 +574,8 @@
where
P: ExprParser + 'a,
{
let_statement(expr_parser).map(StatementKind::new_let)
let_statement(expr_parser)
.map(|((pattern, typ), expr)| StatementKind::new_let(pattern, typ, expr))
}

pub fn pattern() -> impl NoirParser<Pattern> {
Expand Down
Loading