about summary refs log tree commit diff
path: root/tests/ui/closures/closure-upvar-last-use-analysis.rs
blob: 2c3e349437dc1b8825512ea004d17d484e9aa32e (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
//! Regression test for issue #1399
//!
//! This tests that the compiler's last-use analysis correctly handles variables
//! that are captured by closures (upvars). The original issue was that the analysis
//! would incorrectly optimize variable usage as "last use" and perform moves, even when
//! the variable was later needed by a closure that captured it.
//!
//! See: https://github.com/rust-lang/rust/issues/1399

//@ run-pass

struct A {
    _a: Box<isize>,
}

fn foo() -> Box<dyn FnMut() -> isize + 'static> {
    let k: Box<_> = Box::new(22);

    // This use of k.clone() should not be treated as a "last use"
    // even though the closure below doesn't actually capture k
    let _u = A { _a: k.clone() };

    // The closure doesn't actually use k, but the analyzer needs to handle
    // the potential capture scenario correctly
    let result = || 22;

    Box::new(result)
}

pub fn main() {
    assert_eq!(foo()(), 22);
}