-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow &raw [mut | const]
for union field in safe
#141469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ union U4<T: Copy> { | |
a: T, | ||
} | ||
|
||
union U5 { | ||
a: usize, | ||
} | ||
|
||
union URef { | ||
p: &'static mut i32, | ||
} | ||
|
@@ -31,6 +35,12 @@ fn deref_union_field(mut u: URef) { | |
*(u.p) = 13; //~ ERROR access to union field is unsafe | ||
} | ||
|
||
fn raw_deref_union_field(mut u: URef) { | ||
let _p = &raw const *(u.p); | ||
//~^ ERROR access to union field is unsafe | ||
//~| ERROR access to union field is unsafe | ||
} | ||
|
||
fn assign_noncopy_union_field(mut u: URefCell) { | ||
u.a = (ManuallyDrop::new(RefCell::new(0)), 1); // OK (assignment does not drop) | ||
u.a.0 = ManuallyDrop::new(RefCell::new(0)); // OK (assignment does not drop) | ||
|
@@ -57,6 +67,19 @@ fn main() { | |
let a = u1.a; //~ ERROR access to union field is unsafe | ||
u1.a = 11; // OK | ||
|
||
let mut u2 = U1 { a: 10 }; | ||
let a = &raw mut u2.a; // OK | ||
unsafe { *a = 3 }; | ||
|
||
let mut u3 = U1 { a: 10 }; | ||
let a = std::ptr::addr_of_mut!(u3.a); // OK | ||
unsafe { *a = 14 }; | ||
|
||
let u4 = U5 { a: 2 }; | ||
let vec = vec![1, 2, 3]; | ||
let _a = &raw const vec[u4.a]; //~ ERROR access to union field is unsafe | ||
//~^ ERROR access to union field is unsafe | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something is wrong here, the error should not be emitted twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this should be WAY better now, also added more comments for tests and code and also removed this global state |
||
|
||
let U1 { a } = u1; //~ ERROR access to union field is unsafe | ||
if let U1 { a: 12 } = u1 {} //~ ERROR access to union field is unsafe | ||
if let Some(U1 { a: 13 }) = Some(u1) {} //~ ERROR access to union field is unsafe | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,117 @@ | ||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:31:6 | ||
--> $DIR/union-unsafe.rs:35:6 | ||
| | ||
LL | *(u.p) = 13; | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:43:6 | ||
--> $DIR/union-unsafe.rs:39:26 | ||
| | ||
LL | let _p = &raw const *(u.p); | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:39:26 | ||
| | ||
LL | let _p = &raw const *(u.p); | ||
| ^^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:53:6 | ||
| | ||
LL | *u3.a = T::default(); | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:49:6 | ||
--> $DIR/union-unsafe.rs:59:6 | ||
| | ||
LL | *u3.a = T::default(); | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:57:13 | ||
--> $DIR/union-unsafe.rs:67:13 | ||
| | ||
LL | let a = u1.a; | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:60:14 | ||
--> $DIR/union-unsafe.rs:80:29 | ||
| | ||
LL | let _a = &raw const vec[u4.a]; | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:80:29 | ||
| | ||
LL | let _a = &raw const vec[u4.a]; | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:83:14 | ||
| | ||
LL | let U1 { a } = u1; | ||
| ^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:61:20 | ||
--> $DIR/union-unsafe.rs:84:20 | ||
| | ||
LL | if let U1 { a: 12 } = u1 {} | ||
| ^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:62:25 | ||
--> $DIR/union-unsafe.rs:85:25 | ||
| | ||
LL | if let Some(U1 { a: 13 }) = Some(u1) {} | ||
| ^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:67:6 | ||
--> $DIR/union-unsafe.rs:90:6 | ||
| | ||
LL | *u2.a = String::from("new"); | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:71:6 | ||
--> $DIR/union-unsafe.rs:94:6 | ||
| | ||
LL | *u3.a = 1; | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error[E0133]: access to union field is unsafe and requires unsafe function or block | ||
--> $DIR/union-unsafe.rs:75:6 | ||
--> $DIR/union-unsafe.rs:98:6 | ||
| | ||
LL | *u3.a = String::from("new"); | ||
| ^^^^ access to union field | ||
| | ||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior | ||
|
||
error: aborting due to 10 previous errors | ||
error: aborting due to 14 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0133`. |
Uh oh!
There was an error while loading. Please reload this page.