Skip to content

Commit c49d428

Browse files
committed
Rollup merge of #21424 - sanxiyn:coerce-mut, r=nikomatsakis
This is caught in borrowck now, but catching in typeck is faster and improves diagnostics. CC #17561. r? @nikomatsakis
2 parents 1d206e2 + 5cd9a69 commit c49d428

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/librustc/middle/infer/coercion.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
213213

214214
let inner_ty = match a.sty {
215215
ty::ty_uniq(_) => return Err(ty::terr_mismatch),
216-
ty::ty_rptr(_, mt_a) => mt_a.ty,
216+
ty::ty_rptr(_, mt_a) => {
217+
if !can_coerce_mutbls(mt_a.mutbl, mutbl_b) {
218+
return Err(ty::terr_mutability);
219+
}
220+
mt_a.ty
221+
}
217222
_ => {
218223
return self.subtype(a, b);
219224
}

src/test/compile-fail/coerce-mut.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn f(x: &mut i32) {}
12+
13+
fn main() {
14+
let x = 0;
15+
f(&x);
16+
//~^ ERROR mismatched types
17+
//~| expected `&mut i32`
18+
//~| found `&_`
19+
//~| values differ in mutability
20+
}

src/test/compile-fail/issue-12028.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<H: StreamHasher> Hash<H> for u8 {
4343

4444
impl<H: StreamHasher> StreamHash<H> for u8 {
4545
fn input_stream(&self, stream: &mut H::S) {
46-
Stream::input(&*stream, &[*self]);
46+
Stream::input(stream, &[*self]);
4747
}
4848
}
4949

src/test/compile-fail/method-self-arg-2.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {
2222
let y = &mut x;
2323
Foo::bar(&x); //~ERROR cannot borrow `x`
2424

25-
let x = Foo;
26-
Foo::baz(&x); //~ERROR cannot borrow immutable borrowed content as mutable
25+
let mut x = Foo;
26+
let y = &mut x;
27+
Foo::baz(&mut x); //~ERROR cannot borrow `x`
2728
}

src/test/compile-fail/slice-mut.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
fn main() {
1414
let x: &[isize] = &[1, 2, 3, 4, 5];
1515
// Immutable slices are not mutable.
16-
let y: &mut[_] = &x[2..4]; //~ ERROR cannot borrow immutable borrowed content as mutable
16+
let y: &mut[_] = &x[2..4];
17+
//~^ ERROR mismatched types
18+
//~| expected `&mut [_]`
19+
//~| found `&_`
20+
//~| values differ in mutability
1721
}

0 commit comments

Comments
 (0)