summaryrefslogtreecommitdiffstats
path: root/src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs')
-rw-r--r--src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs b/src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs
new file mode 100644
index 000000000..93131b2ac
--- /dev/null
+++ b/src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.rs
@@ -0,0 +1,85 @@
+// edition:2021
+
+// Test that arrays are completely captured by closures by relying on the borrow check diagnostics
+
+fn arrays_1() {
+ let mut arr = [1, 2, 3, 4, 5];
+
+ let mut c = || {
+ arr[0] += 10;
+ };
+
+ // c will capture `arr` completely, therefore another index into the
+ // array can't be modified here
+ arr[1] += 10;
+ //~^ ERROR: cannot use `arr` because it was mutably borrowed
+ //~| ERROR: cannot use `arr[_]` because it was mutably borrowed
+ c();
+}
+
+fn arrays_2() {
+ let mut arr = [1, 2, 3, 4, 5];
+
+ let c = || {
+ println!("{:#?}", &arr[3..4]);
+ };
+
+ // c will capture `arr` completely, therefore another index into the
+ // array can't be modified here
+ arr[1] += 10;
+ //~^ ERROR: cannot assign to `arr[_]` because it is borrowed
+ c();
+}
+
+fn arrays_3() {
+ let mut arr = [1, 2, 3, 4, 5];
+
+ let c = || {
+ println!("{}", arr[3]);
+ };
+
+ // c will capture `arr` completely, therefore another index into the
+ // array can't be modified here
+ arr[1] += 10;
+ //~^ ERROR: cannot assign to `arr[_]` because it is borrowed
+ c();
+}
+
+fn arrays_4() {
+ let mut arr = [1, 2, 3, 4, 5];
+
+ let mut c = || {
+ arr[1] += 10;
+ };
+
+ // c will capture `arr` completely, therefore we cannot borrow another index
+ // into the array.
+ println!("{}", arr[3]);
+ //~^ ERROR: cannot use `arr` because it was mutably borrowed
+ //~| ERROR: cannot borrow `arr[_]` as immutable because it is also borrowed as mutable
+
+ c();
+}
+
+fn arrays_5() {
+ let mut arr = [1, 2, 3, 4, 5];
+
+ let mut c = || {
+ arr[1] += 10;
+ };
+
+ // c will capture `arr` completely, therefore we cannot borrow other indecies
+ // into the array.
+ println!("{:#?}", &arr[3..2]);
+ //~^ ERROR: cannot borrow `arr` as immutable because it is also borrowed as mutable
+
+ c();
+}
+
+fn main() {
+ arrays_1();
+ arrays_2();
+ arrays_3();
+ arrays_4();
+ arrays_5();
+}