diff options
Diffstat (limited to 'src/test/ui/mut')
19 files changed, 311 insertions, 0 deletions
diff --git a/src/test/ui/mut/mut-cant-alias.rs b/src/test/ui/mut/mut-cant-alias.rs new file mode 100644 index 000000000..9146b931a --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-cant-alias.stderr b/src/test/ui/mut/mut-cant-alias.stderr new file mode 100644 index 000000000..6046c076f --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-cross-borrowing.rs b/src/test/ui/mut/mut-cross-borrowing.rs new file mode 100644 index 000000000..080faab73 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-cross-borrowing.stderr b/src/test/ui/mut/mut-cross-borrowing.stderr new file mode 100644 index 000000000..ee739d628 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-pattern-internal-mutability.rs b/src/test/ui/mut/mut-pattern-internal-mutability.rs new file mode 100644 index 000000000..bcee878e3 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-pattern-internal-mutability.stderr b/src/test/ui/mut/mut-pattern-internal-mutability.stderr new file mode 100644 index 000000000..6583546aa --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-pattern-mismatched.rs b/src/test/ui/mut/mut-pattern-mismatched.rs new file mode 100644 index 000000000..700261fe4 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-pattern-mismatched.stderr b/src/test/ui/mut/mut-pattern-mismatched.stderr new file mode 100644 index 000000000..cad1cef51 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-ref.rs b/src/test/ui/mut/mut-ref.rs new file mode 100644 index 000000000..80990b2bf --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-ref.stderr b/src/test/ui/mut/mut-ref.stderr new file mode 100644 index 000000000..e6d4901aa --- /dev/null +++ b/src/test/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/src/test/ui/mut/mut-suggestion.rs b/src/test/ui/mut/mut-suggestion.rs new file mode 100644 index 000000000..3104b20ac --- /dev/null +++ b/src/test/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 + 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 + local.mutate(); + //~^ ERROR cannot borrow `local` as mutable, as it is not declared as mutable +} diff --git a/src/test/ui/mut/mut-suggestion.stderr b/src/test/ui/mut/mut-suggestion.stderr new file mode 100644 index 000000000..cba284550 --- /dev/null +++ b/src/test/ui/mut/mut-suggestion.stderr @@ -0,0 +1,21 @@ +error[E0596]: cannot borrow `arg` as mutable, as it is not declared as mutable + --> $DIR/mut-suggestion.rs:12:5 + | +LL | fn func(arg: S) { + | --- help: consider changing this to be mutable: `mut arg` +... +LL | arg.mutate(); + | ^^^^^^^^^^^^ cannot borrow as mutable + +error[E0596]: cannot borrow `local` as mutable, as it is not declared as mutable + --> $DIR/mut-suggestion.rs:20:5 + | +LL | let local = S; + | ----- help: consider changing this to be mutable: `mut local` +... +LL | local.mutate(); + | ^^^^^^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0596`. diff --git a/src/test/ui/mut/mutable-class-fields-2.rs b/src/test/ui/mut/mutable-class-fields-2.rs new file mode 100644 index 000000000..30e54dfc9 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mutable-class-fields-2.stderr b/src/test/ui/mut/mutable-class-fields-2.stderr new file mode 100644 index 000000000..5a4e31947 --- /dev/null +++ b/src/test/ui/mut/mutable-class-fields-2.stderr @@ -0,0 +1,11 @@ +error[E0594]: cannot assign to `self.how_hungry`, which is behind a `&` reference + --> $DIR/mutable-class-fields-2.rs:9:5 + | +LL | pub fn eat(&self) { + | ----- help: consider changing this to be a mutable reference: `&mut self` +LL | self.how_hungry -= 5; + | ^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be written + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. diff --git a/src/test/ui/mut/mutable-class-fields.rs b/src/test/ui/mut/mutable-class-fields.rs new file mode 100644 index 000000000..30768a1ec --- /dev/null +++ b/src/test/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/src/test/ui/mut/mutable-class-fields.stderr b/src/test/ui/mut/mutable-class-fields.stderr new file mode 100644 index 000000000..40a0dc9b2 --- /dev/null +++ b/src/test/ui/mut/mutable-class-fields.stderr @@ -0,0 +1,11 @@ +error[E0594]: cannot assign to `nyan.how_hungry`, as `nyan` is not declared as mutable + --> $DIR/mutable-class-fields.rs:15:3 + | +LL | let nyan : Cat = cat(52, 99); + | ---- help: consider changing this to be mutable: `mut nyan` +LL | nyan.how_hungry = 0; + | ^^^^^^^^^^^^^^^^^^^ cannot assign + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0594`. diff --git a/src/test/ui/mut/mutable-enum-indirect.rs b/src/test/ui/mut/mutable-enum-indirect.rs new file mode 100644 index 000000000..502859c04 --- /dev/null +++ b/src/test/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/src/test/ui/mut/mutable-enum-indirect.stderr b/src/test/ui/mut/mutable-enum-indirect.stderr new file mode 100644 index 000000000..9e1f4e1fe --- /dev/null +++ b/src/test/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/src/test/ui/mut/no-mut-lint-for-desugared-mut.rs b/src/test/ui/mut/no-mut-lint-for-desugared-mut.rs new file mode 100644 index 000000000..419d58041 --- /dev/null +++ b/src/test/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 +} |