Skip to content
Open
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
102 changes: 81 additions & 21 deletions crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,49 @@ extern fn use_a_drop(x: ADrop) nopanic;
//! > lowering
Parameters: v0: test::ACopy, v1: test::ADrop
blk0 (root):
Statements:
End:
Goto(blk1, {})

blk1:
Statements:
(v2: ()) <- struct_construct()
(v3: core::bool) <- bool::True(v2{`true`})
End:
Match(match_enum(v3{`true`}) {
bool::False(v5) => blk2,
bool::True(v4) => blk1,
bool::False(v5) => blk3,
bool::True(v4) => blk2,
})

blk1:
blk2:
Statements:
() <- test::use_a_copy(v0{`x`})
() <- test::use_a_drop(v1{`y`})
End:
Goto(blk3, {})
Goto(blk4, {})

blk2:
blk3:
Statements:
End:
Goto(blk6, {})

blk4:
Statements:
End:
Goto(blk7, {})

blk5:
Statements:
End:
Goto(blk5, {})

blk6:
Statements:
() <- test::use_a_drop(v1{`y`})
End:
Goto(blk3, {})
Goto(blk7, {})

blk3:
blk7:
Statements:
() <- test::use_a_copy(v0{`x`})
(v6: ()) <- struct_construct()
Expand Down Expand Up @@ -117,28 +137,48 @@ note: Trait has no implementation in context: core::traits::Destruct::<test::ACo
//! > lowering
Parameters: v0: test::ACopy, v1: test::ADrop
blk0 (root):
Statements:
End:
Goto(blk1, {})

blk1:
Statements:
(v2: ()) <- struct_construct()
(v3: core::bool) <- bool::True(v2{`true`})
End:
Match(match_enum(v3{`true`}) {
bool::False(v5) => blk2,
bool::True(v4) => blk1,
bool::False(v5) => blk3,
bool::True(v4) => blk2,
})

blk1:
blk2:
Statements:
() <- test::use_a_copy(v0{`x`})
() <- test::use_a_drop(v1{`y`})
End:
Goto(blk3, {})
Goto(blk4, {})

blk2:
blk3:
Statements:
End:
Goto(blk3, {})
Goto(blk6, {})

blk3:
blk4:
Statements:
End:
Goto(blk7, {})

blk5:
Statements:
End:
Goto(blk5, {})

blk6:
Statements:
End:
Goto(blk7, {})

blk7:
Statements:
End:
Return(v1)
Expand Down Expand Up @@ -344,27 +384,47 @@ note: Trait has no implementation in context: core::traits::Copy::<test::ADrop>.
//! > lowering
Parameters: v0: test::ADrop, v1: test::ADrop
blk0 (root):
Statements:
End:
Goto(blk1, {})

blk1:
Statements:
(v2: ()) <- struct_construct()
(v3: core::bool) <- bool::True(v2{`true`})
End:
Match(match_enum(v3{`true`}) {
bool::False(v5) => blk2,
bool::True(v4) => blk1,
bool::False(v5) => blk3,
bool::True(v4) => blk2,
})

blk1:
blk2:
Statements:
() <- test::use_a_drop(v1{`y`})
End:
Goto(blk3, {v1 -> v6})
Goto(blk4, {})

blk2:
blk3:
Statements:
End:
Goto(blk3, {v0 -> v6})
Goto(blk6, {})

blk3:
blk4:
Statements:
End:
Goto(blk7, {v1 -> v6})

blk5:
Statements:
End:
Goto(blk5, {})

blk6:
Statements:
End:
Goto(blk7, {v0 -> v6})

blk7:
Statements:
End:
Return(v6)
Expand Down
116 changes: 78 additions & 38 deletions crates/cairo-lang-lowering/src/inline/test_data/inline
Original file line number Diff line number Diff line change
Expand Up @@ -392,115 +392,155 @@ fn bar(a: felt252) -> felt252 {
//! > before
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
Goto(blk1, {})

blk1:
Statements:
(v1: core::felt252, v2: @core::felt252) <- snapshot(v0)
(v3: core::felt252) <- 2
(v4: core::felt252, v5: @core::felt252) <- snapshot(v3)
(v6: core::bool) <- core::Felt252PartialEq::eq(v2, v5)
End:
Match(match_enum(v6) {
bool::False(v9) => blk2,
bool::True(v7) => blk1,
bool::False(v9) => blk3,
bool::True(v7) => blk2,
})

blk1:
blk2:
Statements:
(v8: core::felt252) <- test::bar(v1)
End:
Goto(blk3, {v8 -> v10})
Goto(blk4, {v8 -> v10})

blk2:
blk3:
Statements:
End:
Goto(blk3, {v1 -> v10})
Goto(blk6, {v1 -> v11})

blk3:
blk4:
Statements:
End:
Return(v10)
Goto(blk7, {v1 -> v13, v10 -> v12})

blk5:
Statements:
End:
Goto(blk5, {})

blk6:
Statements:
End:
Goto(blk7, {v11 -> v13, v11 -> v12})

blk7:
Statements:
End:
Return(v12)

//! > after
Parameters: v0: core::felt252
blk0 (root):
Statements:
(v1: core::felt252, v2: @core::felt252) <- snapshot(v0)
(v3: core::felt252) <- 2
(v4: core::felt252, v5: @core::felt252) <- snapshot(v3)
End:
Goto(blk4, {})
Goto(blk1, {})

blk1:
Statements:
(v1: core::felt252, v2: @core::felt252) <- snapshot(v0)
(v3: core::felt252) <- 2
(v4: core::felt252, v5: @core::felt252) <- snapshot(v3)
End:
Goto(blk8, {})

blk2:
Statements:
End:
Goto(blk3, {v1 -> v10})
Goto(blk12, {})

blk3:
Statements:
End:
Return(v10)
Goto(blk6, {v1 -> v11})

blk4:
Statements:
(v11: core::felt252) <- desnap(v2)
(v12: core::felt252) <- desnap(v5)
(v13: core::felt252) <- core::felt252_sub(v11, v12)
End:
Match(match core::felt252_is_zero(v13) {
IsZeroResult::Zero => blk5,
IsZeroResult::NonZero(v14) => blk6,
})
Goto(blk7, {v1 -> v13, v10 -> v12})

blk5:
Statements:
(v15: ()) <- struct_construct()
(v16: core::bool) <- bool::True(v15)
End:
Goto(blk7, {v16 -> v6})
Goto(blk5, {})

blk6:
Statements:
(v17: ()) <- struct_construct()
(v18: core::bool) <- bool::False(v17)
End:
Goto(blk7, {v18 -> v6})
Goto(blk7, {v11 -> v13, v11 -> v12})

blk7:
Statements:
End:
Match(match_enum(v6) {
bool::False(v9) => blk2,
bool::True(v7) => blk1,
})
Return(v12)

blk8:
Statements:
(v14: core::felt252) <- desnap(v2)
(v15: core::felt252) <- desnap(v5)
(v16: core::felt252) <- core::felt252_sub(v14, v15)
End:
Match(match core::felt252_is_zero(v1) {
Match(match core::felt252_is_zero(v16) {
IsZeroResult::Zero => blk9,
IsZeroResult::NonZero(v19) => blk10,
IsZeroResult::NonZero(v17) => blk10,
})

blk9:
Statements:
(v20: core::felt252) <- 1
(v18: ()) <- struct_construct()
(v19: core::bool) <- bool::True(v18)
End:
Goto(blk11, {v20 -> v8})
Goto(blk11, {v19 -> v6})

blk10:
Statements:
(v21: core::felt252) <- 0
(v20: ()) <- struct_construct()
(v21: core::bool) <- bool::False(v20)
End:
Goto(blk11, {v21 -> v8})
Goto(blk11, {v21 -> v6})

blk11:
Statements:
End:
Goto(blk3, {v8 -> v10})
Match(match_enum(v6) {
bool::False(v9) => blk3,
bool::True(v7) => blk2,
})

blk12:
Statements:
End:
Match(match core::felt252_is_zero(v1) {
IsZeroResult::Zero => blk13,
IsZeroResult::NonZero(v22) => blk14,
})

blk13:
Statements:
(v23: core::felt252) <- 1
End:
Goto(blk15, {v23 -> v8})

blk14:
Statements:
(v24: core::felt252) <- 0
End:
Goto(blk15, {v24 -> v8})

blk15:
Statements:
End:
Goto(blk4, {v8 -> v10})

//! > lowering_diagnostics

Expand Down
23 changes: 21 additions & 2 deletions crates/cairo-lang-lowering/src/lower/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,33 @@ impl BlockBuilder {
match_info: MatchInfo,
sealed_blocks: Vec<SealedBlockBuilder>,
location: LocationId,
) -> LoweringResult<LoweredExpr> {
self.merge_and_end(
ctx,
sealed_blocks,
location,
BlockEnd::Match { info: match_info.clone() },
LoweringFlowError::Match(match_info),
)
}

/// Merges the sealed blocks and ends the current block with the given block end.
/// Replaces `self` with a sibling builder.
pub fn merge_and_end(
&mut self,
ctx: &mut LoweringContext<'_, '_>,
sealed_blocks: Vec<SealedBlockBuilder>,
location: LocationId,
block_end: BlockEnd,
err: LoweringFlowError, // TODO
) -> LoweringResult<LoweredExpr> {
let Some((merged_expr, following_block)) = self.merge_sealed(ctx, sealed_blocks, location)
else {
return Err(LoweringFlowError::Match(match_info));
return Err(err);
};
let new_scope = self.sibling_block_builder(following_block);
let prev_scope = std::mem::replace(self, new_scope);
prev_scope.finalize(ctx, BlockEnd::Match { info: match_info });
prev_scope.finalize(ctx, block_end);
Ok(merged_expr)
}

Expand Down
Loading
Loading