Skip to content

Commit 926759c

Browse files
committed
moved renamed docs formatted | last-use-in-cap-clause.rs
1 parent 7fc3d3b commit 926759c

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Regression test for issue #1399
2+
//!
3+
//! This test ensures that the compiler's last-use analysis correctly handles variables
4+
//! that are captured by closures (upvars). The original issue was that the analyzer
5+
//! would incorrectly optimize variable usage as "last use" and perform moves, even when
6+
//! the variable was later needed by a closure that captured it.
7+
//!
8+
//! See: https://github.com/rust-lang/rust/issues/1399
9+
10+
//@ run-pass
11+
12+
struct A {
13+
_a: Box<isize>
14+
}
15+
16+
fn foo() -> Box<dyn FnMut() -> isize + 'static> {
17+
let k: Box<_> = Box::new(22);
18+
19+
// This use of k.clone() should not be treated as a "last use"
20+
// even though the closure below doesn't actually capture k
21+
let _u = A { _a: k.clone() };
22+
23+
// The closure doesn't actually use k, but the analyzer needs to handle
24+
// the potential capture scenario correctly
25+
let result = || 22;
26+
27+
Box::new(result)
28+
}
29+
30+
pub fn main() {
31+
assert_eq!(foo()(), 22);
32+
}

tests/ui/last-use-in-cap-clause.rs

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)