Skip to content

Commit

Permalink
Auto merge of #46041 - zilbuz:issue-44837, r=arielb1
Browse files Browse the repository at this point in the history
MIR borrowck: finalize `check_access_permissions()`

Fix #44837 (hopefully for good)

r? @arielb1
  • Loading branch information
bors committed Nov 30, 2017
2 parents 78fcf33 + 1cd9d74 commit 909b94b
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 79 deletions.
256 changes: 193 additions & 63 deletions src/librustc_mir/borrow_check.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/test/compile-fail/E0594.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ static NUM: i32 = 18;

fn main() {
NUM = 20; //[ast]~ ERROR E0594
//[mir]~^ ERROR cannot assign to immutable static item
//[mir]~^ ERROR cannot assign to immutable item `NUM`
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ static foo: isize = 5;
fn main() {
// assigning to various global constants
foo = 6; //[ast]~ ERROR cannot assign to immutable static item
//[mir]~^ ERROR cannot assign to immutable static item `foo`
//[mir]~^ ERROR cannot assign to immutable item `foo`
}
30 changes: 21 additions & 9 deletions src/test/compile-fail/borrowck/borrowck-issue-14498.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// Also includes tests of the errors reported when the Box in question
// is immutable (#14270).

// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir

#![feature(box_syntax)]

struct A { a: isize }
Expand All @@ -23,7 +26,8 @@ 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 a `&` reference
***p = 2; //[ast]~ ERROR cannot assign to data in a `&` reference
//[mir]~^ ERROR cannot assign to immutable item `***p`
drop(p);
}

Expand All @@ -32,7 +36,8 @@ fn borrow_in_var_from_var() {
let mut y: Box<_> = box &mut x;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
**y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -42,7 +47,8 @@ fn borrow_in_var_from_var_via_imm_box() {
let y: Box<_> = box &mut x;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
**y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -52,7 +58,8 @@ fn borrow_in_var_from_field() {
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
**y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -62,7 +69,8 @@ fn borrow_in_var_from_field_via_imm_box() {
let y: Box<_> = box &mut x.a;
let p = &y;
let q = &***p;
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
**y = 2; //[ast]~ ERROR cannot assign to `**y` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -72,7 +80,8 @@ fn borrow_in_field_from_var() {
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
**y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -82,7 +91,8 @@ fn borrow_in_field_from_var_via_imm_box() {
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
**y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -92,7 +102,8 @@ fn borrow_in_field_from_field() {
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
**y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed
drop(p);
drop(q);
}
Expand All @@ -102,7 +113,8 @@ fn borrow_in_field_from_field_via_imm_box() {
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
**y.a = 2; //[ast]~ ERROR cannot assign to `**y.a` because it is borrowed
//[mir]~^ ERROR cannot assign to `**y.a` because it is borrowed
drop(p);
drop(q);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ fn main() {
};
s[2] = 20;
//[ast]~^ ERROR cannot assign to immutable indexed content
// FIXME Error for MIR
//[mir]~^^ ERROR cannot assign to immutable item
}
10 changes: 8 additions & 2 deletions src/test/compile-fail/cannot-mutate-captured-non-mut-var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-tidy-linelength
// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir

#![feature(unboxed_closures)]

use std::io::Read;
Expand All @@ -17,9 +21,11 @@ fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
fn main() {
let x = 1;
to_fn_once(move|| { x = 2; });
//~^ ERROR: cannot assign to immutable captured outer variable
//[ast]~^ ERROR: cannot assign to immutable captured outer variable
//[mir]~^^ ERROR: cannot assign to immutable item `x`

let s = std::io::stdin();
to_fn_once(move|| { s.read_to_end(&mut Vec::new()); });
//~^ ERROR: cannot borrow immutable captured outer variable
//[ast]~^ ERROR: cannot borrow immutable captured outer variable
//[mir]~^^ ERROR: cannot borrow immutable item `s` as mutable
}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-5500-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn main() {
let _iter = TrieMapIterator{node: &a};
_iter.node = & //[ast]~ ERROR cannot assign to immutable field `_iter.node`
//[mir]~^ ERROR cannot assign to immutable field `_iter.node` (Ast)
// FIXME Error for MIR
// MIR doesn't generate an error because the code isn't reachable. This is OK
// because the test is here to check that the compiler doesn't ICE (cf. #5500).
panic!()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// revisions: ast mir
//[mir]compile-flags: -Z borrowck=mir

// Test that a by-ref `FnMut` closure gets an error when it tries to
// mutate a value.

Expand All @@ -19,6 +22,7 @@ fn main() {
let mut counter = 0;
call(|| {
counter += 1;
//~^ ERROR cannot assign to data in a captured outer variable in an `Fn` closure
//[ast]~^ ERROR cannot assign to data in a captured outer variable in an `Fn` closure
//[mir]~^^ ERROR cannot assign to immutable item `counter`
});
}

0 comments on commit 909b94b

Please sign in to comment.