2016-08-26 13:54:58 +00:00
|
|
|
// ignore-tidy-linelength
|
2017-09-26 19:56:37 +00:00
|
|
|
// revisions: ast mir
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]compile-flags: -Z borrowck=mir
|
2016-08-26 13:54:58 +00:00
|
|
|
|
2016-08-26 16:23:42 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2016-08-26 13:54:58 +00:00
|
|
|
union U {
|
|
|
|
a: u8,
|
|
|
|
b: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
let mut u = U { b: 0 };
|
|
|
|
// Imm borrow, same field
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
|
|
|
let ra2 = &u.a; // OK
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
|
|
|
let a = u.a; // OK
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let rma = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.a` as mutable because it is also borrowed as immutable
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot assign to `u.a` because it is borrowed
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
// Imm borrow, other field
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
|
|
|
let rb = &u.b; // OK
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
|
|
|
let b = u.b; // OK
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let rmb = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable because `u` is also borrowed as immutable (via `u.a`)
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.b` as mutable because it is also borrowed as immutable
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot assign to `u.b` because it is borrowed
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
// Mut borrow, same field
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let ra = &u.a; //[ast]~ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.a` as immutable because it is also borrowed as mutable
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let a = u.a; //[ast]~ ERROR cannot use `u.a` because it was mutably borrowed
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let rma2 = &mut u.a; //[ast]~ ERROR cannot borrow `u.a` as mutable more than once at a time
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.a` as mutable more than once at a time
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
u.a = 1; //[ast]~ ERROR cannot assign to `u.a` because it is borrowed
|
2017-11-19 22:37:59 +00:00
|
|
|
//[mir]~^ ERROR cannot assign to `u.a` because it is borrowed
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
// Mut borrow, other field
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let rb = &u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as immutable because `u` is also borrowed as mutable (via `u.a`)
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.b` as immutable because it is also borrowed as mutable
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let ra = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let b = u.b; //[ast]~ ERROR cannot use `u.b` because it was mutably borrowed
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot use `u.b` because it was mutably borrowed
|
|
|
|
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(ra);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
let rmb2 = &mut u.b; //[ast]~ ERROR cannot borrow `u` (via `u.b`) as mutable more than once at a time
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot borrow `u.b` as mutable more than once at a time
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
let rma = &mut u.a;
|
2017-09-26 19:56:37 +00:00
|
|
|
u.b = 1; //[ast]~ ERROR cannot assign to `u.b` because it is borrowed
|
2017-12-05 13:08:10 +00:00
|
|
|
//[mir]~^ ERROR cannot assign to `u.b` because it is borrowed
|
2018-04-09 09:28:00 +00:00
|
|
|
drop(rma);
|
2016-08-26 13:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|