Rollup merge of #23859 - pnkfelix:fsk-lesser-box, r=nikomatsakis

Disallow writing through mutable pointers stored in non-mut Box.

Fix #14270 

The fix works by making `cmt::freely_aliasable` result more fine-grained.

Instead of encoding the aliasability (i.e. whether the cmt is uniquely writable or not) as an option, now pass back an enum indicating either: 1. freely-aliasable (thus not uniquely-writable), 2. non-aliasable (thus uniquely writable), or 3. unique but immutable (and thus not uniquely writable, according to proposal from issue #14270.)

This is all of course a giant hack that will hopefully go away with an eventually removal of special treatment of `Box<T>` (aka `ty_unique`) from the compiler.
This commit is contained in:
Manish Goregaokar 2015-03-31 09:04:38 +05:30
commit b4457fb8a2
6 changed files with 230 additions and 72 deletions

View file

@ -9,26 +9,48 @@
// except according to those terms.
// This tests that we can't modify Box<&mut T> contents while they
// are borrowed.
// are borrowed (#14498).
//
// Also includes tests of the errors reported when the Box in question
// is immutable (#14270).
#![feature(box_syntax)]
struct A { a: isize }
struct B<'a> { a: Box<&'a mut isize> }
fn indirect_write_to_imm_box() {
let mut x: isize = 1;
let y: Box<_> = box &mut x;
let p = &y;
***p = 2; //~ ERROR cannot assign to data in an immutable container
drop(p);
}
fn borrow_in_var_from_var() {
let mut x: isize = 1;
let mut y: Box<_> = box &mut x;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
drop(p);
drop(q);
}
fn borrow_in_var_from_var_via_imm_box() {
let mut x: isize = 1;
let y: Box<_> = box &mut x;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_var_from_field() {
let mut x = A { a: 1 };
let y: Box<_> = box &mut x.a;
let mut y: Box<_> = box &mut x.a;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
@ -36,19 +58,41 @@ fn borrow_in_var_from_field() {
drop(q);
}
fn borrow_in_var_from_field_via_imm_box() {
let mut x = A { a: 1 };
let y: Box<_> = box &mut x.a;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_field_from_var() {
let mut x: isize = 1;
let mut y = B { a: box &mut x };
let p = &y.a;
let q = &***p;
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
drop(p);
drop(q);
}
fn borrow_in_field_from_var_via_imm_box() {
let mut x: isize = 1;
let y = B { a: box &mut x };
let p = &y.a;
let q = &***p;
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_field_from_field() {
let mut x = A { a: 1 };
let y = B { a: box &mut x.a };
let mut y = B { a: box &mut x.a };
let p = &y.a;
let q = &***p;
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
@ -56,9 +100,25 @@ fn borrow_in_field_from_field() {
drop(q);
}
fn main() {
borrow_in_var_from_var();
borrow_in_var_from_field();
borrow_in_field_from_var();
borrow_in_field_from_field();
fn borrow_in_field_from_field_via_imm_box() {
let mut x = A { a: 1 };
let y = B { a: box &mut x.a };
let p = &y.a;
let q = &***p;
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn main() {
indirect_write_to_imm_box();
borrow_in_var_from_var();
borrow_in_var_from_var_via_imm_box();
borrow_in_var_from_field();
borrow_in_var_from_field_via_imm_box();
borrow_in_field_from_var();
borrow_in_field_from_var_via_imm_box();
borrow_in_field_from_field();
borrow_in_field_from_field_via_imm_box();
}