//! > Test loop.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

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

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: core::felt252
blk0 (root):
Statements:
  (v3: core::felt252) <- 5
  (v4: core::felt252, v5: @core::felt252) <- snapshot(v2)
  (v6: core::RangeCheck, v7: core::gas::GasBuiltin, v8: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[40-84](v0, v1, v3, v5)
End:
  Match(match_enum(v8) {
    PanicResult::Ok(v9) => blk1,
    PanicResult::Err(v10) => blk2,
  })

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

blk2:
Statements:
  (v16: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v10)
End:
  Return(v6, v7, v16)

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

//! > Test while let

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

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

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum MyEnum {
    A: felt252,
    B,
    C,
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: test::MyEnum
blk0 (root):
Statements:
  (v3: core::felt252) <- 0
  (v4: core::RangeCheck, v5: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[50-108](v0, v1, v2, 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::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v8)
End:
  Return(v4, v5, v14)

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

//! > Test while let extern enum

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
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
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
  (v2: core::RangeCheck, v3: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[41-100]{0, }(v0, v1)
End:
  Match(match_enum(v4) {
    PanicResult::Ok(v5) => blk1,
    PanicResult::Err(v6) => blk2,
  })

blk1:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
  (v8: core::felt252, v9: ()) <- struct_destructure(v5)
  (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(v2, v7, v13)

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

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

//! > Test while let tuple

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() -> felt252 {
    let mut y = 0;
    while let (MyEnum::A(x), true) = (a(), 5 == 6) {
        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
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-118](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::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v7)
End:
  Return(v3, v4, v15)

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

//! > Test while let unsupported tuple

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() -> felt252 {
    let mut y = 0;
    while let (MyEnum::A(x), 3) = (a(), 3) {
        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
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
  (v2: core::RangeCheck, v3: core::gas::GasBuiltin, v4: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[41-110]{0, }(v0, v1)
End:
  Match(match_enum(v4) {
    PanicResult::Ok(v5) => blk1,
    PanicResult::Err(v6) => blk2,
  })

blk1:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
  (v8: core::felt252, v9: ()) <- struct_destructure(v5)
  (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(v2, v7, v13)

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

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

//! > Test while let numeric

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() -> felt252 {
    let mut y = 0;
    while let x = y {
        y = y + x
    }
    y = y + 1;
    return y;
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

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

blk1:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
  (v8: core::felt252, v9: ()) <- struct_destructure(v5)
  (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(v2, v7, v13)

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

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

//! > Test while let underscore

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

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

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum MyEnum {
    A: felt252,
    B,
    C,
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin, v2: test::MyEnum
blk0 (root):
Statements:
  (v3: core::RangeCheck, v4: core::gas::GasBuiltin, v5: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[50-97]{NotSpecialized, 0, }(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::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v7)
End:
  Return(v3, v4, v15)
