rust/src/test/compile-fail/borrowck/borrowck-issue-14498.rs

125 lines
3.4 KiB
Rust
Raw Normal View History

// Copyright 2014 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.
// This tests that we can't modify Box<&mut T> contents while they
2015-03-30 04:47:27 -05:00
// are borrowed (#14498).
//
// Also includes tests of the errors reported when the Box in question
// is immutable (#14270).
2015-01-07 20:53:58 -06:00
#![feature(box_syntax)]
struct A { a: isize }
struct B<'a> { a: Box<&'a mut isize> }
2015-03-30 04:47:27 -05:00
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() {
2015-03-30 04:47:27 -05:00
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
2015-03-30 04:47:27 -05:00
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_var_from_field() {
2015-03-30 04:47:27 -05:00
let mut x = A { a: 1 };
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
drop(p);
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
2015-03-30 04:47:27 -05:00
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_field_from_var() {
2015-03-30 04:47:27 -05:00
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
2015-03-30 04:47:27 -05:00
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn borrow_in_field_from_field() {
2015-03-30 04:47:27 -05:00
let mut x = A { a: 1 };
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
drop(p);
drop(q);
}
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
2015-03-30 04:47:27 -05:00
//~^ ERROR cannot assign to data in an immutable container
drop(p);
drop(q);
}
fn main() {
2015-03-30 04:47:27 -05:00
indirect_write_to_imm_box();
borrow_in_var_from_var();
2015-03-30 04:47:27 -05:00
borrow_in_var_from_var_via_imm_box();
borrow_in_var_from_field();
2015-03-30 04:47:27 -05:00
borrow_in_var_from_field_via_imm_box();
borrow_in_field_from_var();
2015-03-30 04:47:27 -05:00
borrow_in_field_from_var_via_imm_box();
borrow_in_field_from_field();
2015-03-30 04:47:27 -05:00
borrow_in_field_from_field_via_imm_box();
}