//! > Check that literals are moved to the right arm.

//! > test_runner_name
test_reorder_statements

//! > function_code
fn foo(x: felt252) -> felt252 {
    let opt = get_option();

    let _unused = 17;
    let _unused2 = ();

    let one = 1;
    let two = 2;
    let threex = 3 + x;

    match opt {
        Some(_) => one + two,
        None => {
            let four = 4;
            match opt {
                Some(_) => threex + four,
                None => one,
            }
        },
    }
}

//! > function_name
foo

//! > module_code
extern fn get_option() -> Option<u16> nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > before
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::get_option() {
    Option::Some(v1) => blk1,
    Option::None => blk2,
  })

blk1:
Statements:
  (v2: core::option::Option::<core::integer::u16>) <- Option::Some(v1)
End:
  Goto(blk3, {v2 -> v3})

blk2:
Statements:
  (v4: ()) <- struct_construct()
  (v5: core::option::Option::<core::integer::u16>) <- Option::None(v4)
End:
  Goto(blk3, {v5 -> v3})

blk3:
Statements:
  (v6: core::felt252) <- 17
  (v7: ()) <- struct_construct()
  (v8: core::felt252) <- 1
  (v9: core::felt252) <- 2
  (v10: core::felt252) <- 3
  (v11: core::felt252) <- core::felt252_add(v10, v0)
End:
  Match(match_enum(v3) {
    Option::Some(v12) => blk4,
    Option::None(v13) => blk5,
  })

blk4:
Statements:
  (v14: core::felt252) <- 3
End:
  Goto(blk9, {v14 -> v15})

blk5:
Statements:
  (v16: core::felt252) <- 4
End:
  Match(match_enum(v3) {
    Option::Some(v17) => blk6,
    Option::None(v18) => blk7,
  })

blk6:
Statements:
  (v19: core::felt252) <- core::felt252_add(v11, v16)
End:
  Goto(blk8, {v19 -> v20})

blk7:
Statements:
End:
  Goto(blk8, {v8 -> v20})

blk8:
Statements:
End:
  Goto(blk9, {v20 -> v15})

blk9:
Statements:
End:
  Return(v15)

//! > after
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::get_option() {
    Option::Some(v1) => blk1,
    Option::None => blk2,
  })

blk1:
Statements:
  (v2: core::option::Option::<core::integer::u16>) <- Option::Some(v1)
End:
  Goto(blk3, {v2 -> v3})

blk2:
Statements:
  (v4: ()) <- struct_construct()
  (v5: core::option::Option::<core::integer::u16>) <- Option::None(v4)
End:
  Goto(blk3, {v5 -> v3})

blk3:
Statements:
End:
  Match(match_enum(v3) {
    Option::Some(v12) => blk4,
    Option::None(v13) => blk5,
  })

blk4:
Statements:
  (v14: core::felt252) <- 3
End:
  Goto(blk9, {v14 -> v15})

blk5:
Statements:
End:
  Match(match_enum(v3) {
    Option::Some(v17) => blk6,
    Option::None(v18) => blk7,
  })

blk6:
Statements:
  (v10: core::felt252) <- 3
  (v11: core::felt252) <- core::felt252_add(v10, v0)
  (v16: core::felt252) <- 4
  (v19: core::felt252) <- core::felt252_add(v11, v16)
End:
  Goto(blk8, {v19 -> v20})

blk7:
Statements:
  (v8: core::felt252) <- 1
End:
  Goto(blk8, {v8 -> v20})

blk8:
Statements:
End:
  Goto(blk9, {v20 -> v15})

blk9:
Statements:
End:
  Return(v15)

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

//! > Check that not statements are moved to the right arm or removed.

//! > test_runner_name
test_reorder_statements

//! > function_code
fn foo() {
    let a = true;

    let b = !a;
    !!a;

    match get_option() {
        Some(_) => a,
        None => false ^ b,
    };
}

//! > function_name
foo

//! > module_code
extern fn get_option() -> Option<u16> nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
  (v0: ()) <- struct_construct()
  (v1: core::bool) <- bool::True(v0)
  (v2: core::bool) <- core::bool_not_impl(v1)
  (v3: core::bool) <- core::bool_not_impl(v1)
  (v4: core::bool) <- core::bool_not_impl(v3)
End:
  Match(match test::get_option() {
    Option::Some(v5) => blk1,
    Option::None => blk2,
  })

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

blk2:
Statements:
  (v6: ()) <- struct_construct()
  (v7: ()) <- struct_construct()
  (v8: core::bool) <- bool::False(v7)
  (v9: core::bool) <- core::bool_xor_impl(v8, v2)
End:
  Goto(blk3, {})

blk3:
Statements:
  (v10: ()) <- struct_construct()
End:
  Return(v10)

//! > after
Parameters:
blk0 (root):
Statements:
End:
  Match(match test::get_option() {
    Option::Some(v5) => blk1,
    Option::None => blk2,
  })

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

blk2:
Statements:
  (v0: ()) <- struct_construct()
  (v1: core::bool) <- bool::True(v0)
  (v2: core::bool) <- core::bool_not_impl(v1)
  (v7: ()) <- struct_construct()
  (v8: core::bool) <- bool::False(v7)
  (v9: core::bool) <- core::bool_xor_impl(v8, v2)
End:
  Goto(blk3, {})

blk3:
Statements:
  (v10: ()) <- struct_construct()
End:
  Return(v10)

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

//! > Test non-droppable struct

//! > test_runner_name
test_reorder_statements

//! > function_code
fn foo(a: felt252) {
    let b = NonDupStruct { a };
    let NonDupStruct { a: _ } = b;
}

//! > function_name
foo

//! > module_code
struct NonDupStruct {
    a: felt252,
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > before
Parameters: v0: core::felt252
blk0 (root):
Statements:
  (v1: test::NonDupStruct) <- struct_construct(v0)
  (v2: core::felt252) <- struct_destructure(v1)
  (v3: ()) <- struct_construct()
End:
  Return(v3)

//! > after
Parameters: v0: core::felt252
blk0 (root):
Statements:
  (v1: test::NonDupStruct) <- struct_construct(v0)
  (v2: core::felt252) <- struct_destructure(v1)
  (v3: ()) <- struct_construct()
End:
  Return(v3)

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

//! > Test match inputs are moved next to the match.

//! > test_runner_name
test_reorder_statements

//! > function_code
fn foo() -> felt252 {
    let a = true;
    let v = 5;
    if a {
        v + 3
    } else {
        v + 3
    }
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
  (v0: ()) <- struct_construct()
  (v1: core::bool) <- bool::True(v0)
  (v2: core::felt252) <- 5
  (v3: ()) <- struct_construct()
  (v4: core::felt252) <- 3
  (v5: core::felt252) <- 8
End:
  Return(v5)

//! > after
Parameters:
blk0 (root):
Statements:
  (v5: core::felt252) <- 8
End:
  Return(v5)
