summaryrefslogtreecommitdiffstats
path: root/tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs')
-rw-r--r--tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs b/tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs
new file mode 100644
index 000000000..9f0c4d96a
--- /dev/null
+++ b/tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs
@@ -0,0 +1,54 @@
+// edition:2021
+// run-pass
+
+// Test that we can mutate a place through a mut-borrow
+// that is captured by the closure
+
+// Check that we can mutate when one deref is required
+fn mut_ref_1() {
+ let mut x = String::new();
+ let rx = &mut x;
+
+ let mut c = || {
+ *rx = String::new();
+ };
+
+ c();
+}
+
+// Similar example as mut_ref_1, we don't deref the imm-borrow here,
+// and so we are allowed to mutate.
+fn mut_ref_2() {
+ let x = String::new();
+ let y = String::new();
+ let mut ref_x = &x;
+ let m_ref_x = &mut ref_x;
+
+ let mut c = || {
+ *m_ref_x = &y;
+ };
+
+ c();
+}
+
+// Check that we can mutate when multiple derefs of mut-borrows are required to reach
+// the target place.
+// It works because all derefs are mutable, if either of them was an immutable
+// borrow, then we would not be able to deref.
+fn mut_mut_ref() {
+ let mut x = String::new();
+ let mut mref_x = &mut x;
+ let m_mref_x = &mut mref_x;
+
+ let mut c = || {
+ **m_mref_x = String::new();
+ };
+
+ c();
+}
+
+fn main() {
+ mut_ref_1();
+ mut_ref_2();
+ mut_mut_ref();
+}