about summary refs log tree commit diff
path: root/tests/ui/mut
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/mut
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/mut')
-rw-r--r--tests/ui/mut/mut-cant-alias.rs14
-rw-r--r--tests/ui/mut/mut-cant-alias.stderr13
-rw-r--r--tests/ui/mut/mut-cross-borrowing.rs8
-rw-r--r--tests/ui/mut/mut-cross-borrowing.stderr21
-rw-r--r--tests/ui/mut/mut-pattern-internal-mutability.rs15
-rw-r--r--tests/ui/mut/mut-pattern-internal-mutability.stderr25
-rw-r--r--tests/ui/mut/mut-pattern-mismatched.rs20
-rw-r--r--tests/ui/mut/mut-pattern-mismatched.stderr27
-rw-r--r--tests/ui/mut/mut-ref.rs4
-rw-r--r--tests/ui/mut/mut-ref.stderr8
-rw-r--r--tests/ui/mut/mut-suggestion.rs22
-rw-r--r--tests/ui/mut/mut-suggestion.stderr25
-rw-r--r--tests/ui/mut/mutable-class-fields-2.rs24
-rw-r--r--tests/ui/mut/mutable-class-fields-2.stderr14
-rw-r--r--tests/ui/mut/mutable-class-fields.rs16
-rw-r--r--tests/ui/mut/mutable-class-fields.stderr14
-rw-r--r--tests/ui/mut/mutable-enum-indirect.rs19
-rw-r--r--tests/ui/mut/mutable-enum-indirect.stderr24
-rw-r--r--tests/ui/mut/no-mut-lint-for-desugared-mut.rs8
19 files changed, 321 insertions, 0 deletions
diff --git a/tests/ui/mut/mut-cant-alias.rs b/tests/ui/mut/mut-cant-alias.rs
new file mode 100644
index 00000000000..9146b931a19
--- /dev/null
+++ b/tests/ui/mut/mut-cant-alias.rs
@@ -0,0 +1,14 @@
+use std::cell::RefCell;
+
+
+
+fn main() {
+    let m = RefCell::new(0);
+    let mut b = m.borrow_mut();
+    let b1 = &mut *b;
+    let b2 = &mut *b; //~ ERROR cannot borrow
+    b1.use_mut();
+}
+
+trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
+impl<T> Fake for T { }
diff --git a/tests/ui/mut/mut-cant-alias.stderr b/tests/ui/mut/mut-cant-alias.stderr
new file mode 100644
index 00000000000..6046c076f2e
--- /dev/null
+++ b/tests/ui/mut/mut-cant-alias.stderr
@@ -0,0 +1,13 @@
+error[E0499]: cannot borrow `b` as mutable more than once at a time
+  --> $DIR/mut-cant-alias.rs:9:20
+   |
+LL |     let b1 = &mut *b;
+   |                    - first mutable borrow occurs here
+LL |     let b2 = &mut *b;
+   |                    ^ second mutable borrow occurs here
+LL |     b1.use_mut();
+   |     ------------ first borrow later used here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0499`.
diff --git a/tests/ui/mut/mut-cross-borrowing.rs b/tests/ui/mut/mut-cross-borrowing.rs
new file mode 100644
index 00000000000..080faab7326
--- /dev/null
+++ b/tests/ui/mut/mut-cross-borrowing.rs
@@ -0,0 +1,8 @@
+fn f(_: &mut isize) {}
+
+fn main() {
+
+    let mut x: Box<_> = Box::new(3);
+
+    f(x)    //~ ERROR mismatched types
+}
diff --git a/tests/ui/mut/mut-cross-borrowing.stderr b/tests/ui/mut/mut-cross-borrowing.stderr
new file mode 100644
index 00000000000..ee739d6286a
--- /dev/null
+++ b/tests/ui/mut/mut-cross-borrowing.stderr
@@ -0,0 +1,21 @@
+error[E0308]: mismatched types
+  --> $DIR/mut-cross-borrowing.rs:7:7
+   |
+LL |     f(x)
+   |     - ^
+   |     | |
+   |     | expected `&mut isize`, found struct `Box`
+   |     | help: consider mutably borrowing here: `&mut x`
+   |     arguments to this function are incorrect
+   |
+   = note: expected mutable reference `&mut isize`
+                         found struct `Box<{integer}>`
+note: function defined here
+  --> $DIR/mut-cross-borrowing.rs:1:4
+   |
+LL | fn f(_: &mut isize) {}
+   |    ^ -------------
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/mut/mut-pattern-internal-mutability.rs b/tests/ui/mut/mut-pattern-internal-mutability.rs
new file mode 100644
index 00000000000..bcee878e389
--- /dev/null
+++ b/tests/ui/mut/mut-pattern-internal-mutability.rs
@@ -0,0 +1,15 @@
+fn main() {
+    let foo = &mut 1;
+
+    let &mut x = foo;
+    x += 1; //~ ERROR cannot assign twice to immutable variable `x`
+
+    // explicitly mut-ify internals
+    let &mut mut x = foo;
+    x += 1;
+
+    // check borrowing is detected successfully
+    let &mut ref x = foo;
+    *foo += 1; //~ ERROR cannot assign to `*foo` because it is borrowed
+    drop(x);
+}
diff --git a/tests/ui/mut/mut-pattern-internal-mutability.stderr b/tests/ui/mut/mut-pattern-internal-mutability.stderr
new file mode 100644
index 00000000000..6583546aa5c
--- /dev/null
+++ b/tests/ui/mut/mut-pattern-internal-mutability.stderr
@@ -0,0 +1,25 @@
+error[E0384]: cannot assign twice to immutable variable `x`
+  --> $DIR/mut-pattern-internal-mutability.rs:5:5
+   |
+LL |     let &mut x = foo;
+   |              -
+   |              |
+   |              first assignment to `x`
+   |              help: consider making this binding mutable: `mut x`
+LL |     x += 1;
+   |     ^^^^^^ cannot assign twice to immutable variable
+
+error[E0506]: cannot assign to `*foo` because it is borrowed
+  --> $DIR/mut-pattern-internal-mutability.rs:13:5
+   |
+LL |     let &mut ref x = foo;
+   |              ----- borrow of `*foo` occurs here
+LL |     *foo += 1;
+   |     ^^^^^^^^^ assignment to borrowed `*foo` occurs here
+LL |     drop(x);
+   |          - borrow later used here
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0384, E0506.
+For more information about an error, try `rustc --explain E0384`.
diff --git a/tests/ui/mut/mut-pattern-mismatched.rs b/tests/ui/mut/mut-pattern-mismatched.rs
new file mode 100644
index 00000000000..700261fe40b
--- /dev/null
+++ b/tests/ui/mut/mut-pattern-mismatched.rs
@@ -0,0 +1,20 @@
+fn main() {
+    let foo = &mut 1;
+
+    // (separate lines to ensure the spans are accurate)
+
+     let &_ //~  ERROR mismatched types
+            //~| expected mutable reference `&mut {integer}`
+            //~| found reference `&_`
+            //~| types differ in mutability
+        = foo;
+    let &mut _ = foo;
+
+    let bar = &1;
+    let &_ = bar;
+    let &mut _ //~  ERROR mismatched types
+               //~| expected reference `&{integer}`
+               //~| found mutable reference `&mut _`
+               //~| types differ in mutability
+         = bar;
+}
diff --git a/tests/ui/mut/mut-pattern-mismatched.stderr b/tests/ui/mut/mut-pattern-mismatched.stderr
new file mode 100644
index 00000000000..cad1cef5155
--- /dev/null
+++ b/tests/ui/mut/mut-pattern-mismatched.stderr
@@ -0,0 +1,27 @@
+error[E0308]: mismatched types
+  --> $DIR/mut-pattern-mismatched.rs:6:10
+   |
+LL |      let &_
+   |          ^^ types differ in mutability
+...
+LL |         = foo;
+   |           --- this expression has type `&mut {integer}`
+   |
+   = note: expected mutable reference `&mut {integer}`
+                      found reference `&_`
+
+error[E0308]: mismatched types
+  --> $DIR/mut-pattern-mismatched.rs:15:9
+   |
+LL |     let &mut _
+   |         ^^^^^^ types differ in mutability
+...
+LL |          = bar;
+   |            --- this expression has type `&{integer}`
+   |
+   = note:      expected reference `&{integer}`
+           found mutable reference `&mut _`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/mut/mut-ref.rs b/tests/ui/mut/mut-ref.rs
new file mode 100644
index 00000000000..80990b2bfde
--- /dev/null
+++ b/tests/ui/mut/mut-ref.rs
@@ -0,0 +1,4 @@
+fn main() {
+    let mut ref x = 10; //~ ERROR the order of `mut` and `ref` is incorrect
+    let ref mut y = 11;
+}
diff --git a/tests/ui/mut/mut-ref.stderr b/tests/ui/mut/mut-ref.stderr
new file mode 100644
index 00000000000..e6d4901aafb
--- /dev/null
+++ b/tests/ui/mut/mut-ref.stderr
@@ -0,0 +1,8 @@
+error: the order of `mut` and `ref` is incorrect
+  --> $DIR/mut-ref.rs:2:9
+   |
+LL |     let mut ref x = 10;
+   |         ^^^^^^^ help: try switching the order: `ref mut`
+
+error: aborting due to previous error
+
diff --git a/tests/ui/mut/mut-suggestion.rs b/tests/ui/mut/mut-suggestion.rs
new file mode 100644
index 00000000000..8c269d1e727
--- /dev/null
+++ b/tests/ui/mut/mut-suggestion.rs
@@ -0,0 +1,22 @@
+#[derive(Copy, Clone)]
+struct S;
+
+impl S {
+    fn mutate(&mut self) {
+    }
+}
+
+fn func(arg: S) {
+    //~^ HELP consider changing this to be mutable
+    //~| SUGGESTION mut
+    arg.mutate();
+    //~^ ERROR cannot borrow `arg` as mutable, as it is not declared as mutable
+}
+
+fn main() {
+    let local = S;
+    //~^ HELP consider changing this to be mutable
+    //~| SUGGESTION mut
+    local.mutate();
+    //~^ ERROR cannot borrow `local` as mutable, as it is not declared as mutable
+}
diff --git a/tests/ui/mut/mut-suggestion.stderr b/tests/ui/mut/mut-suggestion.stderr
new file mode 100644
index 00000000000..d89c8b41304
--- /dev/null
+++ b/tests/ui/mut/mut-suggestion.stderr
@@ -0,0 +1,25 @@
+error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable
+  --> $DIR/mut-suggestion.rs:12:5
+   |
+LL |     arg.mutate();
+   |     ^^^^^^^^^^^^ cannot borrow as mutable
+   |
+help: consider changing this to be mutable
+   |
+LL | fn func(mut arg: S) {
+   |         +++
+
+error[E0596]: cannot borrow `local` as mutable, as it is not declared as mutable
+  --> $DIR/mut-suggestion.rs:20:5
+   |
+LL |     local.mutate();
+   |     ^^^^^^^^^^^^^^ cannot borrow as mutable
+   |
+help: consider changing this to be mutable
+   |
+LL |     let mut local = S;
+   |         +++
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0596`.
diff --git a/tests/ui/mut/mutable-class-fields-2.rs b/tests/ui/mut/mutable-class-fields-2.rs
new file mode 100644
index 00000000000..30e54dfc9cd
--- /dev/null
+++ b/tests/ui/mut/mutable-class-fields-2.rs
@@ -0,0 +1,24 @@
+struct Cat {
+  meows : usize,
+
+  how_hungry : isize,
+}
+
+impl Cat {
+  pub fn eat(&self) {
+    self.how_hungry -= 5; //~ ERROR cannot assign
+  }
+
+}
+
+fn cat(in_x : usize, in_y : isize) -> Cat {
+    Cat {
+        meows: in_x,
+        how_hungry: in_y
+    }
+}
+
+fn main() {
+  let nyan : Cat = cat(52, 99);
+  nyan.eat();
+}
diff --git a/tests/ui/mut/mutable-class-fields-2.stderr b/tests/ui/mut/mutable-class-fields-2.stderr
new file mode 100644
index 00000000000..c53c6ea302c
--- /dev/null
+++ b/tests/ui/mut/mutable-class-fields-2.stderr
@@ -0,0 +1,14 @@
+error[E0594]: cannot assign to `self.how_hungry`, which is behind a `&` reference
+  --> $DIR/mutable-class-fields-2.rs:9:5
+   |
+LL |     self.how_hungry -= 5;
+   |     ^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written
+   |
+help: consider changing this to be a mutable reference
+   |
+LL |   pub fn eat(&mut self) {
+   |              ~~~~~~~~~
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0594`.
diff --git a/tests/ui/mut/mutable-class-fields.rs b/tests/ui/mut/mutable-class-fields.rs
new file mode 100644
index 00000000000..30768a1ec9b
--- /dev/null
+++ b/tests/ui/mut/mutable-class-fields.rs
@@ -0,0 +1,16 @@
+struct Cat {
+  meows : usize,
+  how_hungry : isize,
+}
+
+fn cat(in_x : usize, in_y : isize) -> Cat {
+    Cat {
+        meows: in_x,
+        how_hungry: in_y
+    }
+}
+
+fn main() {
+  let nyan : Cat = cat(52, 99);
+  nyan.how_hungry = 0; //~ ERROR cannot assign
+}
diff --git a/tests/ui/mut/mutable-class-fields.stderr b/tests/ui/mut/mutable-class-fields.stderr
new file mode 100644
index 00000000000..1d731be8a85
--- /dev/null
+++ b/tests/ui/mut/mutable-class-fields.stderr
@@ -0,0 +1,14 @@
+error[E0594]: cannot assign to `nyan.how_hungry`, as `nyan` is not declared as mutable
+  --> $DIR/mutable-class-fields.rs:15:3
+   |
+LL |   nyan.how_hungry = 0;
+   |   ^^^^^^^^^^^^^^^^^^^ cannot assign
+   |
+help: consider changing this to be mutable
+   |
+LL |   let mut nyan : Cat = cat(52, 99);
+   |       +++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0594`.
diff --git a/tests/ui/mut/mutable-enum-indirect.rs b/tests/ui/mut/mutable-enum-indirect.rs
new file mode 100644
index 00000000000..502859c0413
--- /dev/null
+++ b/tests/ui/mut/mutable-enum-indirect.rs
@@ -0,0 +1,19 @@
+// Tests that an `&` pointer to something inherently mutable is itself
+// to be considered mutable.
+
+#![feature(negative_impls)]
+
+use std::marker::Sync;
+
+struct NoSync;
+impl !Sync for NoSync {}
+
+enum Foo { A(NoSync) }
+
+fn bar<T: Sync>(_: T) {}
+
+fn main() {
+    let x = Foo::A(NoSync);
+    bar(&x);
+    //~^ ERROR `NoSync` cannot be shared between threads safely [E0277]
+}
diff --git a/tests/ui/mut/mutable-enum-indirect.stderr b/tests/ui/mut/mutable-enum-indirect.stderr
new file mode 100644
index 00000000000..9e1f4e1fe4e
--- /dev/null
+++ b/tests/ui/mut/mutable-enum-indirect.stderr
@@ -0,0 +1,24 @@
+error[E0277]: `NoSync` cannot be shared between threads safely
+  --> $DIR/mutable-enum-indirect.rs:17:9
+   |
+LL |     bar(&x);
+   |     --- ^^ `NoSync` cannot be shared between threads safely
+   |     |
+   |     required by a bound introduced by this call
+   |
+   = help: within `&Foo`, the trait `Sync` is not implemented for `NoSync`
+note: required because it appears within the type `Foo`
+  --> $DIR/mutable-enum-indirect.rs:11:6
+   |
+LL | enum Foo { A(NoSync) }
+   |      ^^^
+   = note: required because it appears within the type `&Foo`
+note: required by a bound in `bar`
+  --> $DIR/mutable-enum-indirect.rs:13:11
+   |
+LL | fn bar<T: Sync>(_: T) {}
+   |           ^^^^ required by this bound in `bar`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/tests/ui/mut/no-mut-lint-for-desugared-mut.rs b/tests/ui/mut/no-mut-lint-for-desugared-mut.rs
new file mode 100644
index 00000000000..419d580419f
--- /dev/null
+++ b/tests/ui/mut/no-mut-lint-for-desugared-mut.rs
@@ -0,0 +1,8 @@
+// run-pass
+
+#![deny(unused_mut)]
+#![allow(unreachable_code)]
+
+fn main() {
+    for _ in { return (); 0..3 } {} // ok
+}