Skip to content

Forbid coercing &T to &mut T #21424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/librustc/middle/infer/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/coerce-mut.rs
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-12028.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/compile-fail/method-self-arg-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
6 changes: 5 additions & 1 deletion src/test/compile-fail/slice-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}