//! > Test extern function calling.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(ref a: felt252, b: felt252) {
    f(ref a, b);
    g(ref a, b);
    h(ref a, b);
    i(ref a, b);
}

//! > function_name
foo

//! > module_code
extern fn f(ref a: felt252, b: felt252) -> felt252 nopanic;
extern fn g(ref a: felt252, b: felt252) -> (felt252,) nopanic;
extern fn h(ref a: felt252, b: felt252) -> (felt252, felt252) nopanic;
extern fn i(ref a: felt252, b: felt252) -> ((felt252,),) nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::felt252, v1: core::felt252
blk0 (root):
Statements:
  (v2: core::felt252, v3: core::felt252) <- test::f(v0, v1)
  (v4: core::felt252, v5: core::felt252) <- test::g(v2, v1)
  (v6: core::felt252, v7: core::felt252, v8: core::felt252) <- test::h(v4, v1)
  (v9: core::felt252, v10: (core::felt252,)) <- test::i(v6, v1)
End:
  Return(v9)

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

//! > Test extern function enum calling.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(ref a: felt252, b: felt252) {
    let x = f(ref a, b);
    match x {
        MyEnum::A(_y) => (),
        MyEnum::B(_y) => (),
        MyEnum::C(_y) => (),
    }
}

//! > function_name
foo

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

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::felt252, v1: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1) {
    MyEnum::A(v2, v3) => blk1,
    MyEnum::B(v4, v5) => blk2,
    MyEnum::C(v6, v7, v8) => blk3,
  })

blk1:
Statements:
End:
  Return(v2)

blk2:
Statements:
End:
  Return(v4)

blk3:
Statements:
End:
  Return(v6)

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

//! > Test extern function enum calling with optimization.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(a: felt252, b: felt252) -> felt252 {
    match f(a, b) {
        MyEnum::A(y) => y,
        MyEnum::B((y,)) => y,
        MyEnum::C((y, _)) => y,
    }
}

//! > function_name
foo

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

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::felt252, v1: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1) {
    MyEnum::A(v2) => blk1,
    MyEnum::B(v3) => blk2,
    MyEnum::C(v4, v5) => blk3,
  })

blk1:
Statements:
End:
  Return(v2)

blk2:
Statements:
End:
  Return(v3)

blk3:
Statements:
End:
  Return(v4)

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

//! > Test extern function enum calling with optimization with ref.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(ref a: felt252, b: felt252) -> felt252 {
    match f(ref a, b) {
        MyEnum::A(y) => y,
        MyEnum::B((y,)) => y,
        MyEnum::C((y, _)) => y,
    }
}

//! > function_name
foo

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

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::felt252, v1: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1) {
    MyEnum::A(v2, v3) => blk1,
    MyEnum::B(v4, v5) => blk2,
    MyEnum::C(v6, v7, v8) => blk3,
  })

blk1:
Statements:
End:
  Return(v2, v3)

blk2:
Statements:
End:
  Return(v4, v5)

blk3:
Statements:
End:
  Return(v6, v7)

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

//! > Test extern function enum calling with implicits.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(ref a: felt252, b: felt252) {
    let x = f(ref a, b);
    match x {
        MyEnum::A(_y) => (),
        MyEnum::B(_y) => (),
        MyEnum::C(_y) => (),
    }
}

//! > function_name
foo

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

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::felt252, v2: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1, v2) {
    MyEnum::A(v3, v4, v5) => blk1,
    MyEnum::B(v6, v7, v8) => blk2,
    MyEnum::C(v9, v10, v11, v12) => blk3,
  })

blk1:
Statements:
End:
  Return(v3, v4)

blk2:
Statements:
End:
  Return(v6, v7)

blk3:
Statements:
End:
  Return(v9, v10)

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

//! > Test extern function enum calling with optimization with implicits.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(ref a: felt252, b: felt252) -> felt252 {
    match f(ref a, b) {
        MyEnum::A(y) => y,
        MyEnum::B((y,)) => y,
        MyEnum::C((y, _)) => y,
    }
}

//! > function_name
foo

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

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::felt252, v2: core::felt252
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1, v2) {
    MyEnum::A(v3, v4, v5) => blk1,
    MyEnum::B(v6, v7, v8) => blk2,
    MyEnum::C(v9, v10, v11, v12) => blk3,
  })

blk1:
Statements:
End:
  Return(v3, v4, v5)

blk2:
Statements:
End:
  Return(v6, v7, v8)

blk3:
Statements:
End:
  Return(v9, v10, v11)

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

//! > Test match on libfunc call that uses the refs of the libfunc in one of the arms.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo(mut arr: Array<felt252>, mut b: (felt252,)) -> Array<felt252> {
    let y = match f(ref arr, ref b) {
        MyEnum::A(_x) => arr,
    };
    y
}

//! > function_name
foo

//! > module_code
enum MyEnum {
    A: felt252,
}
extern fn f(ref arr: Array<felt252>, ref b: (felt252,)) -> MyEnum nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::array::Array::<core::felt252>, v1: (core::felt252,)
blk0 (root):
Statements:
End:
  Match(match test::f(v0, v1) {
    MyEnum::A(v2, v3, v4) => blk1,
  })

blk1:
Statements:
End:
  Return(v2)

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

//! > Test calling libfunc that returns enum but does nothing with the result.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() {
    let _unused = gas::withdraw_gas();
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
End:
  Match(match core::gas::withdraw_gas(v0, v1) {
    Option::Some(v2, v3) => blk1,
    Option::None(v4, v5) => blk2,
  })

blk1:
Statements:
  (v6: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
End:
  Return(v2, v6)

blk2:
Statements:
  (v7: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
End:
  Return(v4, v7)

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

//! > Test calling libfunc that returns enum and returns the result as a tail expression.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<()> {
    gas::withdraw_gas()
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
End:
  Match(match core::gas::withdraw_gas(v0, v1) {
    Option::Some(v2, v3) => blk1,
    Option::None(v4, v5) => blk2,
  })

blk1:
Statements:
  (v6: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
  (v7: ()) <- struct_construct()
  (v8: core::option::Option::<()>) <- Option::Some(v7)
End:
  Return(v2, v6, v8)

blk2:
Statements:
  (v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
  (v10: ()) <- struct_construct()
  (v11: core::option::Option::<()>) <- Option::None(v10)
End:
  Return(v4, v9, v11)

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

//! > Test calling libfunc that returns enum and returns the result with `return`.

//! > test_runner_name
test_function_lowering(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<()> {
    return gas::withdraw_gas();
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::gas::GasBuiltin
blk0 (root):
Statements:
End:
  Match(match core::gas::withdraw_gas(v0, v1) {
    Option::Some(v2, v3) => blk1,
    Option::None(v4, v5) => blk2,
  })

blk1:
Statements:
  (v6: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v3)
  (v7: ()) <- struct_construct()
  (v8: core::option::Option::<()>) <- Option::Some(v7)
End:
  Return(v2, v6, v8)

blk2:
Statements:
  (v9: core::gas::GasBuiltin) <- core::gas::redeposit_gas(v5)
  (v10: ()) <- struct_construct()
  (v11: core::option::Option::<()>) <- Option::None(v10)
End:
  Return(v4, v9, v11)
