Forbid coercing &T to &mut T

This commit is contained in:
Seo Sanghyeon 2015-01-20 22:56:53 +09:00
parent 3bf41dafcf
commit 5cd9a69832
5 changed files with 35 additions and 5 deletions

View File

@ -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);
}

View File

@ -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
}

View File

@ -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]);
}
}

View File

@ -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`
}

View File

@ -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
}