summaryrefslogtreecommitdiffstats
path: root/tests/ui/borrowck/borrowck-closures-slice-patterns.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/borrowck/borrowck-closures-slice-patterns.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/borrowck/borrowck-closures-slice-patterns.rs')
-rw-r--r--tests/ui/borrowck/borrowck-closures-slice-patterns.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/ui/borrowck/borrowck-closures-slice-patterns.rs b/tests/ui/borrowck/borrowck-closures-slice-patterns.rs
new file mode 100644
index 000000000..32057d5c1
--- /dev/null
+++ b/tests/ui/borrowck/borrowck-closures-slice-patterns.rs
@@ -0,0 +1,82 @@
+// Check that closure captures for slice patterns are inferred correctly
+
+fn arr_by_ref(mut x: [String; 3]) {
+ let f = || {
+ let [ref y, ref z @ ..] = x;
+ };
+ let r = &mut x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn arr_by_mut(mut x: [String; 3]) {
+ let mut f = || {
+ let [ref mut y, ref mut z @ ..] = x;
+ };
+ let r = &x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn arr_by_move(x: [String; 3]) {
+ let f = || {
+ let [y, z @ ..] = x;
+ };
+ &x;
+ //~^ ERROR borrow of moved value
+}
+
+fn arr_ref_by_ref(x: &mut [String; 3]) {
+ let f = || {
+ let [ref y, ref z @ ..] = *x;
+ };
+ let r = &mut *x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn arr_ref_by_uniq(x: &mut [String; 3]) {
+ let mut f = || {
+ let [ref mut y, ref mut z @ ..] = *x;
+ };
+ let r = &x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn arr_box_by_move(x: Box<[String; 3]>) {
+ let f = || {
+ let [y, z @ ..] = *x;
+ };
+ &x;
+ //~^ ERROR borrow of moved value
+}
+
+fn slice_by_ref(x: &mut [String]) {
+ let f = || {
+ if let [ref y, ref z @ ..] = *x {}
+ };
+ let r = &mut *x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn slice_by_uniq(x: &mut [String]) {
+ let mut f = || {
+ if let [ref mut y, ref mut z @ ..] = *x {}
+ };
+ let r = &x;
+ //~^ ERROR cannot borrow
+ f();
+}
+
+fn main() {
+ arr_by_ref(Default::default());
+ arr_by_mut(Default::default());
+ arr_by_move(Default::default());
+ arr_ref_by_ref(&mut Default::default());
+ arr_ref_by_uniq(&mut Default::default());
+ arr_box_by_move(Default::default());
+ slice_by_ref(&mut <[_; 3]>::default());
+ slice_by_uniq(&mut <[_; 3]>::default());
+}