]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/send-is-not-static-std-sync.rs
Auto merge of #51066 - est31:master, r=sfackler
[rust.git] / src / test / ui / span / send-is-not-static-std-sync.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // basic tests to see that certain "obvious" errors are caught by
12 // these types no longer requiring `'static` (RFC 458)
13
14 #![allow(dead_code)]
15
16 use std::sync::{Mutex, RwLock, mpsc};
17
18 fn mutex() {
19     let x = 1;
20     let y = Box::new(1);
21     let lock = Mutex::new(&x);
22     *lock.lock().unwrap() = &*y;
23     drop(y); //~ ERROR cannot move out
24     {
25         let z = 2;
26         *lock.lock().unwrap() = &z;
27     }
28     //~^^ ERROR `z` does not live long enough
29     lock.use_ref(); // (Mutex is #[may_dangle] so its dtor does not use `z` => needs explicit use)
30 }
31
32 fn rwlock() {
33     let x = 1;
34     let y = Box::new(1);
35     let lock = RwLock::new(&x);
36     *lock.write().unwrap() = &*y;
37     drop(y); //~ ERROR cannot move out
38     {
39         let z = 2;
40         *lock.write().unwrap() = &z;
41     }
42     //~^^ ERROR `z` does not live long enough
43     lock.use_ref(); // (RwLock is #[may_dangle] so its dtor does not use `z` => needs explicit use)
44 }
45
46 fn channel() {
47     let x = 1;
48     let y = Box::new(1);
49     let (tx, rx) = mpsc::channel();
50
51     tx.send(&x).unwrap();
52     tx.send(&*y);
53     drop(y); //~ ERROR cannot move out
54     {
55         let z = 2;
56         tx.send(&z).unwrap();
57     }
58     //~^^ ERROR `z` does not live long enough
59     // (channels lack #[may_dangle], thus their dtors are implicit uses of `z`)
60 }
61
62 fn main() {}
63
64 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
65 impl<T> Fake for T { }