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