1 #[macro_use]
2 mod compiletest;
3 
4 #[rustversion::attr(not(nightly), ignore)]
5 #[test]
ui()6 fn ui() {
7     let t = trybuild::TestCases::new();
8     t.compile_fail("tests/ui/*.rs");
9 }
10 
11 assert_no_panic! {
12     mod test_readme {
13         #[no_panic]
14         fn demo(s: &str) -> &str {
15             &s[1..]
16         }
17 
18         fn main() {
19             println!("{}", demo("input string"));
20         }
21     }
22 
23     mod test_method_in_impl {
24         struct S;
25 
26         impl S {
27             #[no_panic]
28             fn demo(self) -> &'static str {
29                 "test"
30             }
31         }
32 
33         fn main() {
34             println!("{}", S.demo());
35         }
36     }
37 
38     mod test_lifetime_elision {
39         struct Buffer {
40             bytes: [u8; 24],
41         }
42 
43         #[no_panic]
44         fn demo(buffer: &mut Buffer) -> &[u8] {
45             &buffer.bytes[..]
46         }
47 
48         fn main() {
49             let mut buffer = Buffer {
50                 bytes: [0u8; 24],
51             };
52             println!("{:?}", demo(&mut buffer));
53         }
54     }
55 
56     mod test_receiver_lifetime_elision {
57         struct Buffer {
58             bytes: [u8; 24],
59         }
60 
61         impl Buffer {
62             #[no_panic]
63             fn demo(&mut self, _s: &str) -> &[u8] {
64                 &self.bytes[..]
65             }
66         }
67 
68         fn main() {
69             let mut buffer = Buffer {
70                 bytes: [0u8; 24],
71             };
72             println!("{:?}", buffer.demo(""));
73         }
74     }
75 
76     mod test_ref_argument {
77         #[no_panic]
78         fn demo(ref i: i32) -> i32 {
79             *i
80         }
81 
82         fn main() {
83             println!("{}", demo(0));
84         }
85     }
86 
87     mod test_mut_argument {
88         #[no_panic]
89         fn demo(mut i: i32) -> i32 {
90             i += 1;
91             i
92         }
93 
94         fn main() {
95             println!("{}", demo(0));
96         }
97     }
98 
99     mod test_ref_mut_argument {
100         #[no_panic]
101         fn demo(ref mut i: i32) -> i32 {
102             *i += 1;
103             *i
104         }
105 
106         fn main() {
107             println!("{}", demo(0));
108         }
109     }
110 
111     mod test_borrow_from_mut_self {
112         struct S {
113             data: usize,
114         }
115 
116         impl S {
117             #[no_panic]
118             fn get_mut(&mut self) -> &mut usize {
119                 &mut self.data
120             }
121         }
122 
123         fn main() {
124             let mut s = S { data: 0 };
125             println!("{}", s.get_mut());
126         }
127     }
128 
129     mod test_self_in_macro {
130         struct S {
131             data: usize,
132         }
133 
134         macro_rules! id {
135             ($expr:expr) => {
136                 $expr
137             };
138         }
139 
140         impl S {
141             #[no_panic]
142             fn get_mut(&mut self) -> usize {
143                 id![self.data]
144             }
145         }
146 
147         fn main() {
148             let mut s = S { data: 0 };
149             println!("{}", s.get_mut());
150         }
151     }
152 
153     mod test_self_in_macro_containing_fn {
154         pub struct S {
155             data: usize,
156         }
157 
158         macro_rules! emit {
159             ($($tt:tt)*) => {
160                 $($tt)*
161             };
162         }
163 
164         impl S {
165             #[no_panic]
166             fn get_mut(&mut self) -> usize {
167                 let _ = emit!({
168                     impl S {
169                         pub fn f(self) {}
170                     }
171                 });
172                 self.data
173             }
174         }
175 
176         fn main() {
177             let mut s = S { data: 0 };
178             println!("{}", s.get_mut());
179         }
180     }
181 
182     mod test_self_with_std_pin {
183         use std::pin::Pin;
184 
185         pub struct S;
186 
187         impl S {
188             #[no_panic]
189             fn f(mut self: Pin<&mut Self>) {
190                 let _ = self.as_mut();
191             }
192         }
193 
194         fn main() {}
195     }
196 
197     mod test_deref_coercion {
198         #[no_panic]
199         pub fn f(s: &str) -> &str {
200             &s
201         }
202 
203         fn main() {}
204     }
205 
206     mod test_return_impl_trait {
207         use std::io;
208 
209         #[no_panic]
210         pub fn f() -> io::Result<impl io::Write> {
211             Ok(Vec::new())
212         }
213 
214         fn main() {}
215     }
216 
217     mod test_conditional_return {
218         #[no_panic]
219         pub fn f(i: i32) {
220             if i < 0 {
221                 return;
222             }
223         }
224 
225         fn main() {
226             println!("{:?}", f(-1));
227         }
228     }
229 
230     mod test_conditional_return_macro {
231         macro_rules! return_if_negative {
232             ($e:expr) => {
233                 if $e < 0 {
234                     return;
235                 }
236             }
237         }
238 
239         #[no_panic]
240         pub fn f(i: i32) {
241             return_if_negative!(i);
242         }
243 
244         fn main() {
245             println!("{:?}", f(-1));
246         }
247     }
248 }
249 
250 assert_link_error! {
251     mod test_readme {
252         #[no_panic]
253         fn demo(s: &str) -> &str {
254             &s[1..]
255         }
256 
257         fn main() {
258             println!("{}", demo("\u{1f980}input string"));
259         }
260     }
261 }
262