2025-06-13 02:30:47 +05:00
|
|
|
//! 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
|
2019-07-27 00:54:25 +03:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
//@ run-pass
|
2012-01-04 14:16:41 -08:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
struct A {
|
|
|
|
|
_a: Box<isize>,
|
|
|
|
|
}
|
2013-01-25 22:46:32 -08:00
|
|
|
|
2019-05-28 14:47:21 -04:00
|
|
|
fn foo() -> Box<dyn FnMut() -> isize + 'static> {
|
2015-12-02 17:31:49 -08:00
|
|
|
let k: Box<_> = Box::new(22);
|
2025-06-13 02:30:47 +05:00
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
2015-02-15 09:52:21 +01:00
|
|
|
Box::new(result)
|
2012-01-04 14:16:41 -08:00
|
|
|
}
|
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2015-12-02 17:31:49 -08:00
|
|
|
assert_eq!(foo()(), 22);
|
2012-01-04 14:16:41 -08:00
|
|
|
}
|