@@ -37,8 +37,8 @@ unsafe fn ref_to_mut() {
3737 let _num = & mut * ( std:: mem:: transmute :: < _ , * mut i32 > ( num) as * mut i32 ) ;
3838 //~^ ERROR casting `&T` to `&mut T` is undefined behavior
3939 let _num = & mut * std:: cell:: UnsafeCell :: raw_get (
40- //~^ ERROR casting `&T` to `&mut T` is undefined behavior
41- num as * const i32 as * const std:: cell:: UnsafeCell < i32 >
40+ //~^ ERROR casting `&T` to `&mut T` is undefined behavior
41+ num as * const i32 as * const std:: cell:: UnsafeCell < i32 > ,
4242 ) ;
4343
4444 let deferred = num as * const i32 as * mut i32 ;
@@ -62,10 +62,6 @@ unsafe fn ref_to_mut() {
6262 let _num = & mut * num;
6363 //~^ ERROR casting `&T` to `&mut T` is undefined behavior
6464
65- let cell = & std:: cell:: UnsafeCell :: new ( 0 ) ;
66- let _num = & mut * ( cell as * const _ as * mut i32 ) ;
67- //~^ ERROR casting `&T` to `&mut T` is undefined behavior
68-
6965 unsafe fn generic_ref_cast_mut < T > ( this : & T ) -> & mut T {
7066 & mut * ( ( this as * const _ ) as * mut _ )
7167 //~^ ERROR casting `&T` to `&mut T` is undefined behavior
@@ -104,12 +100,10 @@ unsafe fn assign_to_ref() {
104100 * ( std:: mem:: transmute :: < _ , * mut i32 > ( num) as * mut i32 ) += 1 ;
105101 //~^ ERROR assigning to `&T` is undefined behavior
106102 std:: ptr:: write (
107- //~^ ERROR assigning to `&T` is undefined behavior
103+ //~^ ERROR assigning to `&T` is undefined behavior
108104 std:: mem:: transmute :: < * const i32 , * mut i32 > ( num) ,
109105 -1i32 ,
110106 ) ;
111- * ( ( & std:: cell:: UnsafeCell :: new ( 0 ) ) as * const _ as * mut i32 ) = 5 ;
112- //~^ ERROR assigning to `&T` is undefined behavior
113107
114108 let value = num as * const i32 as * mut i32 ;
115109 * value = 1 ;
@@ -207,14 +201,16 @@ unsafe fn bigger_layout() {
207201 }
208202
209203 {
210- let mut l: [ u8 ; 2 ] = [ 0 , 1 ] ;
204+ let mut l: [ u8 ; 2 ] = [ 0 , 1 ] ;
211205 let w: * mut [ u16 ; 2 ] = & mut l as * mut [ u8 ; 2 ] as * mut _ ;
212- let w: * mut [ u16 ] = unsafe { & mut * w} ;
206+ let w: * mut [ u16 ] = unsafe { & mut * w } ;
213207 //~^ ERROR casting references to a bigger memory layout
214208 }
215209
216210 {
217- fn foo ( ) -> [ i32 ; 1 ] { todo ! ( ) }
211+ fn foo ( ) -> [ i32 ; 1 ] {
212+ todo ! ( )
213+ }
218214
219215 let num = foo ( ) ;
220216 let _num = & * ( & num as * const i32 as * const i64 ) ;
@@ -224,15 +220,19 @@ unsafe fn bigger_layout() {
224220 }
225221
226222 {
227- fn bar ( _a : & [ i32 ; 2 ] ) -> & [ i32 ; 1 ] { todo ! ( ) }
223+ fn bar ( _a : & [ i32 ; 2 ] ) -> & [ i32 ; 1 ] {
224+ todo ! ( )
225+ }
228226
229227 let num = bar ( & [ 0 , 0 ] ) ;
230228 let _num = & * ( num as * const i32 as * const i64 ) ;
231229 let _num = & * ( bar ( & [ 0 , 0 ] ) as * const i32 as * const i64 ) ;
232230 }
233231
234232 {
235- fn foi < T > ( ) -> T { todo ! ( ) }
233+ fn foi < T > ( ) -> T {
234+ todo ! ( )
235+ }
236236
237237 let num = foi :: < i32 > ( ) ;
238238 let _num = & * ( & num as * const i32 as * const i64 ) ;
@@ -286,24 +286,41 @@ unsafe fn no_warn() {
286286 let value: * const i32 = & mut value;
287287 * ( value as * const i16 as * mut i16 ) = 42 ;
288288 * RAW_PTR = 42 ; // RAW_PTR is defined outside the function body,
289- // make sure we don't ICE on it when trying to
290- // determine if we should lint on it or not.
289+ // make sure we don't ICE on it when trying to
290+ // determine if we should lint on it or not.
291+ * ( ( & std:: cell:: UnsafeCell :: new ( 0 ) ) as * const _ as * mut i32 ) = 5 ;
292+
291293 let cell = & std:: cell:: UnsafeCell :: new ( 0 ) ;
292294 let _num = & mut * ( cell. get ( ) as * mut i32 ) ;
295+ let _num = & mut * ( cell as * const _ as * mut i32 ) ;
293296
294- fn safe_as_mut < T > ( x : & std:: cell:: UnsafeCell < T > ) -> & mut T {
297+ unsafe fn get_mut_unchecked < T > ( x : & std:: cell:: UnsafeCell < T > ) -> & mut T {
295298 unsafe { & mut * std:: cell:: UnsafeCell :: raw_get ( x as * const _ as * const _ ) }
296299 }
297300
298- fn cell_as_mut ( x : & std:: cell:: Cell < i32 > ) -> & mut i32 {
301+ unsafe fn get_mut_unchecked2 < T > ( ptr : & std:: cell:: UnsafeCell < T > ) -> & mut T {
302+ let t = ptr as * const std:: cell:: UnsafeCell < T > as * mut T ;
303+ unsafe { & mut * t }
304+ }
305+
306+ unsafe fn cell_as_mut ( x : & std:: cell:: Cell < i32 > ) -> & mut i32 {
299307 unsafe { & mut * std:: cell:: UnsafeCell :: raw_get ( x as * const _ as * const _ ) }
300308 }
301309
310+ unsafe fn cell_as_mut2 ( x : & std:: cell:: Cell < i32 > ) -> & mut i32 {
311+ unsafe { & mut * ( x as * const std:: cell:: Cell < i32 > as * mut i32 ) }
312+ }
313+
302314 #[ repr( transparent) ]
303315 struct DoesContainUnsafeCell ( std:: cell:: UnsafeCell < i32 > ) ;
304- fn safe_as_mut2 ( x : & DoesContainUnsafeCell ) -> & mut DoesContainUnsafeCell {
316+
317+ unsafe fn get_mut_unchecked3 ( x : & DoesContainUnsafeCell ) -> & mut DoesContainUnsafeCell {
305318 unsafe { & mut * std:: cell:: UnsafeCell :: raw_get ( x as * const _ as * const _ ) }
306319 }
320+
321+ unsafe fn get_mut_unchecked4 ( x : & DoesContainUnsafeCell ) -> & mut DoesContainUnsafeCell {
322+ unsafe { & mut * ( x as * const DoesContainUnsafeCell as * mut _ ) }
323+ }
307324}
308325
309326fn main ( ) { }
0 commit comments