Skip to content

Commit 9c787a1

Browse files
committed
moved renamed docs formatted | last-use-is-capture.rs
1 parent 926759c commit 9c787a1

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Regression test for issue #1399
2+
//!
3+
//! This test ensures that when a variable is used (via clone) and then later
4+
//! captured by a closure, the last-use analyzer doesn't incorrectly optimize
5+
//! the earlier use as a "last use" and perform an invalid move.
6+
//!
7+
//! The sequence being tested:
8+
//! 1. Create variable `k`
9+
//! 2. Use `k.clone()` for some purpose
10+
//! 3. Later capture `k` in a closure
11+
//!
12+
//! The analyzer must not treat step 2 as the "last use" since step 3 needs `k`.
13+
//!
14+
//! See: https://github.com/rust-lang/rust/issues/1399
15+
16+
//@ run-pass
17+
18+
struct A {
19+
_a: Box<isize>
20+
}
21+
22+
pub fn main() {
23+
fn invoke<F>(f: F)
24+
where
25+
F: FnOnce()
26+
{
27+
f();
28+
}
29+
30+
let k: Box<_> = 22.into();
31+
32+
// This clone should NOT be treated as "last use" of k
33+
// even though k is not used again until the closure
34+
let _u = A { _a: k.clone() };
35+
36+
// Here k is actually captured by the closure
37+
// The last-use analyzer must have accounted for this when processing the clone above
38+
invoke(|| println!("{}", k.clone()));
39+
}

tests/ui/closures/closure-last-use-move.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
1010
//@ run-pass
1111

12-
fn apply<T, F>(s: String, mut f: F) -> T
13-
where
14-
F: FnMut(String) -> T
12+
fn apply<T, F>(s: String, mut f: F) -> T
13+
where
14+
F: FnMut(String) -> T
1515
{
16-
fn g<T, F>(s: String, mut f: F) -> T
17-
where
18-
F: FnMut(String) -> T
16+
fn g<T, F>(s: String, mut f: F) -> T
17+
where
18+
F: FnMut(String) -> T
1919
{
2020
f(s)
2121
}
22-
22+
2323
g(s, |v| {
2424
let r = f(v);
2525
r // This should be a move, not requiring copy

tests/ui/closures/closure-upvar-last-use-analysis.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ struct A {
1515

1616
fn foo() -> Box<dyn FnMut() -> isize + 'static> {
1717
let k: Box<_> = Box::new(22);
18-
19-
// This use of k.clone() should not be treated as a "last use"
18+
19+
// This use of k.clone() should not be treated as a "last use"
2020
// even though the closure below doesn't actually capture k
2121
let _u = A { _a: k.clone() };
22-
22+
2323
// The closure doesn't actually use k, but the analyzer needs to handle
2424
// the potential capture scenario correctly
2525
let result = || 22;
26-
26+
2727
Box::new(result)
2828
}
2929

tests/ui/last-use-is-capture.rs

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

0 commit comments

Comments
 (0)