File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ // check-pass
2
+ // known-bug: #49206
3
+
4
+ // Should fail. Compiles and prints 2 identical addresses, which shows 2 threads
5
+ // with the same `'static` reference to non-`Sync` struct. The problem is that
6
+ // promotion to static does not check if the type is `Sync`.
7
+
8
+ #[ allow( dead_code) ]
9
+ #[ derive( Debug ) ]
10
+ struct Foo {
11
+ value : u32 ,
12
+ }
13
+
14
+ // stable negative impl trick from https://crates.io/crates/negative-impl
15
+ // see https://github.com/taiki-e/pin-project/issues/102#issuecomment-540472282
16
+ // for details.
17
+ struct Wrapper < ' a , T > ( :: std:: marker:: PhantomData < & ' a ( ) > , T ) ;
18
+ unsafe impl < T > Sync for Wrapper < ' _ , T > where T : Sync { }
19
+ unsafe impl < ' a > std:: marker:: Sync for Foo where Wrapper < ' a , * const ( ) > : Sync { }
20
+ fn _assert_sync < T : Sync > ( ) { }
21
+
22
+ fn inspect ( ) {
23
+ let foo: & ' static Foo = & Foo { value : 1 } ;
24
+ println ! (
25
+ "I am in thread {:?}, address: {:p}" ,
26
+ std:: thread:: current( ) . id( ) ,
27
+ foo as * const Foo ,
28
+ ) ;
29
+ }
30
+
31
+ fn main ( ) {
32
+ // _assert_sync::<Foo>(); // uncomment this line causes compile error
33
+ // "`*const ()` cannot be shared between threads safely"
34
+
35
+ let handle = std:: thread:: spawn ( inspect) ;
36
+ inspect ( ) ;
37
+ handle. join ( ) . unwrap ( ) ;
38
+ }
You can’t perform that action at this time.
0 commit comments