Skip to content

Commit ad6a371

Browse files
committed
flow_control: Small changes in lower_if_bool_condition to align with the new code.
commit-id:97600111
1 parent ed018b0 commit ad6a371

File tree

9 files changed

+163
-136
lines changed

9 files changed

+163
-136
lines changed

crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ error: Variable not dropped.
108108
fn foo(x: ACopy, y: ADrop) -> ADrop {
109109
^
110110
note: the variable needs to be dropped due to the divergence here:
111-
--> lib.cairo:9:8
112-
if true {
113-
^^^^
111+
--> lib.cairo:9:5-12:13
112+
if true {
113+
_____^
114+
| ...
115+
| } else {}
116+
|_____________^
114117
note: Trait has no implementation in context: core::traits::Drop::<test::ACopy>.
115118
note: Trait has no implementation in context: core::traits::Destruct::<test::ACopy>.
116119

@@ -646,16 +649,16 @@ Statements:
646649
(v8: core::bool) <- core::integer::U8PartialEq::eq(v4{`self.b`}, v7{`0`})
647650
End:
648651
Match(match_enum(v8{`self.b == 0`}) {
649-
bool::False(v11) => blk2,
652+
bool::False(v10) => blk2,
650653
bool::True(v9) => blk1,
651654
})
652655

653656
blk1:
654657
Statements:
655-
(v10: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
656-
() <- test::invalidate(v10{`self.a`})
658+
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
659+
() <- test::invalidate(v11{`self.a`})
657660
End:
658-
Goto(blk3, {v3 -> v12, v10 -> v13})
661+
Goto(blk3, {v3 -> v12, v11 -> v13})
659662

660663
blk2:
661664
Statements:
@@ -712,16 +715,16 @@ Statements:
712715
(v8: core::bool) <- core::integer::U8PartialEq::eq(v4{`v.b`}, v7{`0`})
713716
End:
714717
Match(match_enum(v8{`v.b == 0`}) {
715-
bool::False(v11) => blk2,
718+
bool::False(v10) => blk2,
716719
bool::True(v9) => blk1,
717720
})
718721

719722
blk1:
720723
Statements:
721-
(v10: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
722-
() <- test::invalidate(v10{`v.a`})
724+
(v11: core::array::Array::<core::felt252>) <- core::array::ArrayDefault::<core::felt252>::default()
725+
() <- test::invalidate(v11{`v.a`})
723726
End:
724-
Goto(blk3, {v3 -> v12, v10 -> v13})
727+
Goto(blk3, {v3 -> v12, v11 -> v13})
725728

726729
blk2:
727730
Statements:
@@ -824,16 +827,16 @@ Statements:
824827
(v2: core::bool) <- bool::True(v1{`true`})
825828
End:
826829
Match(match_enum(v2{`true`}) {
827-
bool::False(v8) => blk2,
830+
bool::False(v4) => blk2,
828831
bool::True(v3) => blk1,
829832
})
830833

831834
blk1:
832835
Statements:
833-
(v4: core::integer::u32) <- struct_destructure(v0{`a.x`})
834-
(v5: core::integer::u32, v6: @core::integer::u32) <- snapshot(v4{`a.x`})
835-
(v7: ()) <- test::bar(v6{`@a.x`})
836-
(v10: test::A) <- struct_construct(v5{`true`})
836+
(v5: core::integer::u32) <- struct_destructure(v0{`a.x`})
837+
(v6: core::integer::u32, v7: @core::integer::u32) <- snapshot(v5{`a.x`})
838+
(v8: ()) <- test::bar(v7{`@a.x`})
839+
(v10: test::A) <- struct_construct(v6{`if true { bar(@a.x); }`})
837840
End:
838841
Goto(blk3, {v10 -> v9})
839842

crates/cairo-lang-lowering/src/inline/test_data/inline

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,15 @@ Statements:
399399
(v6: core::bool) <- core::Felt252PartialEq::eq(v2, v5)
400400
End:
401401
Match(match_enum(v6) {
402-
bool::False(v9) => blk2,
402+
bool::False(v8) => blk2,
403403
bool::True(v7) => blk1,
404404
})
405405

406406
blk1:
407407
Statements:
408-
(v8: core::felt252) <- test::bar(v1)
408+
(v9: core::felt252) <- test::bar(v1)
409409
End:
410-
Goto(blk3, {v8 -> v10})
410+
Goto(blk3, {v9 -> v10})
411411

412412
blk2:
413413
Statements:
@@ -473,7 +473,7 @@ blk7:
473473
Statements:
474474
End:
475475
Match(match_enum(v6) {
476-
bool::False(v9) => blk2,
476+
bool::False(v8) => blk2,
477477
bool::True(v7) => blk1,
478478
})
479479

@@ -489,18 +489,18 @@ blk9:
489489
Statements:
490490
(v20: core::felt252) <- 1
491491
End:
492-
Goto(blk11, {v20 -> v8})
492+
Goto(blk11, {v20 -> v9})
493493

494494
blk10:
495495
Statements:
496496
(v21: core::felt252) <- 0
497497
End:
498-
Goto(blk11, {v21 -> v8})
498+
Goto(blk11, {v21 -> v9})
499499

500500
blk11:
501501
Statements:
502502
End:
503-
Goto(blk3, {v8 -> v10})
503+
Goto(blk3, {v9 -> v10})
504504

505505
//! > lowering_diagnostics
506506

crates/cairo-lang-lowering/src/lower/lower_if.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ use crate::{MatchArm, MatchEnumInfo, MatchInfo};
2424
///
2525
/// In particular, note that if `conditions` is empty, there are no conditions and the
2626
/// expression is simply [Self::expr].
27-
pub struct ConditionedExpr<'a> {
27+
pub struct ConditionedExpr<'db, 'a> {
2828
pub expr: semantic::ExprId,
2929
pub conditions: &'a [Condition],
3030
pub else_block: Option<semantic::ExprId>,
31+
pub if_expr_location: LocationId<'db>,
3132
}
3233

33-
impl ConditionedExpr<'_> {
34+
impl ConditionedExpr<'_, '_> {
3435
/// Returns a copy of self, without the first condition.
3536
pub fn remove_first(&self) -> Self {
3637
Self { conditions: &self.conditions[1..], ..*self }
@@ -59,6 +60,7 @@ pub fn lower_expr_if<'db>(
5960
expr: expr.if_block,
6061
conditions: &expr.conditions,
6162
else_block: expr.else_block,
63+
if_expr_location: ctx.get_location(expr.stable_ptr.untyped()),
6264
},
6365
)
6466
}
@@ -68,21 +70,21 @@ pub fn lower_if_bool_condition<'db>(
6870
ctx: &mut LoweringContext<'db, '_>,
6971
builder: &mut BlockBuilder<'db>,
7072
condition: semantic::ExprId,
71-
inner_expr: ConditionedExpr<'_>,
73+
inner_expr: ConditionedExpr<'db, '_>,
7274
) -> LoweringResult<'db, LoweredExpr<'db>> {
7375
// The condition cannot be unit.
7476
let condition_var = lower_expr_to_var_usage(ctx, builder, condition)?;
7577
let db = ctx.db;
7678
let unit_ty = corelib::unit_ty(db);
7779

78-
let condition_expr = &ctx.function_body.arenas.exprs[condition];
79-
let stable_ptr = condition_expr.stable_ptr().untyped();
80-
let condition_location = ctx.get_location(stable_ptr);
80+
let if_expr_location = inner_expr.if_expr_location;
8181

8282
// Main block.
8383
let subscope_main = create_subscope(ctx, builder);
8484
let block_main_id = subscope_main.block_id;
85-
let main_block_var_id = ctx.new_var(VarRequest { ty: unit_ty, location: condition_location });
85+
let main_block_var_id = ctx.new_var(VarRequest { ty: unit_ty, location: if_expr_location });
86+
let else_block_input_var_id =
87+
ctx.new_var(VarRequest { ty: unit_ty, location: if_expr_location });
8688

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

94-
let else_block_input_var_id =
95-
ctx.new_var(VarRequest { ty: unit_ty, location: condition_location });
9696
let block_else =
97-
lower_optional_else_block(ctx, subscope_else, inner_expr.else_block, condition_location)
97+
lower_optional_else_block(ctx, subscope_else, inner_expr.else_block, if_expr_location)
9898
.map_err(LoweringFlowError::Failed)?;
9999

100100
let match_info = MatchInfo::Enum(MatchEnumInfo {
@@ -112,13 +112,13 @@ pub fn lower_if_bool_condition<'db>(
112112
var_ids: vec![main_block_var_id],
113113
},
114114
],
115-
location: condition_location,
115+
location: if_expr_location,
116116
});
117117
builder.merge_and_end_with_match(
118118
ctx,
119119
match_info,
120120
vec![block_main, block_else],
121-
condition_location,
121+
if_expr_location,
122122
)
123123
}
124124

@@ -128,7 +128,7 @@ pub fn lower_if_let_condition<'db>(
128128
builder: &mut BlockBuilder<'db>,
129129
matched_expr_id: semantic::ExprId,
130130
patterns: &[semantic::PatternId],
131-
inner_expr: ConditionedExpr<'_>,
131+
inner_expr: ConditionedExpr<'db, '_>,
132132
) -> LoweringResult<'db, LoweredExpr<'db>> {
133133
let matched_expr = &ctx.function_body.arenas.exprs[matched_expr_id];
134134
let stable_ptr = matched_expr.stable_ptr().untyped();
@@ -169,7 +169,7 @@ pub fn lower_if_let_condition<'db>(
169169
fn lower_conditioned_expr<'db>(
170170
ctx: &mut LoweringContext<'db, '_>,
171171
builder: &mut BlockBuilder<'db>,
172-
expr: &ConditionedExpr<'_>,
172+
expr: &ConditionedExpr<'db, '_>,
173173
) -> LoweringResult<'db, LoweredExpr<'db>> {
174174
log::trace!(
175175
"Lowering a conditioned expression: {:?} (# of conditions: {})",
@@ -196,7 +196,7 @@ fn lower_conditioned_expr<'db>(
196196
pub fn lower_conditioned_expr_and_seal<'db>(
197197
ctx: &mut LoweringContext<'db, '_>,
198198
mut builder: BlockBuilder<'db>,
199-
expr: &ConditionedExpr<'_>,
199+
expr: &ConditionedExpr<'db, '_>,
200200
) -> Maybe<SealedBlockBuilder<'db>> {
201201
let lowered_expr = lower_conditioned_expr(ctx, &mut builder, expr);
202202
lowered_expr_to_block_scope_end(ctx, builder, lowered_expr)

crates/cairo-lang-lowering/src/lower/lower_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub enum MatchArmWrapper<'db, 'a> {
7474
),
7575
/// Similar to [Self::Arm], except that the expression is a conditioned expression
7676
/// (see [ConditionedExpr]).
77-
ConditionedArm(&'a [PatternId], ConditionedExpr<'a>),
77+
ConditionedArm(&'a [PatternId], ConditionedExpr<'db, 'a>),
7878
}
7979

8080
impl<'db, 'a> From<&'a semantic::MatchArm> for MatchArmWrapper<'db, 'a> {

crates/cairo-lang-lowering/src/lower/test_data/closure

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ Statements:
404404
(v13: core::bool) <- core::Felt252PartialEq::eq(v9, v12)
405405
End:
406406
Match(match_enum(v13) {
407-
bool::False(v17) => blk2,
407+
bool::False(v15) => blk2,
408408
bool::True(v14) => blk1,
409409
})
410410

411411
blk1:
412412
Statements:
413-
(v15: core::felt252, v16: @core::felt252) <- snapshot(v3)
413+
(v16: core::felt252, v17: @core::felt252) <- snapshot(v3)
414414
End:
415-
Goto(blk3, {v15 -> v20, v16 -> v21})
415+
Goto(blk3, {v16 -> v20, v17 -> v21})
416416

417417
blk2:
418418
Statements:

0 commit comments

Comments
 (0)