about summary refs log tree commit diff
path: root/tests/ui/closures
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/closures')
-rw-r--r--tests/ui/closures/aliasability-violation-with-closure-21600.rs19
-rw-r--r--tests/ui/closures/aliasability-violation-with-closure-21600.stderr31
2 files changed, 50 insertions, 0 deletions
diff --git a/tests/ui/closures/aliasability-violation-with-closure-21600.rs b/tests/ui/closures/aliasability-violation-with-closure-21600.rs
new file mode 100644
index 00000000000..d4c658319ab
--- /dev/null
+++ b/tests/ui/closures/aliasability-violation-with-closure-21600.rs
@@ -0,0 +1,19 @@
+// https://github.com/rust-lang/rust/issues/21600
+fn call_it<F>(f: F) where F: Fn() { f(); }
+
+struct A;
+
+impl A {
+    fn gen(&self) {}
+    fn gen_mut(&mut self) {}
+}
+
+fn main() {
+    let mut x = A;
+    call_it(|| {
+        call_it(|| x.gen());
+        call_it(|| x.gen_mut());
+        //~^ ERROR cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
+        //~| ERROR cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
+    });
+}
diff --git a/tests/ui/closures/aliasability-violation-with-closure-21600.stderr b/tests/ui/closures/aliasability-violation-with-closure-21600.stderr
new file mode 100644
index 00000000000..2d2397a2141
--- /dev/null
+++ b/tests/ui/closures/aliasability-violation-with-closure-21600.stderr
@@ -0,0 +1,31 @@
+error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
+  --> $DIR/aliasability-violation-with-closure-21600.rs:15:20
+   |
+LL | fn call_it<F>(f: F) where F: Fn() { f(); }
+   |                  - change this to accept `FnMut` instead of `Fn`
+...
+LL |         call_it(|| x.gen_mut());
+   |         ------- -- ^ cannot borrow as mutable
+   |         |       |
+   |         |       in this closure
+   |         expects `Fn` instead of `FnMut`
+
+error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
+  --> $DIR/aliasability-violation-with-closure-21600.rs:15:17
+   |
+LL | fn call_it<F>(f: F) where F: Fn() { f(); }
+   |                  - change this to accept `FnMut` instead of `Fn`
+...
+LL |     call_it(|| {
+   |     ------- -- in this closure
+   |     |
+   |     expects `Fn` instead of `FnMut`
+LL |         call_it(|| x.gen());
+LL |         call_it(|| x.gen_mut());
+   |                 ^^ - mutable borrow occurs due to use of `x` in closure
+   |                 |
+   |                 cannot borrow as mutable
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0596`.