From 218caa410aa38c29984be31a5229b9fa717560ee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:13 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- .../unboxed-closures-counter-not-moved.rs | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs (limited to 'tests/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs') diff --git a/tests/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs b/tests/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs new file mode 100644 index 000000000..390386e57 --- /dev/null +++ b/tests/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs @@ -0,0 +1,28 @@ +// run-pass +// Test that we mutate a counter on the stack only when we expect to. + +fn call(f: F) where F : FnOnce() { + f(); +} + +fn main() { + let y = vec![format!("Hello"), format!("World")]; + let mut counter = 22_u32; + + call(|| { + // Move `y`, but do not move `counter`, even though it is read + // by value (note that it is also mutated). + for item in y { //~ WARN unused variable: `item` + let v = counter; + counter += v; + } + }); + assert_eq!(counter, 88); + + call(move || { + // this mutates a moved copy, and hence doesn't affect original + counter += 1; //~ WARN value assigned to `counter` is never read + //~| WARN unused variable: `counter` + }); + assert_eq!(counter, 88); +} -- cgit v1.2.3