2025-06-13 02:30:47 +05:00
|
|
|
//! Regression test for issue #1818
|
|
|
|
|
//! last-use analysis in closures should allow moves instead of requiring copies.
|
|
|
|
|
//!
|
|
|
|
|
//! The original issue was that the compiler incorrectly flagged certain return values
|
|
|
|
|
//! in anonymous functions/closures as requiring copies of non-copyable values, when
|
|
|
|
|
//! they should have been treated as moves (since they were the last use of the value).
|
|
|
|
|
//!
|
|
|
|
|
//! See: https://github.com/rust-lang/rust/issues/1818
|
2012-02-13 14:47:36 +01:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
//@ run-pass
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
fn apply<T, F>(s: String, mut f: F) -> T
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(String) -> T
|
|
|
|
|
{
|
|
|
|
|
fn g<T, F>(s: String, mut f: F) -> T
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(String) -> T
|
|
|
|
|
{
|
|
|
|
|
f(s)
|
2012-02-13 14:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
g(s, |v| {
|
|
|
|
|
let r = f(v);
|
|
|
|
|
r // This should be a move, not requiring copy
|
|
|
|
|
})
|
2012-02-13 14:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 02:30:47 +05:00
|
|
|
pub fn main() {
|
|
|
|
|
// Actually test the functionality
|
|
|
|
|
let result = apply(String::from("test"), |s| s.len());
|
|
|
|
|
assert_eq!(result, 4);
|
|
|
|
|
}
|