diff --git a/compiler/ast-passes/src/canonicalization/canonicalizer.rs b/compiler/ast-passes/src/canonicalization/canonicalizer.rs index 2798ec1937..0bec06f5a2 100644 --- a/compiler/ast-passes/src/canonicalization/canonicalizer.rs +++ b/compiler/ast-passes/src/canonicalization/canonicalizer.rs @@ -409,12 +409,14 @@ impl Canonicalizer { }) } Statement::Iteration(iteration) => { + let type_ = self.canonicalize_self_type(Some(&iteration.type_)).unwrap(); let start = self.canonicalize_expression(&iteration.start); let stop = self.canonicalize_expression(&iteration.stop); let block = self.canonicalize_block(&iteration.block); Statement::Iteration(Box::new(IterationStatement { variable: iteration.variable.clone(), + type_, start, stop, inclusive: iteration.inclusive, diff --git a/compiler/ast/src/reducer/reconstructing_director.rs b/compiler/ast/src/reducer/reconstructing_director.rs index 3d2c7785b3..dac65809bd 100644 --- a/compiler/ast/src/reducer/reconstructing_director.rs +++ b/compiler/ast/src/reducer/reconstructing_director.rs @@ -364,11 +364,13 @@ impl ReconstructingDirector { pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result { let variable = self.reduce_identifier(&iteration.variable)?; + let type_ = self.reduce_type(&iteration.type_, iteration.span())?; let start = self.reduce_expression(&iteration.start)?; let stop = self.reduce_expression(&iteration.stop)?; let block = self.reduce_block(&iteration.block)?; - self.reducer.reduce_iteration(iteration, variable, start, stop, block) + self.reducer + .reduce_iteration(iteration, variable, type_, start, stop, block) } pub fn reduce_console(&mut self, console_function_call: &ConsoleStatement) -> Result { diff --git a/compiler/ast/src/reducer/reconstructing_reducer.rs b/compiler/ast/src/reducer/reconstructing_reducer.rs index 4db38d8fa5..4b0b3938d5 100644 --- a/compiler/ast/src/reducer/reconstructing_reducer.rs +++ b/compiler/ast/src/reducer/reconstructing_reducer.rs @@ -343,12 +343,14 @@ pub trait ReconstructingReducer { &mut self, iteration: &IterationStatement, variable: Identifier, + type_: Type, start: Expression, stop: Expression, block: Block, ) -> Result { Ok(IterationStatement { variable, + type_, start, stop, inclusive: iteration.inclusive, diff --git a/compiler/ast/src/statements/iteration.rs b/compiler/ast/src/statements/iteration.rs index 7e2a529565..3fdfc94bc9 100644 --- a/compiler/ast/src/statements/iteration.rs +++ b/compiler/ast/src/statements/iteration.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Block, Expression, Identifier, Node}; +use crate::{Block, Expression, Identifier, Node, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -25,6 +25,8 @@ use std::fmt; pub struct IterationStatement { /// The binding / variable to introduce in the body `block`. pub variable: Identifier, + /// The type of the iteration. + pub type_: Type, /// The start of the iteration. pub start: Expression, /// The end of the iteration, possibly `inclusive`. diff --git a/compiler/parser/src/parser/statement.rs b/compiler/parser/src/parser/statement.rs index d25139cdaa..d80b5953cd 100644 --- a/compiler/parser/src/parser/statement.rs +++ b/compiler/parser/src/parser/statement.rs @@ -193,6 +193,8 @@ impl ParserContext<'_> { pub fn parse_loop_statement(&mut self) -> Result { let start_span = self.expect(Token::For)?; let ident = self.expect_ident()?; + self.expect(Token::Colon)?; + let type_ = self.parse_type()?; self.expect(Token::In)?; // Parse iteration range. @@ -208,6 +210,7 @@ impl ParserContext<'_> { Ok(IterationStatement { span: start_span + block.span.clone(), variable: ident, + type_: type_.0, start, stop, inclusive,