-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #90373 - tmiasko:union-qualification, r=oli-obk
Use type based qualification for unions Union field access is currently qualified based on the qualification of a value previously assigned to the union. At the same time, every union access transmutes the content of the union, which might result in a different qualification. For example, consider constants A and B as defined below, under the current rules neither contains interior mutability, since a value used in the initial assignment did not contain `UnsafeCell` constructor. ```rust #![feature(untagged_unions)] union U { i: u32, c: std::cell::Cell<u32> } const A: U = U { i: 0 }; const B: std::cell::Cell<u32> = unsafe { U { i: 0 }.c }; ``` To avoid the issue, the changes here propose to consider the content of a union as opaque and use type based qualification for union types. Fixes #90268. `@rust-lang/wg-const-eval`
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
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
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
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,32 @@ | ||
// Checks that unions use type based qualification. Regression test for issue #90268. | ||
#![feature(untagged_unions)] | ||
use std::cell::Cell; | ||
|
||
union U { i: u32, c: Cell<u32> } | ||
|
||
const C1: Cell<u32> = { | ||
unsafe { U { c: Cell::new(0) }.c } | ||
}; | ||
|
||
const C2: Cell<u32> = { | ||
unsafe { U { i : 0 }.c } | ||
}; | ||
|
||
const C3: Cell<u32> = { | ||
let mut u = U { i: 0 }; | ||
u.i = 1; | ||
unsafe { u.c } | ||
}; | ||
|
||
const C4: U = U { i: 0 }; | ||
|
||
const C5: [U; 1] = [U {i : 0}; 1]; | ||
|
||
fn main() { | ||
// Interior mutability should prevent promotion. | ||
let _: &'static _ = &C1; //~ ERROR temporary value dropped while borrowed | ||
let _: &'static _ = &C2; //~ ERROR temporary value dropped while borrowed | ||
let _: &'static _ = &C3; //~ ERROR temporary value dropped while borrowed | ||
let _: &'static _ = &C4; //~ ERROR temporary value dropped while borrowed | ||
let _: &'static _ = &C5; //~ ERROR temporary value dropped while borrowed | ||
} |
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,57 @@ | ||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/qualif-union.rs:27:26 | ||
| | ||
LL | let _: &'static _ = &C1; | ||
| ---------- ^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
... | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/qualif-union.rs:28:26 | ||
| | ||
LL | let _: &'static _ = &C2; | ||
| ---------- ^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
... | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/qualif-union.rs:29:26 | ||
| | ||
LL | let _: &'static _ = &C3; | ||
| ---------- ^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
... | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/qualif-union.rs:30:26 | ||
| | ||
LL | let _: &'static _ = &C4; | ||
| ---------- ^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
LL | let _: &'static _ = &C5; | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/qualif-union.rs:31:26 | ||
| | ||
LL | let _: &'static _ = &C5; | ||
| ---------- ^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0716`. |