//! > unused captures

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(a: u32, mut b: Array<u32>, mut c: Felt252Dict<felt252>) {
    |d: u32| {
        let _ = a + d;
    };
    |d: u32| {
        let _ = b.append(d);
    };
    |d: felt252| {
        let _ = c.insert(d, d);
    };
}

//! > function_name
foo

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:6:17
        let _ = b.append(d);
                ^

error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:9:17
        let _ = c.insert(d, d);
                ^

//! > lowering_diagnostics

//! > lowering

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

//! > Panic destructable capture

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(a: PanicDestructable) {
    || {
        let PanicDestructable { } = a;
    };
    panic_with_felt252('Panic');
}

//! > function_name
foo

//! > module_code
struct PanicDestructable {}

impl MyPanicDestruct of PanicDestruct<PanicDestructable> {
    fn panic_destruct(self: PanicDestructable, ref panic: Panic) nopanic {
        let PanicDestructable { } = self;
    }
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters: v0: test::PanicDestructable
blk0 (root):
Statements:
  (v1: {closure@lib.cairo:9:5: 9:7}) <- struct_construct(v0{`a`})
  (v2: core::felt252) <- 345232009571
  (v3: core::never) <- core::panic_with_felt252(v2{`'Panic'`})
End:
  Match(match_enum(v3{`panic_with_felt252('Panic')`}) {
  })

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

//! > invalidated capture mutable variable

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(mut a: Array<u32>) {
    |b: u32| {
        let _ = a.append(b);
    };
    a.append(0);
}

//! > function_name
foo

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:3:17
        let _ = a.append(b);
                ^

//! > lowering_diagnostics

//! > lowering

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

//! > Use of captured var.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(a: S) {
    || {
        use_s(a)
    };

    // TODO(ilya): Consider adding automatic destructuring of the closure before the following line
    let _ = a.a;
}

//! > function_name
foo

//! > module_code
#[derive(Drop)]
struct S {
    a: u32,
}

extern fn use_s(s: S) nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics
error[E3001]: Variable was previously moved.
 --> lib.cairo:13:13
    let _ = a.a;
            ^^^
note: variable was previously used here:
  --> lib.cairo:9:15
        use_s(a)
              ^
note: Trait has no implementation in context: core::traits::Copy::<test::S>.

//! > lowering
Parameters: v0: test::S
blk0 (root):
Statements:
  (v1: {closure@lib.cairo:8:5: 8:7}) <- struct_construct(v0{`a`})
  (v3: ()) <- struct_construct()
End:
  Return(v3)

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

//! > Use of captured mutable variable.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(mut a: Array<u32>) {
    let f = |b: u32| {
        let _ = a.append(b);
    };
    a.append(0);
    use_f(f)
}

//! > function_name
foo

//! > module_code
extern fn use_f<T>(f: T) nopanic;

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:4:17
        let _ = a.append(b);
                ^

//! > lowering_diagnostics

//! > lowering

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

//! > Snapshot capture

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(a: Array<u32>) {
    let f = |_b: u32| {
        let _ = a.len();
    };
    // Since only a snapshot of `a` is capture, we can use it while it is captured.
    use_value(a);

    use_value(f);
}

//! > function_name
foo

//! > module_code
extern fn use_value<T>(f: T) nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters: v0: core::array::Array::<core::integer::u32>
blk0 (root):
Statements:
  (v1: core::array::Array::<core::integer::u32>, v2: @core::array::Array::<core::integer::u32>) <- snapshot(v0{`a`})
  (v3: {closure@lib.cairo:3:13: 3:22}) <- struct_construct(v2{`|_b: u32| { let _ = a.len(); }`})
  () <- test::use_value::<core::array::Array::<core::integer::u32>>(v1{`a`})
  () <- test::use_value::<{closure@lib.cairo:3:13: 3:22}>(v3{`f`})
  (v4: ()) <- struct_construct()
End:
  Return(v4)

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

//! > Capture changed but unused variable.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(mut a: Array<u32>) {
    |_b: u32| {
        a = array![];
    };
    a.append(0);
}

//! > function_name
foo

//! > module_code
extern fn use_f<T>(f: T) nopanic;

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:4:9
        a = array![];
        ^

//! > lowering_diagnostics

//! > lowering

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

//! > Test using captured copiable variable after closure.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo() -> felt252 {
    let x = 8;
    let c = |a| {
        x * (a + 3)
    };
    let y = c(2);
    y + x
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters:
blk0 (root):
Statements:
  (v0: core::felt252) <- 8
  (v1: {closure@lib.cairo:3:13: 3:16}) <- struct_construct(v0{`x`})
  (v2: {closure@lib.cairo:3:13: 3:16}, v3: @{closure@lib.cairo:3:13: 3:16}) <- snapshot(v1{`c`})
  (v4: core::felt252) <- 2
  (v5: (core::felt252,)) <- struct_construct(v4{`2`})
  (v6: core::felt252) <- Generated `core::ops::function::Fn::call` for {closure@lib.cairo:3:13: 3:16}(v3{`c`}, v5{`c(2)`})
  (v7: core::felt252) <- core::Felt252Add::add(v6{`y`}, v0{`x`})
End:
  Return(v7)

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

//! > Test using captured snapshotted variable after closure.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo() -> felt252 {
    let x = array![99_felt252];
    let c = |a| {
        (@x).len() * (a + 3)
    };
    let _ = c(2);
    *x[0]
}

//! > function_name
foo

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters:
blk0 (root):
Statements:
  (v0: core::array::Array::<core::felt252>) <- core::array::ArrayImpl::<core::felt252>::new()
  (v1: core::felt252) <- 99
  (v3: core::array::Array::<core::felt252>, v2: ()) <- core::array::ArrayImpl::<core::felt252>::append(v0{`__array_builder_macro_result__`}, v1{`99_felt252`})
  (v4: core::array::Array::<core::felt252>, v5: @core::array::Array::<core::felt252>) <- snapshot(v3{`x`})
  (v6: {closure@lib.cairo:3:13: 3:16}) <- struct_construct(v5{`|a| { (@x).len() * (a + 3) }`})
  (v7: {closure@lib.cairo:3:13: 3:16}, v8: @{closure@lib.cairo:3:13: 3:16}) <- snapshot(v6{`c`})
  (v9: core::integer::u32) <- 2
  (v10: (core::integer::u32,)) <- struct_construct(v9{`2`})
  (v11: core::integer::u32) <- Generated `core::ops::function::Fn::call` for {closure@lib.cairo:3:13: 3:16}(v8{`c`}, v10{`c(2)`})
  (v12: core::array::Array::<core::felt252>, v13: @core::array::Array::<core::felt252>) <- snapshot(v4{`x`})
  (v14: core::integer::u32) <- 0
  (v15: @core::felt252) <- core::ops::index::DeprecatedIndexViewImpl::<core::array::Array::<core::felt252>, core::integer::u32, @core::felt252, core::array::ArrayIndex::<core::felt252>>::index(v13{`x`}, v14{`0`})
  (v16: core::felt252) <- desnap(v15{`0`})
End:
  Return(v16)

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

//! > Test mutating captured copiable variable before closure.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo() -> felt252 {
    let mut x = 8;
    let c = |a| {
        x * (a + 3)
    };
    x = 9;
    let y = c(2);
    y + x
}

//! > function_name
foo

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:4:9
        x * (a + 3)
        ^

//! > lowering_diagnostics

//! > lowering

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

//! > Test mutating captured copiable variable before closure with indirection.

//! > test_runner_name
test_borrow_check

//! > function_code
fn identity<T>(x: T) -> T {
    x
}
fn foo() {
    let mut x = 8;
    let c = |a| {
        x * (a + 3)
    };
    let c2 = identity(c);
    x = 9;
    let y = c2(2);
}

//! > function_name
foo

//! > semantic_diagnostics
error[E2188]: Capture of mutable variables in a closure is not supported
 --> lib.cairo:7:9
        x * (a + 3)
        ^

warning[E0001]: Unused variable. Consider ignoring by prefixing with `_`.
 --> lib.cairo:11:9
    let y = c2(2);
        ^

//! > lowering_diagnostics

//! > lowering

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

//! > capturing two destructables variables of the same type.

//! > test_runner_name
test_borrow_check

//! > function_code
fn foo(a: Destructable, b: Destructable) {
    let c = || {
        let Destructable { } = a;
        let Destructable { } = b;
    };
    c.destruct();
}

//! > function_name
foo

//! > module_code
struct Destructable {}

impl MyDestruct of Destruct<Destructable> {
    fn destruct(self: Destructable) nopanic {
        let Destructable { } = self;
    }
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > lowering
Parameters: v0: test::Destructable, v1: test::Destructable
blk0 (root):
Statements:
  (v2: {closure@lib.cairo:9:13: 9:15}) <- struct_construct(v0{`a`}, v1{`b`})
  (v3: ()) <- Generated `core::traits::Destruct::destruct` for {closure@lib.cairo:9:13: 9:15}(v2{`c`})
  (v4: ()) <- struct_construct()
End:
  Return(v4)
