]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dynamic-drop.rs
Add invalid unary operator usage error code
[rust.git] / src / test / run-pass / dynamic-drop.rs
1 // Copyright 2016 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 #![feature(untagged_unions)]
12
13 use std::cell::{Cell, RefCell};
14 use std::panic;
15 use std::usize;
16
17 struct InjectedFailure;
18
19 struct Allocator {
20     data: RefCell<Vec<bool>>,
21     failing_op: usize,
22     cur_ops: Cell<usize>,
23 }
24
25 impl panic::UnwindSafe for Allocator {}
26 impl panic::RefUnwindSafe for Allocator {}
27
28 impl Drop for Allocator {
29     fn drop(&mut self) {
30         let data = self.data.borrow();
31         if data.iter().any(|d| *d) {
32             panic!("missing free: {:?}", data);
33         }
34     }
35 }
36
37 impl Allocator {
38     fn new(failing_op: usize) -> Self {
39         Allocator {
40             failing_op: failing_op,
41             cur_ops: Cell::new(0),
42             data: RefCell::new(vec![])
43         }
44     }
45     fn alloc(&self) -> Ptr {
46         self.cur_ops.set(self.cur_ops.get() + 1);
47
48         if self.cur_ops.get() == self.failing_op {
49             panic!(InjectedFailure);
50         }
51
52         let mut data = self.data.borrow_mut();
53         let addr = data.len();
54         data.push(true);
55         Ptr(addr, self)
56     }
57 }
58
59 struct Ptr<'a>(usize, &'a Allocator);
60 impl<'a> Drop for Ptr<'a> {
61     fn drop(&mut self) {
62         match self.1.data.borrow_mut()[self.0] {
63             false => {
64                 panic!("double free at index {:?}", self.0)
65             }
66             ref mut d => *d = false
67         }
68
69         self.1.cur_ops.set(self.1.cur_ops.get()+1);
70
71         if self.1.cur_ops.get() == self.1.failing_op {
72             panic!(InjectedFailure);
73         }
74     }
75 }
76
77 fn dynamic_init(a: &Allocator, c: bool) {
78     let _x;
79     if c {
80         _x = Some(a.alloc());
81     }
82 }
83
84 fn dynamic_drop(a: &Allocator, c: bool) {
85     let x = a.alloc();
86     if c {
87         Some(x)
88     } else {
89         None
90     };
91 }
92
93 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
94     let mut _v = a.alloc();
95     let mut _w = a.alloc();
96     if c0 {
97         drop(_v);
98     }
99     _v = _w;
100     if c1 {
101         _w = a.alloc();
102     }
103 }
104
105 fn assignment1(a: &Allocator, c0: bool) {
106     let mut _v = a.alloc();
107     let mut _w = a.alloc();
108     if c0 {
109         drop(_v);
110     }
111     _v = _w;
112 }
113
114 #[allow(unions_with_drop_fields)]
115 union Boxy<T> {
116     a: T,
117     b: T,
118 }
119
120 fn union1(a: &Allocator) {
121     unsafe {
122         let mut u = Boxy { a: a.alloc() };
123         u.b = a.alloc();
124         drop(u.a);
125     }
126 }
127
128 fn run_test<F>(mut f: F)
129     where F: FnMut(&Allocator)
130 {
131     let first_alloc = Allocator::new(usize::MAX);
132     f(&first_alloc);
133
134     for failing_op in 1..first_alloc.cur_ops.get()+1 {
135         let alloc = Allocator::new(failing_op);
136         let alloc = &alloc;
137         let f = panic::AssertUnwindSafe(&mut f);
138         let result = panic::catch_unwind(move || {
139             f.0(alloc);
140         });
141         match result {
142             Ok(..) => panic!("test executed {} ops but now {}",
143                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
144             Err(e) => {
145                 if e.downcast_ref::<InjectedFailure>().is_none() {
146                     panic::resume_unwind(e);
147                 }
148             }
149         }
150     }
151 }
152
153 fn run_test_nopanic<F>(mut f: F)
154     where F: FnMut(&Allocator)
155 {
156     let first_alloc = Allocator::new(usize::MAX);
157     f(&first_alloc);
158 }
159
160 fn main() {
161     run_test(|a| dynamic_init(a, false));
162     run_test(|a| dynamic_init(a, true));
163     run_test(|a| dynamic_drop(a, false));
164     run_test(|a| dynamic_drop(a, true));
165
166     run_test(|a| assignment2(a, false, false));
167     run_test(|a| assignment2(a, false, true));
168     run_test(|a| assignment2(a, true, false));
169     run_test(|a| assignment2(a, true, true));
170
171     run_test(|a| assignment1(a, false));
172     run_test(|a| assignment1(a, true));
173
174     run_test_nopanic(|a| union1(a));
175 }