//! > Test loop.

//! > test_runner_name
test_generated_function

//! > function
fn foo(y: felt252) -> felt252 {
    let mut x = 5;
    while x != 0 {
        x = x - 1;
    }
    x
}

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters:

//! > lowering
Main:
Parameters: v0: core::felt252
blk0 (root):
Statements:
  (v1: core::felt252) <- 5
  (v3: core::felt252, v2: ()) <- test::foo[51-95](v1)
End:
  Return(v3)


Final lowering:
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: core::felt252
blk0 (root):
Statements:
  (v3: core::felt252) <- 5
  (v4: core::RangeCheck, v5: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[51-95](v0, v1, v3)
End:
  Match(match_enum(v6) {
    PanicResult::Ok(v7) => blk1,
    PanicResult::Err(v8) => blk2,
  })

blk1:
Statements:
  (v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
  (v10: core::felt252, v11: ()) <- struct_destructure(v7)
  (v12: (core::felt252,)) <- struct_construct(v10)
  (v13: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v12)
End:
  Return(v4, v9, v13)

blk2:
Statements:
  (v14: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
  (v15: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v8)
End:
  Return(v4, v14, v15)


Generated loop lowering for source location:
      while x != 0 {
 _____^
|         x = x - 1;
|     }
|_____^

Parameters: v0: core::felt252
blk0 (root):
Statements:
  (v1: core::felt252, v2: @core::felt252) <- snapshot(v0)
  (v3: core::felt252) <- 0
  (v4: core::felt252, v5: @core::felt252) <- snapshot(v3)
  (v6: core::bool) <- core::Felt252PartialEq::ne(v2, v5)
End:
  Match(match_enum(v6) {
    bool::False(v12) => blk2,
    bool::True(v7) => blk1,
  })

blk1:
Statements:
  (v8: core::felt252) <- 1
  (v9: core::felt252) <- core::Felt252Sub::sub(v1, v8)
  (v11: core::felt252, v10: ()) <- test::foo[51-95](v9)
End:
  Return(v11, v10)

blk2:
Statements:
  (v13: ()) <- struct_construct()
End:
  Return(v1, v13)


Final lowering:
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: core::felt252
blk0 (root):
Statements:
End:
  Match(match core::gas::withdraw_gas(v0, v1) {
    Option::Some(v3, v4) => blk1,
    Option::None(v5, v6) => blk4,
  })

blk1:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
End:
  Match(match core::felt252_is_zero(v2) {
    IsZeroResult::Zero => blk2,
    IsZeroResult::NonZero(v8) => blk3,
  })

blk2:
Statements:
  (v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v7)
  (v10: ()) <- struct_construct()
  (v11: (core::felt252, ())) <- struct_construct(v2, v10)
  (v12: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Ok(v11)
End:
  Return(v3, v9, v12)

blk3:
Statements:
  (v13: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v7)
  (v14: core::felt252) <- 1
  (v15: core::felt252) <- core::felt252_sub(v2, v14)
  (v16: core::RangeCheck, v17: core::gas::GasBuiltin, v18: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[51-95](v3, v13, v15)
End:
  Return(v16, v17, v18)

blk4:
Statements:
  (v19: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v6)
  (v20: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
  (v21: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Err(v20)
End:
  Return(v5, v19, v21)

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

//! > Test while-let.

//! > test_runner_name
test_generated_function

//! > function
fn foo(ref arr: Array<felt252>, mut x: felt252) -> felt252 {
    while let true = (x != 0) {
        x = x - 1;
        break;
    }
    x
}

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters:

//! > lowering
Main:
Parameters: v0: core::array::Array::<core::felt252>, v1: core::felt252
blk0 (root):
Statements:
  (v3: core::felt252, v2: ()) <- test::foo[61-133](v1)
End:
  Return(v0, v3)


Final lowering:
Parameters: v0: core::array::Array::<core::felt252>, v1: core::felt252
blk0 (root):
Statements:
End:
  Match(match core::felt252_is_zero(v1) {
    IsZeroResult::Zero => blk1,
    IsZeroResult::NonZero(v2) => blk2,
  })

blk1:
Statements:
End:
  Return(v0, v1)

blk2:
Statements:
  (v3: core::felt252) <- 1
  (v4: core::felt252) <- core::felt252_sub(v1, v3)
End:
  Return(v0, v4)


Generated loop lowering for source location:
      while let true = (x != 0) {
 _____^
| ...
|     }
|_____^

Parameters: v0: core::felt252
blk0 (root):
Statements:
  (v1: core::felt252, v2: @core::felt252) <- snapshot(v0)
  (v3: core::felt252) <- 0
  (v4: core::felt252, v5: @core::felt252) <- snapshot(v3)
  (v6: core::bool) <- core::Felt252PartialEq::ne(v2, v5)
End:
  Match(match_enum(v6) {
    bool::False(v7) => blk1,
    bool::True(v8) => blk2,
  })

blk1:
Statements:
End:
  Goto(blk3, {})

blk2:
Statements:
  (v9: core::felt252) <- 1
  (v10: core::felt252) <- core::Felt252Sub::sub(v1, v9)
  (v11: ()) <- struct_construct()
End:
  Return(v10, v11)

blk3:
Statements:
  (v12: ()) <- struct_construct()
End:
  Return(v1, v12)


Final lowering:
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
  Match(match core::felt252_is_zero(v0) {
    IsZeroResult::Zero => blk1,
    IsZeroResult::NonZero(v1) => blk2,
  })

blk1:
Statements:
End:
  Return(v0)

blk2:
Statements:
  (v2: core::felt252) <- 1
  (v3: core::felt252) <- core::felt252_sub(v0, v2)
End:
  Return(v3)

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

//! > Test while-let extern enum.

//! > test_runner_name
test_generated_function

//! > function
fn foo() -> felt252 {
    let mut y = 0;
    while let MyEnum::A(x) = a() {
        y = y + x
    }
    y = y + 1;
    return y;
}

//! > function_name
foo

//! > module_code
enum MyEnum {
    A: felt252,
    B,
    C,
}
extern fn a() -> MyEnum nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat

//! > lowering
Main:
Parameters:
blk0 (root):
Statements:
  (v0: core::felt252) <- 0
  (v2: core::felt252, v1: ()) <- test::foo[41-100](v0)
  (v3: core::felt252) <- 1
  (v4: core::felt252) <- core::Felt252Add::add(v2, v3)
End:
  Return(v4)


Final lowering:
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
  (v2: core::felt252) <- 0
  (v3: core::RangeCheck, v4: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[41-100](v0, v1, v2)
End:
  Match(match_enum(v5) {
    PanicResult::Ok(v6) => blk1,
    PanicResult::Err(v7) => blk2,
  })

blk1:
Statements:
  (v8: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
  (v9: core::felt252, v10: ()) <- struct_destructure(v6)
  (v11: core::felt252) <- 1
  (v12: core::felt252) <- core::felt252_add(v9, v11)
  (v13: (core::felt252,)) <- struct_construct(v12)
  (v14: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v13)
End:
  Return(v3, v8, v14)

blk2:
Statements:
  (v15: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
  (v16: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v7)
End:
  Return(v3, v15, v16)


Generated loop lowering for source location:
      while let MyEnum::A(x) = a() {
 _____^
|         y = y + x
|     }
|_____^

Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::a() {
    MyEnum::A(v1) => blk1,
    MyEnum::B => blk2,
    MyEnum::C => blk3,
  })

blk1:
Statements:
  (v2: core::felt252) <- core::Felt252Add::add(v0, v1)
  (v4: core::felt252, v3: ()) <- test::foo[41-100](v2)
End:
  Goto(blk6, {v4 -> v6, v3 -> v5})

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

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

blk4:
Statements:
End:
  Match(match test::a() {
  })

blk5:
Statements:
  (v7: ()) <- struct_construct()
End:
  Goto(blk6, {v0 -> v6, v7 -> v5})

blk6:
Statements:
End:
  Return(v6, v5)


Final lowering:
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: core::felt252
blk0 (root):
Statements:
End:
  Match(match core::gas::withdraw_gas(v0, v1) {
    Option::Some(v3, v4) => blk1,
    Option::None(v5, v6) => blk6,
  })

blk1:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v4)
End:
  Match(match test::a() {
    MyEnum::A(v8) => blk2,
    MyEnum::B => blk3,
    MyEnum::C => blk4,
  })

blk2:
Statements:
  (v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v7)
  (v10: core::felt252) <- core::felt252_add(v2, v8)
  (v11: core::RangeCheck, v12: core::gas::GasBuiltin, v13: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[41-100](v3, v9, v10)
End:
  Return(v11, v12, v13)

blk3:
Statements:
  (v14: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v7)
End:
  Goto(blk5, {v14 -> v15})

blk4:
Statements:
  (v16: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v7)
End:
  Goto(blk5, {v16 -> v15})

blk5:
Statements:
  (v17: ()) <- struct_construct()
  (v18: (core::felt252, ())) <- struct_construct(v2, v17)
  (v19: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Ok(v18)
End:
  Return(v3, v15, v19)

blk6:
Statements:
  (v20: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v6)
  (v21: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
  (v22: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Err(v21)
End:
  Return(v5, v20, v22)
