diff options
Diffstat (limited to 'tests/ui/closures/closure-upvar-last-use-analysis.rs')
| -rw-r--r-- | tests/ui/closures/closure-upvar-last-use-analysis.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/tests/ui/closures/closure-upvar-last-use-analysis.rs b/tests/ui/closures/closure-upvar-last-use-analysis.rs index 23c263c9805..2c3e349437d 100644 --- a/tests/ui/closures/closure-upvar-last-use-analysis.rs +++ b/tests/ui/closures/closure-upvar-last-use-analysis.rs @@ -1,14 +1,29 @@ -//@ run-pass +//! 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 -#![allow(dead_code)] -// Make sure #1399 stays fixed +//@ run-pass -struct A { a: Box<isize> } +struct A { + _a: Box<isize>, +} fn foo() -> Box<dyn FnMut() -> isize + 'static> { let k: Box<_> = Box::new(22); - let _u = A {a: k.clone()}; - let result = || 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) } |
