]> git.lizzy.rs Git - rust.git/blob - tests/ui/span/send-is-not-static-std-sync.rs
Rollup merge of #106441 - mllken:abstract-socket-noref, r=joshtriplett
[rust.git] / tests / ui / span / send-is-not-static-std-sync.rs
1 // basic tests to see that certain "obvious" errors are caught by
2 // these types no longer requiring `'static` (RFC 458)
3
4 #![allow(dead_code)]
5
6 use std::sync::{Mutex, RwLock, mpsc};
7
8 fn mutex() {
9     let x = 1;
10     let y = Box::new(1);
11     let lock = Mutex::new(&x);
12     *lock.lock().unwrap() = &*y;
13     drop(y); //~ ERROR cannot move out
14     {
15         let z = 2;
16         *lock.lock().unwrap() = &z;
17     }
18     //~^^ ERROR `z` does not live long enough
19     lock.use_ref(); // (Mutex is #[may_dangle] so its dtor does not use `z` => needs explicit use)
20 }
21
22 fn rwlock() {
23     let x = 1;
24     let y = Box::new(1);
25     let lock = RwLock::new(&x);
26     *lock.write().unwrap() = &*y;
27     drop(y); //~ ERROR cannot move out
28     {
29         let z = 2;
30         *lock.write().unwrap() = &z;
31     }
32     //~^^ ERROR `z` does not live long enough
33     lock.use_ref(); // (RwLock is #[may_dangle] so its dtor does not use `z` => needs explicit use)
34 }
35
36 fn channel() {
37     let x = 1;
38     let y = Box::new(1);
39     let (tx, rx) = mpsc::channel();
40
41     tx.send(&x).unwrap();
42     tx.send(&*y);
43     drop(y); //~ ERROR cannot move out
44     {
45         let z = 2;
46         tx.send(&z).unwrap();
47     }
48     //~^^ ERROR `z` does not live long enough
49     // (channels lack #[may_dangle], thus their dtors are implicit uses of `z`)
50 }
51
52 fn main() {}
53
54 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
55 impl<T> Fake for T { }