//! > Test loop.

//! > test_runner_name
test_function_lowering

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

//! > function_name
foo

//! > module_code

//! > 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::RangeCheck, v5: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr12](v0, v1, v3, v2)
End:
  Match(match_enum(v6) {
    PanicResult::Ok(v7) => blk1,
    PanicResult::Err(v8) => blk2,
  })

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

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

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

//! > Test while let

//! > test_runner_name
test_function_lowering

//! > function
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[expr8](v0, v1, v2, 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)

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

//! > Test while let extern enum

//! > test_runner_name
test_function_lowering

//! > 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
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)

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

//! > Test while let tuple

//! > test_runner_name
test_function_lowering

//! > function
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[expr15](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)

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

//! > Test while let unsupported tuple

//! > test_runner_name
test_function_lowering

//! > function
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
error: Unsupported value in while-let. Currently, while-let on tuples only supports enums as tuple members.
 --> lib.cairo:9:35
    while let (MyEnum::A(x), 3) = (a(), 3) {
                                  ^******^

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

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

//! > Test while let numeric

//! > test_runner_name
test_function_lowering

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

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > lowering_diagnostics
error: Numeric values are not supported in while-let conditions.
 --> lib.cairo:3:5
    while let x = y {
    ^***************^

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

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

//! > Test while let underscore

//! > test_runner_name
test_function_lowering

//! > function
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::felt252) <- 0
  (v4: core::RangeCheck, v5: core::gas::GasBuiltin, v6: core::panics::PanicResult::<(core::felt252, ())>) <- test::foo[expr8](v0, v1, v2, 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) <- 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(v4, v5, v14)

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

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

//! > Test while let unsupported type

//! > test_runner_name
test_function_lowering

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

//! > function_name
foo

//! > module_code
struct MyStruct {
    A: felt252,
    B: felt252,
}

//! > semantic_diagnostics

//! > lowering_diagnostics
error: Unsupported type in while-let. Type: `test::MyStruct`.
 --> lib.cairo:7:19
    while let _ = a {
                  ^

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