rust/src/test/ui/issues/issue-45157.rs
David Wood 69bded2493
Add union justifications to conflicting borrows.
This commit adds justifications to error messages for conflicting
borrows of union fields.

Where previously an error message would say
``cannot borrow `u.b` as mutable..``, it now says
``cannot borrow `u` (via `u.b`) as mutable..``.
2018-12-26 21:28:33 +01:00

34 lines
556 B
Rust

#![allow(unused)]
#![feature(nll)]
// ignore-tidy-linelength
#[derive(Clone, Copy, Default)]
struct S {
a: u8,
b: u8,
}
#[derive(Clone, Copy, Default)]
struct Z {
c: u8,
d: u8,
}
union U {
s: S,
z: Z,
}
fn main() {
unsafe {
let mut u = U { s: Default::default() };
let mref = &mut u.s.a;
*mref = 22;
let nref = &u.z.c;
//~^ ERROR cannot borrow `u` (via `u.z.c`) as immutable because it is also borrowed as mutable (via `u.s.a`) [E0502]
println!("{} {}", mref, nref)
}
}