-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Allow UnsafeCell content access without get in invalid_reference_casting lint
#159960
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
Changes from all 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 |
|---|---|---|
|
|
@@ -37,8 +37,8 @@ unsafe fn ref_to_mut() { | |
| let _num = &mut *(std::mem::transmute::<_, *mut i32>(num) as *mut i32); | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
| let _num = &mut *std::cell::UnsafeCell::raw_get( | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
| num as *const i32 as *const std::cell::UnsafeCell<i32> | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
| num as *const i32 as *const std::cell::UnsafeCell<i32>, | ||
| ); | ||
|
|
||
| let deferred = num as *const i32 as *mut i32; | ||
|
|
@@ -62,10 +62,6 @@ unsafe fn ref_to_mut() { | |
| let _num = &mut *num; | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
|
|
||
| let cell = &std::cell::UnsafeCell::new(0); | ||
| let _num = &mut *(cell as *const _ as *mut i32); | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
|
|
||
| unsafe fn generic_ref_cast_mut<T>(this: &T) -> &mut T { | ||
| &mut *((this as *const _) as *mut _) | ||
| //~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
|
|
@@ -104,12 +100,10 @@ unsafe fn assign_to_ref() { | |
| *(std::mem::transmute::<_, *mut i32>(num) as *mut i32) += 1; | ||
| //~^ ERROR assigning to `&T` is undefined behavior | ||
| std::ptr::write( | ||
| //~^ ERROR assigning to `&T` is undefined behavior | ||
| //~^ ERROR assigning to `&T` is undefined behavior | ||
| std::mem::transmute::<*const i32, *mut i32>(num), | ||
| -1i32, | ||
| ); | ||
| *((&std::cell::UnsafeCell::new(0)) as *const _ as *mut i32) = 5; | ||
| //~^ ERROR assigning to `&T` is undefined behavior | ||
|
|
||
| let value = num as *const i32 as *mut i32; | ||
| *value = 1; | ||
|
|
@@ -207,14 +201,16 @@ unsafe fn bigger_layout() { | |
| } | ||
|
|
||
| { | ||
| let mut l: [u8; 2] = [0,1]; | ||
| let mut l: [u8; 2] = [0, 1]; | ||
| let w: *mut [u16; 2] = &mut l as *mut [u8; 2] as *mut _; | ||
| let w: *mut [u16] = unsafe {&mut *w}; | ||
| let w: *mut [u16] = unsafe { &mut *w }; | ||
| //~^ ERROR casting references to a bigger memory layout | ||
| } | ||
|
|
||
| { | ||
| fn foo() -> [i32; 1] { todo!() } | ||
| fn foo() -> [i32; 1] { | ||
| todo!() | ||
| } | ||
|
|
||
| let num = foo(); | ||
| let _num = &*(&num as *const i32 as *const i64); | ||
|
|
@@ -224,15 +220,19 @@ unsafe fn bigger_layout() { | |
| } | ||
|
|
||
| { | ||
| fn bar(_a: &[i32; 2]) -> &[i32; 1] { todo!() } | ||
| fn bar(_a: &[i32; 2]) -> &[i32; 1] { | ||
| todo!() | ||
| } | ||
|
|
||
| let num = bar(&[0, 0]); | ||
| let _num = &*(num as *const i32 as *const i64); | ||
| let _num = &*(bar(&[0, 0]) as *const i32 as *const i64); | ||
| } | ||
|
|
||
| { | ||
| fn foi<T>() -> T { todo!() } | ||
| fn foi<T>() -> T { | ||
| todo!() | ||
| } | ||
|
|
||
| let num = foi::<i32>(); | ||
| let _num = &*(&num as *const i32 as *const i64); | ||
|
|
@@ -286,24 +286,41 @@ unsafe fn no_warn() { | |
| let value: *const i32 = &mut value; | ||
| *(value as *const i16 as *mut i16) = 42; | ||
| *RAW_PTR = 42; // RAW_PTR is defined outside the function body, | ||
| // make sure we don't ICE on it when trying to | ||
| // determine if we should lint on it or not. | ||
| // make sure we don't ICE on it when trying to | ||
| // determine if we should lint on it or not. | ||
| *((&std::cell::UnsafeCell::new(0)) as *const _ as *mut i32) = 5; | ||
|
|
||
| let cell = &std::cell::UnsafeCell::new(0); | ||
| let _num = &mut *(cell.get() as *mut i32); | ||
| let _num = &mut *(cell as *const _ as *mut i32); | ||
|
|
||
| fn safe_as_mut<T>(x: &std::cell::UnsafeCell<T>) -> &mut T { | ||
| unsafe fn get_mut_unchecked<T>(x: &std::cell::UnsafeCell<T>) -> &mut T { | ||
| unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) } | ||
| } | ||
|
|
||
| fn cell_as_mut(x: &std::cell::Cell<i32>) -> &mut i32 { | ||
| unsafe fn get_mut_unchecked2<T>(ptr: &std::cell::UnsafeCell<T>) -> &mut T { | ||
| let t = ptr as *const std::cell::UnsafeCell<T> as *mut T; | ||
| unsafe { &mut *t } | ||
| } | ||
|
|
||
| unsafe fn cell_as_mut(x: &std::cell::Cell<i32>) -> &mut i32 { | ||
| unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) } | ||
| } | ||
|
|
||
| unsafe fn cell_as_mut2(x: &std::cell::Cell<i32>) -> &mut i32 { | ||
| unsafe { &mut *(x as *const std::cell::Cell<i32> as *mut i32) } | ||
| } | ||
|
|
||
| #[repr(transparent)] | ||
| struct DoesContainUnsafeCell(std::cell::UnsafeCell<i32>); | ||
| fn safe_as_mut2(x: &DoesContainUnsafeCell) -> &mut DoesContainUnsafeCell { | ||
|
|
||
| unsafe fn get_mut_unchecked3(x: &DoesContainUnsafeCell) -> &mut DoesContainUnsafeCell { | ||
| unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) } | ||
| } | ||
|
|
||
| unsafe fn get_mut_unchecked4(x: &DoesContainUnsafeCell) -> &mut DoesContainUnsafeCell { | ||
| unsafe { &mut *(x as *const DoesContainUnsafeCell as *mut _) } | ||
| } | ||
|
Comment on lines
+289
to
+323
Member
Author
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. @RalfJung could you double-check and confirm that all of those casts are now fine.
Member
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. Depends on what you mean by "fine". ;) They are not immediate UB. But getting a reference into a
Member
Author
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. Yeah, by "fine" I mean no longer immediate UB, it can still be very UB. Thanks for checking. |
||
| } | ||
|
|
||
| fn main() {} | ||
Uh oh!
There was an error while loading. Please reload this page.