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
35 changes: 19 additions & 16 deletions crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ error: Variable not dropped.
fn foo(x: ACopy, y: ADrop) -> ADrop {
^
note: the variable needs to be dropped due to the divergence here:
--> lib.cairo:9:8
if true {
^^^^
--> lib.cairo:9:5-12:13
if true {
_____^
| ...
| } else {}
|_____________^
note: Trait has no implementation in context: core::traits::Drop::<test::ACopy>.
note: Trait has no implementation in context: core::traits::Destruct::<test::ACopy>.

Expand Down Expand Up @@ -646,16 +649,16 @@ Statements:
(v8: core::bool) <- core::integer::U8PartialEq::eq(v4{`self.b`}, v7{`0`})
End:
Match(match_enum(v8{`self.b == 0`}) {
bool::False(v11) => blk2,
bool::False(v10) => blk2,
bool::True(v9) => blk1,
})

blk1:
Statements:
(v10: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v10{`self.a`})
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v11{`self.a`})
End:
Goto(blk3, {v3 -> v12, v10 -> v13})
Goto(blk3, {v3 -> v12, v11 -> v13})

blk2:
Statements:
Expand Down Expand Up @@ -712,16 +715,16 @@ Statements:
(v8: core::bool) <- core::integer::U8PartialEq::eq(v4{`v.b`}, v7{`0`})
End:
Match(match_enum(v8{`v.b == 0`}) {
bool::False(v11) => blk2,
bool::False(v10) => blk2,
bool::True(v9) => blk1,
})

blk1:
Statements:
(v10: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v10{`v.a`})
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
() <- test::invalidate(v11{`v.a`})
End:
Goto(blk3, {v3 -> v12, v10 -> v13})
Goto(blk3, {v3 -> v12, v11 -> v13})

blk2:
Statements:
Expand Down Expand Up @@ -824,16 +827,16 @@ Statements:
(v2: core::bool) <- bool::True(v1{`true`})
End:
Match(match_enum(v2{`true`}) {
bool::False(v8) => blk2,
bool::False(v4) => blk2,
bool::True(v3) => blk1,
})

blk1:
Statements:
(v4: core::integer::u32) <- struct_destructure(v0{`a.x`})
(v5: core::integer::u32, v6: @core::integer::u32) <- snapshot(v4{`a.x`})
(v7: ()) <- test::bar(v6{`@a.x`})
(v10: test::A) <- struct_construct(v5{`true`})
(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})

Expand Down
14 changes: 7 additions & 7 deletions crates/cairo-lang-lowering/src/inline/test_data/inline
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ Statements:
(v6: core::bool) <- core::Felt252PartialEq::eq(v2, v5)
End:
Match(match_enum(v6) {
bool::False(v9) => blk2,
bool::False(v8) => blk2,
bool::True(v7) => blk1,
})

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

blk2:
Statements:
Expand Down Expand Up @@ -473,7 +473,7 @@ blk7:
Statements:
End:
Match(match_enum(v6) {
bool::False(v9) => blk2,
bool::False(v8) => blk2,
bool::True(v7) => blk1,
})

Expand All @@ -489,18 +489,18 @@ blk9:
Statements:
(v20: core::felt252) <- 1
End:
Goto(blk11, {v20 -> v8})
Goto(blk11, {v20 -> v9})

blk10:
Statements:
(v21: core::felt252) <- 0
End:
Goto(blk11, {v21 -> v8})
Goto(blk11, {v21 -> v9})

blk11:
Statements:
End:
Goto(blk3, {v8 -> v10})
Goto(blk3, {v9 -> v10})

//! > lowering_diagnostics

Expand Down
31 changes: 16 additions & 15 deletions crates/cairo-lang-lowering/src/lower/lower_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ use crate::{MatchArm, MatchEnumInfo, MatchInfo};
///
/// In particular, note that if `conditions` is empty, there are no conditions and the
/// expression is simply [Self::expr].
pub struct ConditionedExpr<'a> {
pub struct ConditionedExpr<'db, 'a> {
pub expr: semantic::ExprId,
pub conditions: &'a [Condition],
pub else_block: Option<semantic::ExprId>,
/// The location of the `if` expression.
pub if_expr_location: LocationId<'db>,
}

impl ConditionedExpr<'_> {
impl ConditionedExpr<'_, '_> {
/// Returns a copy of self, without the first condition.
pub fn remove_first(&self) -> Self {
Self { conditions: &self.conditions[1..], ..*self }
Expand Down Expand Up @@ -59,6 +61,7 @@ pub fn lower_expr_if<'db>(
expr: expr.if_block,
conditions: &expr.conditions,
else_block: expr.else_block,
if_expr_location: ctx.get_location(expr.stable_ptr.untyped()),
},
)
}
Expand All @@ -68,21 +71,21 @@ pub fn lower_if_bool_condition<'db>(
ctx: &mut LoweringContext<'db, '_>,
builder: &mut BlockBuilder<'db>,
condition: semantic::ExprId,
inner_expr: ConditionedExpr<'_>,
inner_expr: ConditionedExpr<'db, '_>,
) -> LoweringResult<'db, LoweredExpr<'db>> {
// The condition cannot be unit.
let condition_var = lower_expr_to_var_usage(ctx, builder, condition)?;
let db = ctx.db;
let unit_ty = corelib::unit_ty(db);

let condition_expr = &ctx.function_body.arenas.exprs[condition];
let stable_ptr = condition_expr.stable_ptr().untyped();
let condition_location = ctx.get_location(stable_ptr);
let if_expr_location = inner_expr.if_expr_location;

// Main block.
let subscope_main = create_subscope(ctx, builder);
let block_main_id = subscope_main.block_id;
let main_block_var_id = ctx.new_var(VarRequest { ty: unit_ty, location: condition_location });
let main_block_var_id = ctx.new_var(VarRequest { ty: unit_ty, location: if_expr_location });
let else_block_input_var_id =
ctx.new_var(VarRequest { ty: unit_ty, location: if_expr_location });

let block_main = lower_conditioned_expr_and_seal(ctx, subscope_main, &inner_expr)
.map_err(LoweringFlowError::Failed)?;
Expand All @@ -91,10 +94,8 @@ pub fn lower_if_bool_condition<'db>(
let subscope_else = create_subscope(ctx, builder);
let block_else_id = subscope_else.block_id;

let else_block_input_var_id =
ctx.new_var(VarRequest { ty: unit_ty, location: condition_location });
let block_else =
lower_optional_else_block(ctx, subscope_else, inner_expr.else_block, condition_location)
lower_optional_else_block(ctx, subscope_else, inner_expr.else_block, if_expr_location)
.map_err(LoweringFlowError::Failed)?;

let match_info = MatchInfo::Enum(MatchEnumInfo {
Expand All @@ -112,13 +113,13 @@ pub fn lower_if_bool_condition<'db>(
var_ids: vec![main_block_var_id],
},
],
location: condition_location,
location: if_expr_location,
});
builder.merge_and_end_with_match(
ctx,
match_info,
vec![block_main, block_else],
condition_location,
if_expr_location,
)
}

Expand All @@ -128,7 +129,7 @@ pub fn lower_if_let_condition<'db>(
builder: &mut BlockBuilder<'db>,
matched_expr_id: semantic::ExprId,
patterns: &[semantic::PatternId],
inner_expr: ConditionedExpr<'_>,
inner_expr: ConditionedExpr<'db, '_>,
) -> LoweringResult<'db, LoweredExpr<'db>> {
let matched_expr = &ctx.function_body.arenas.exprs[matched_expr_id];
let stable_ptr = matched_expr.stable_ptr().untyped();
Expand Down Expand Up @@ -169,7 +170,7 @@ pub fn lower_if_let_condition<'db>(
fn lower_conditioned_expr<'db>(
ctx: &mut LoweringContext<'db, '_>,
builder: &mut BlockBuilder<'db>,
expr: &ConditionedExpr<'_>,
expr: &ConditionedExpr<'db, '_>,
) -> LoweringResult<'db, LoweredExpr<'db>> {
log::trace!(
"Lowering a conditioned expression: {:?} (# of conditions: {})",
Expand All @@ -196,7 +197,7 @@ fn lower_conditioned_expr<'db>(
pub fn lower_conditioned_expr_and_seal<'db>(
ctx: &mut LoweringContext<'db, '_>,
mut builder: BlockBuilder<'db>,
expr: &ConditionedExpr<'_>,
expr: &ConditionedExpr<'db, '_>,
) -> Maybe<SealedBlockBuilder<'db>> {
let lowered_expr = lower_conditioned_expr(ctx, &mut builder, expr);
lowered_expr_to_block_scope_end(ctx, builder, lowered_expr)
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-lowering/src/lower/lower_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub enum MatchArmWrapper<'db, 'a> {
),
/// Similar to [Self::Arm], except that the expression is a conditioned expression
/// (see [ConditionedExpr]).
ConditionedArm(&'a [PatternId], ConditionedExpr<'a>),
ConditionedArm(&'a [PatternId], ConditionedExpr<'db, 'a>),
}

impl<'db, 'a> From<&'a semantic::MatchArm> for MatchArmWrapper<'db, 'a> {
Expand Down
6 changes: 3 additions & 3 deletions crates/cairo-lang-lowering/src/lower/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ Statements:
(v13: core::bool) <- core::Felt252PartialEq::eq(v9, v12)
End:
Match(match_enum(v13) {
bool::False(v17) => blk2,
bool::False(v15) => blk2,
bool::True(v14) => blk1,
})

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

blk2:
Statements:
Expand Down
Loading