File tree 5 files changed +73
-0
lines changed
5 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std:: cell:: * ;
2
+
3
+ // not ok, because this would create a silent constant with interior mutability.
4
+ // the rules could be relaxed in the future
5
+ static FOO : Wrap < * mut u32 > = Wrap ( Cell :: new ( 42 ) . as_ptr ( ) ) ;
6
+ //~^ ERROR cannot borrow a constant which may contain interior mutability
7
+
8
+ static FOO3 : Wrap < Cell < u32 > > = Wrap ( Cell :: new ( 42 ) ) ;
9
+ // ok
10
+ static FOO4 : Wrap < * mut u32 > = Wrap ( FOO3 . 0 . as_ptr ( ) ) ;
11
+
12
+ // not ok, because the `as_ptr` call takes a reference to a type with interior mutability
13
+ // which is not allowed in constants
14
+ const FOO2 : * mut u32 = Cell :: new ( 42 ) . as_ptr ( ) ;
15
+ //~^ ERROR cannot borrow a constant which may contain interior mutability
16
+
17
+ struct IMSafeTrustMe ( UnsafeCell < u32 > ) ;
18
+ unsafe impl Send for IMSafeTrustMe { }
19
+ unsafe impl Sync for IMSafeTrustMe { }
20
+
21
+ static BAR : IMSafeTrustMe = IMSafeTrustMe ( UnsafeCell :: new ( 5 ) ) ;
22
+
23
+
24
+ struct Wrap < T > ( T ) ;
25
+ unsafe impl < T > Send for Wrap < T > { }
26
+ unsafe impl < T > Sync for Wrap < T > { }
27
+
28
+ static BAR_PTR : Wrap < * mut u32 > = Wrap ( BAR . 0 . get ( ) ) ;
29
+
30
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2
+ --> $DIR/cell.rs:5:35
3
+ |
4
+ LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
5
+ | ^^^^^^^^^^^^^
6
+
7
+ error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8
+ --> $DIR/cell.rs:14:24
9
+ |
10
+ LL | const FOO2: *mut u32 = Cell::new(42).as_ptr();
11
+ | ^^^^^^^^^^^^^
12
+
13
+ error: aborting due to 2 previous errors
14
+
15
+ For more information about this error, try `rustc --explain E0492`.
Original file line number Diff line number Diff line change
1
+ // run-pass
2
+
3
+ static X : bool = 'a' . is_ascii ( ) ;
4
+ static Y : bool = 'ä' . is_ascii ( ) ;
5
+
6
+ fn main ( ) {
7
+ assert ! ( X ) ;
8
+ assert ! ( !Y ) ;
9
+ }
Original file line number Diff line number Diff line change
1
+ // run-pass
2
+
3
+ const I : std:: iter:: Empty < u32 > = std:: iter:: empty ( ) ;
4
+
5
+ fn main ( ) {
6
+ for i in I {
7
+ panic ! ( "magical value creation: {}" , i) ;
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ // compile-pass
2
+
3
+ struct Wrap < T > ( T ) ;
4
+ unsafe impl < T > Send for Wrap < T > { }
5
+ unsafe impl < T > Sync for Wrap < T > { }
6
+
7
+ static FOO : Wrap < * const u32 > = Wrap ( [ 42 , 44 , 46 ] . as_ptr ( ) ) ;
8
+ static BAR : Wrap < * const u8 > = Wrap ( "hello" . as_ptr ( ) ) ;
9
+
10
+ fn main ( ) { }
You can’t perform that action at this time.
0 commit comments