//! > 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[expr12](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[expr12](v0, v1, v3)
End:
  Match(match_enum(v6) {
    PanicResult::Ok(v7) => blk1,
    PanicResult::Err(v8) => blk2,
  })

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

blk2:
Statements:
  (v13: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v8)
End:
  Return(v4, v5, v13)


Generated loop lowering for source location:
    while 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(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[expr12](v9)
End:
  Goto(blk3, {v11 -> v14, v10 -> v13})

blk2:
Statements:
  (v15: ()) <- struct_construct()
End:
  Goto(blk3, {v1 -> v14, v15 -> v13})

blk3:
Statements:
End:
  Return(v14, 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:
End:
  Match(match core::felt252_is_zero(v2) {
    IsZeroResult::Zero => blk2,
    IsZeroResult::NonZero(v7) => blk3,
  })

blk2:
Statements:
  (v8: ()) <- struct_construct()
  (v9: (core::felt252, ())) <- struct_construct(v2, v8)
  (v10: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Ok(v9)
End:
  Return(v3, v4, v10)

blk3:
Statements:
  (v11: core::felt252) <- 1
  (v12: core::felt252) <- core::felt252_sub(v2, v11)
  (v13: core::RangeCheck, v14: core::gas::GasBuiltin, v15: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr12](v3, v4, v12)
End:
  Return(v13, v14, v15)

blk4:
Statements:
  (v16: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
  (v17: core::felt252) <- 375233589013918064796019
  (v18: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v16, v17)
  (v19: core::panics::Panic) <- struct_construct()
  (v20: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v19, v18)
  (v21: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Err(v20)
End:
  Return(v5, v6, 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[expr12](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[expr8](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[expr8](v0, v1, v2)
End:
  Match(match_enum(v5) {
    PanicResult::Ok(v6) => blk1,
    PanicResult::Err(v7) => blk2,
  })

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

blk2:
Statements:
  (v14: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v7)
End:
  Return(v3, v4, v14)


Generated loop lowering for source location:
    while let MyEnum::A(x) = a() {
    ^****************************^

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[expr8](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:
End:
  Match(match test::a() {
    MyEnum::A(v7) => blk2,
    MyEnum::B => blk3,
    MyEnum::C => blk4,
  })

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

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

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

blk5:
Statements:
  (v12: ()) <- struct_construct()
  (v13: (core::felt252, ())) <- struct_construct(v2, v12)
  (v14: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Ok(v13)
End:
  Return(v3, v4, v14)

blk6:
Statements:
  (v15: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
  (v16: core::felt252) <- 375233589013918064796019
  (v17: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v15, v16)
  (v18: core::panics::Panic) <- struct_construct()
  (v19: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v18, v17)
  (v20: core::panics::PanicResult::<(core::felt252, ())>) <- PanicResult::Err(v19)
End:
  Return(v5, v6, v20)
