Skip to content
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
25 changes: 13 additions & 12 deletions crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check
Original file line number Diff line number Diff line change
Expand Up @@ -658,19 +658,19 @@ Statements:
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v11{`self.a`})
End:
Goto(blk3, {v3 -> v12, v11 -> v13})
Goto(blk3, {v11 -> v12})

blk2:
Statements:
End:
Goto(blk3, {v3 -> v12, v1 -> v13})
Goto(blk3, {v1 -> v12})

blk3:
Statements:
(v14: ()) <- struct_construct()
(v15: test::MyStruct) <- struct_construct(v13{`self`}, v12{`self`})
(v13: ()) <- struct_construct()
(v14: test::MyStruct) <- struct_construct(v12{`self`}, v3{`self`})
End:
Return(v15, v14)
Return(v14, v13)

//! > ==========================================================================

Expand Down Expand Up @@ -724,17 +724,17 @@ Statements:
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v11{`v.a`})
End:
Goto(blk3, {v3 -> v12, v11 -> v13})
Goto(blk3, {v11 -> v12})

blk2:
Statements:
End:
Goto(blk3, {v3 -> v12, v1 -> v13})
Goto(blk3, {v1 -> v12})

blk3:
Statements:
End:
Return(v12)
Return(v3)

//! > ==========================================================================

Expand Down Expand Up @@ -836,20 +836,21 @@ Statements:
(v5: core::integer::u32) <- struct_destructure(v0{`a.x`})
(v6: core::integer::u32, v7: @core::integer::u32) <- snapshot(v5{`a.x`})
(v8: ()) <- test::bar(v7{`@a.x`})
(v10: test::A) <- struct_construct(v6{`if true { bar(@a.x); }`})
End:
Goto(blk3, {v10 -> v9})
Goto(blk3, {v6 -> v9})

blk2:
Statements:
(v10: core::integer::u32) <- struct_destructure(v0{`if true { bar(@a.x); }`})
End:
Goto(blk3, {v0 -> v9})
Goto(blk3, {v10 -> v9})

blk3:
Statements:
(v11: ()) <- struct_construct()
(v12: test::A) <- struct_construct(v9{`a`})
End:
Return(v9, v11)
Return(v12, v11)

//! > ==========================================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod filtered_patterns;
mod patterns;

/// Creates a graph node for [semantic::ExprIf].
#[allow(dead_code)]
pub fn create_graph_expr_if<'db>(
ctx: &LoweringContext<'db, '_>,
expr: &semantic::ExprIf<'db>,
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-lowering/src/lower/flow_control/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod create_graph;
pub mod create_graph;
mod graph;
mod lower_graph;
pub mod lower_graph;

#[cfg(test)]
mod graph_test;
7 changes: 7 additions & 0 deletions crates/cairo-lang-lowering/src/lower/lower_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use semantic::{Condition, MatchArmSelector};

use super::block_builder::{BlockBuilder, SealedBlockBuilder};
use super::context::{LoweredExpr, LoweringContext, LoweringFlowError, LoweringResult};
use super::flow_control::create_graph::create_graph_expr_if;
use super::flow_control::lower_graph::lower_graph;
use super::lowered_expr_to_block_scope_end;
use crate::diagnostic::LoweringDiagnosticKind::{self};
use crate::diagnostic::{LoweringDiagnosticsBuilder, MatchDiagnostic, MatchError, MatchKind};
Expand Down Expand Up @@ -45,6 +47,11 @@ pub fn lower_expr_if<'db>(
builder: &mut BlockBuilder<'db>,
expr: &semantic::ExprIf<'db>,
) -> LoweringResult<'db, LoweredExpr<'db>> {
if expr.conditions.len() == 1 && matches!(expr.conditions[0], Condition::BoolExpr(_)) {
let graph = create_graph_expr_if(ctx, expr);
return lower_graph(ctx, builder, &graph, ctx.get_location(expr.stable_ptr.untyped()));
}

// Else block is not supported yet for multiple conditions.
if expr.conditions.len() > 1 {
if let Some(else_block) = expr.else_block {
Expand Down
8 changes: 4 additions & 4 deletions crates/cairo-lang-lowering/src/lower/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,15 @@ End:

blk1:
Statements:
(v16: core::felt252, v17: @core::felt252) <- snapshot(v3)
(v18: core::felt252, v19: @core::felt252) <- snapshot(v3)
End:
Goto(blk3, {v16 -> v20, v17 -> v21})
Goto(blk3, {v18 -> v20, v19 -> v21})

blk2:
Statements:
(v18: core::felt252, v19: @core::felt252) <- snapshot(v3)
(v16: core::felt252, v17: @core::felt252) <- snapshot(v3)
End:
Goto(blk3, {v18 -> v20, v19 -> v21})
Goto(blk3, {v16 -> v20, v17 -> v21})

blk3:
Statements:
Expand Down
8 changes: 4 additions & 4 deletions crates/cairo-lang-lowering/src/lower/test_data/loop
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,15 @@ End:

blk1:
Statements:
(v5: core::integer::u8) <- 0
(v6: core::integer::u8) <- 0
End:
Goto(blk3, {v5 -> v7})
Goto(blk3, {v6 -> v7})

blk2:
Statements:
(v6: core::integer::u8) <- 0
(v5: core::integer::u8) <- 0
End:
Goto(blk3, {v6 -> v7})
Goto(blk3, {v5 -> v7})

blk3:
Statements:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ End:

blk1:
Statements:
(v9: core::felt252) <- test::heavy_op1()
(v10: core::felt252) <- test::heavy_op1()
End:
Goto(blk3, {v9 -> v11})
Goto(blk3, {v10 -> v11})

blk2:
Statements:
(v10: core::felt252) <- test::heavy_op2()
(v9: core::felt252) <- test::heavy_op2()
End:
Goto(blk3, {v10 -> v11})
Goto(blk3, {v9 -> v11})

blk3:
Statements:
Expand All @@ -77,15 +77,15 @@ End:

blk1:
Statements:
(v9: core::felt252) <- test::heavy_op1()
(v10: core::felt252) <- test::heavy_op1()
End:
Goto(blk3, {v9 -> v11})
Goto(blk3, {v10 -> v11})

blk2:
Statements:
(v10: core::felt252) <- test::heavy_op2()
(v9: core::felt252) <- test::heavy_op2()
End:
Goto(blk3, {v10 -> v11})
Goto(blk3, {v9 -> v11})

blk3:
Statements:
Expand Down Expand Up @@ -140,15 +140,15 @@ End:

blk1:
Statements:
(v9: core::felt252) <- test::heavy_op1()
(v10: core::felt252) <- test::heavy_op1()
End:
Goto(blk3, {v9 -> v11})
Goto(blk3, {v10 -> v11})

blk2:
Statements:
(v10: core::felt252) <- test::heavy_op2()
(v9: core::felt252) <- test::heavy_op2()
End:
Goto(blk3, {v10 -> v11})
Goto(blk3, {v9 -> v11})

blk3:
Statements:
Expand All @@ -172,16 +172,16 @@ End:
blk1:
Statements:
() <- core::gas::redeposit_gas()
(v9: core::felt252) <- test::heavy_op1()
(v10: core::felt252) <- test::heavy_op1()
End:
Goto(blk3, {v9 -> v11})
Goto(blk3, {v10 -> v11})

blk2:
Statements:
() <- core::gas::redeposit_gas()
(v10: core::felt252) <- test::heavy_op2()
(v9: core::felt252) <- test::heavy_op2()
End:
Goto(blk3, {v10 -> v11})
Goto(blk3, {v9 -> v11})

blk3:
Statements:
Expand Down