forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#47689 - davidtwco:issue-45157, r=nikomatsakis
Fix borrow checker unsoundness with unions Fixes rust-lang#45157. After discussion with @nikomatsakis on Gitter, this PR only adds a test since the original issue was resolved elsewhere. r? @nikomatsakis
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2017 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. | ||
|
||
#![allow(unused)] | ||
#![feature(nll)] | ||
|
||
#[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.z.c` as immutable because it is also borrowed as mutable [E0502] | ||
println!("{} {}", mref, nref) | ||
//~^ ERROR cannot borrow `u.s.a` as mutable because it is also borrowed as immutable [E0502] | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0502]: cannot borrow `u.z.c` as immutable because it is also borrowed as mutable | ||
--> $DIR/issue-45157.rs:37:20 | ||
| | ||
34 | let mref = &mut u.s.a; | ||
| ---------- mutable borrow occurs here | ||
... | ||
37 | let nref = &u.z.c; | ||
| ^^^^^^ immutable borrow occurs here | ||
|
||
error[E0502]: cannot borrow `u.s.a` as mutable because it is also borrowed as immutable | ||
--> $DIR/issue-45157.rs:39:27 | ||
| | ||
37 | let nref = &u.z.c; | ||
| ------ immutable borrow occurs here | ||
38 | //~^ ERROR cannot borrow `u.z.c` as immutable because it is also borrowed as mutable [E0502] | ||
39 | println!("{} {}", mref, nref) | ||
| ^^^^ mutable borrow occurs here | ||
|
||
error: aborting due to 2 previous errors | ||
|