about summary refs log tree commit diff
path: root/tests/ui/closures/closure-capture-after-clone.rs
blob: 29fba147909b4ab7e52f190d8cb2014b690bd201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Regression test for issue #1399
//!
//! This tests that when a variable is used (via clone) and then later
//! captured by a closure, the last-use analysis doesn't incorrectly optimize
//! the earlier use as a "last use" and perform an invalid move.
//!
//! The sequence being tested:
//! 1. Create variable `k`
//! 2. Use `k.clone()` for some purpose
//! 3. Later capture `k` in a closure
//!
//! The analysis must not treat step 2 as the "last use" since step 3 needs `k`.
//!
//! See: https://github.com/rust-lang/rust/issues/1399

//@ run-pass

struct A {
    _a: Box<isize>,
}

pub fn main() {
    fn invoke<F>(f: F)
    where
        F: FnOnce(),
    {
        f();
    }

    let k: Box<_> = 22.into();

    // This clone should NOT be treated as "last use" of k
    // even though k is not used again until the closure
    let _u = A { _a: k.clone() };

    // Here k is actually captured by the closure
    // The last-use analyzer must have accounted for this when processing the clone above
    invoke(|| println!("{}", k.clone()));
}