diff --git a/src/librustc/middle/infer/coercion.rs b/src/librustc/middle/infer/coercion.rs index 9f87e73d4af..e8b8ecc701f 100644 --- a/src/librustc/middle/infer/coercion.rs +++ b/src/librustc/middle/infer/coercion.rs @@ -213,7 +213,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { let inner_ty = match a.sty { ty::ty_uniq(_) => return Err(ty::terr_mismatch), - ty::ty_rptr(_, mt_a) => mt_a.ty, + ty::ty_rptr(_, mt_a) => { + if !can_coerce_mutbls(mt_a.mutbl, mutbl_b) { + return Err(ty::terr_mutability); + } + mt_a.ty + } _ => { return self.subtype(a, b); } diff --git a/src/test/compile-fail/coerce-mut.rs b/src/test/compile-fail/coerce-mut.rs new file mode 100644 index 00000000000..30c1b66a7b8 --- /dev/null +++ b/src/test/compile-fail/coerce-mut.rs @@ -0,0 +1,20 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn f(x: &mut i32) {} + +fn main() { + let x = 0; + f(&x); + //~^ ERROR mismatched types + //~| expected `&mut i32` + //~| found `&_` + //~| values differ in mutability +} diff --git a/src/test/compile-fail/issue-12028.rs b/src/test/compile-fail/issue-12028.rs index 24ffc5e9ee3..980385ce4cc 100644 --- a/src/test/compile-fail/issue-12028.rs +++ b/src/test/compile-fail/issue-12028.rs @@ -43,7 +43,7 @@ impl<H: StreamHasher> Hash<H> for u8 { impl<H: StreamHasher> StreamHash<H> for u8 { fn input_stream(&self, stream: &mut H::S) { - Stream::input(&*stream, &[*self]); + Stream::input(stream, &[*self]); } } diff --git a/src/test/compile-fail/method-self-arg-2.rs b/src/test/compile-fail/method-self-arg-2.rs index ad255ecd9c0..dd5b2004145 100644 --- a/src/test/compile-fail/method-self-arg-2.rs +++ b/src/test/compile-fail/method-self-arg-2.rs @@ -22,6 +22,7 @@ fn main() { let y = &mut x; Foo::bar(&x); //~ERROR cannot borrow `x` - let x = Foo; - Foo::baz(&x); //~ERROR cannot borrow immutable borrowed content as mutable + let mut x = Foo; + let y = &mut x; + Foo::baz(&mut x); //~ERROR cannot borrow `x` } diff --git a/src/test/compile-fail/slice-mut.rs b/src/test/compile-fail/slice-mut.rs index 0e1dd0d8f6c..a1747f3b6bd 100644 --- a/src/test/compile-fail/slice-mut.rs +++ b/src/test/compile-fail/slice-mut.rs @@ -13,5 +13,9 @@ fn main() { let x: &[isize] = &[1, 2, 3, 4, 5]; // Immutable slices are not mutable. - let y: &mut[_] = &x[2..4]; //~ ERROR cannot borrow immutable borrowed content as mutable + let y: &mut[_] = &x[2..4]; + //~^ ERROR mismatched types + //~| expected `&mut [_]` + //~| found `&_` + //~| values differ in mutability }