2025-06-13 02:30:47 +05:00
|
|
|
//! 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
|
2019-07-27 00:54:25 +03:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
//@ run-pass
|
2012-01-02 10:20:58 +01:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
struct A {
|
|
|
|
|
_a: Box<isize>,
|
|
|
|
|
}
|
2013-01-25 22:46:32 -08:00
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2025-06-13 02:30:47 +05:00
|
|
|
fn invoke<F>(f: F)
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(),
|
|
|
|
|
{
|
|
|
|
|
f();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 02:39:40 +02:00
|
|
|
let k: Box<_> = 22.into();
|
2025-06-13 02:30:47 +05:00
|
|
|
|
|
|
|
|
// 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()));
|
2012-01-02 10:20:58 +01:00
|
|
|
}
|