Skip to content

Commit 54578fe

Browse files
author
Mohammad Fawaz
committed
Address review comments
1 parent 1c9c850 commit 54578fe

3 files changed

Lines changed: 18 additions & 34 deletions

File tree

compiler/passes/src/destructuring/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ impl AstReconstructor for DestructuringVisitor<'_> {
6969
.collect();
7070

7171
// Fold appropriately
72-
let combined = match input.op {
73-
Eq => self.fold_with_and(pieces),
74-
Neq => self.fold_with_or(pieces),
72+
let op = match input.op {
73+
Eq => BinaryOperation::And,
74+
Neq => BinaryOperation::Or,
7575
_ => unreachable!(),
7676
};
7777

78-
return (combined, statements);
78+
return (self.fold_with_op(op, pieces.into_iter()), statements);
7979
}
8080

8181
// Fallback

compiler/passes/src/destructuring/visitor.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,21 @@ impl DestructuringVisitor<'_> {
106106
}
107107
}
108108

109-
/// Folds a list of boolean expressions into a left-associated chain of
110-
/// `&&` operations (e.g. `[a, b, c]` → `(a && b) && c`).
111-
/// Panics if the list is empty. Inserts boolean types for all generated nodes.
112-
pub fn fold_with_and(&mut self, pieces: Vec<Expression>) -> Expression {
109+
/// Folds an iterator of expressions into a left-associated chain using `op`.
110+
///
111+
/// Given expressions `[e1, e2, e3]`, this produces `((e1 op e2) op e3)`.
112+
/// Each intermediate node is assigned a fresh ID and recorded as `Boolean`
113+
/// in the type table.
114+
///
115+
/// Panics if the iterator is empty.
116+
pub fn fold_with_op<I>(&mut self, op: BinaryOperation, pieces: I) -> Expression
117+
where
118+
I: Iterator<Item = Expression>,
119+
{
113120
pieces
114-
.into_iter()
115121
.reduce(|left, right| {
116122
let expr: Expression = BinaryExpression {
117-
op: BinaryOperation::And,
123+
op,
118124
left,
119125
right,
120126
span: Default::default(),
@@ -125,29 +131,7 @@ impl DestructuringVisitor<'_> {
125131
self.state.type_table.insert(expr.id(), Type::Boolean);
126132
expr
127133
})
128-
.expect("fold_with_and called with empty vec")
129-
}
130-
131-
/// Folds a list of boolean expressions into a left-associated chain of
132-
/// `||` operations (e.g. `[a, b, c]` → `(a || b) || c`).
133-
/// Panics if the list is empty. Inserts boolean types for all generated nodes.
134-
pub fn fold_with_or(&mut self, pieces: Vec<Expression>) -> Expression {
135-
pieces
136-
.into_iter()
137-
.reduce(|left, right| {
138-
let expr: Expression = BinaryExpression {
139-
op: BinaryOperation::Or,
140-
left,
141-
right,
142-
span: Default::default(),
143-
id: self.state.node_builder.next_id(),
144-
}
145-
.into();
146-
147-
self.state.type_table.insert(expr.id(), Type::Boolean);
148-
expr
149-
})
150-
.expect("fold_with_or called with empty vec")
134+
.expect("fold_with_op called with empty iterator")
151135
}
152136

153137
// Given the `expression` of tuple type, make a definition assigning variable to its members.

compiler/passes/src/option_lowering/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl leo_ast::AstReconstructor for OptionLoweringVisitor<'_> {
173173
span: Span::default(),
174174
id: self.state.node_builder.next_id(),
175175
};
176-
self.state.type_table.insert(dbg!(is_some_access.id()), Type::Boolean);
176+
self.state.type_table.insert(is_some_access.id(), Type::Boolean);
177177

178178
// Create assertion: ensure `is_some` is `true`.
179179
let assert_stmt = AssertStatement {

0 commit comments

Comments
 (0)