]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/macro_expansion_tests/builtin_fn_macro.rs
6e91301ecdc896508286d327c77cf170355fe3ff
[rust.git] / crates / hir_def / src / macro_expansion_tests / builtin_fn_macro.rs
1 //! Tests for `builtin_fn_macro.rs` from `hir_expand`.
2
3 use expect_test::expect;
4
5 use crate::macro_expansion_tests::check;
6
7 #[test]
8 fn test_column_expand() {
9     check(
10         r#"
11 #[rustc_builtin_macro]
12 macro_rules! column {() => {}}
13
14 fn main() { column!(); }
15 "#,
16         expect![[r##"
17 #[rustc_builtin_macro]
18 macro_rules! column {() => {}}
19
20 fn main() { 0; }
21 "##]],
22     );
23 }
24
25 #[test]
26 fn test_line_expand() {
27     check(
28         r#"
29 #[rustc_builtin_macro]
30 macro_rules! line {() => {}}
31
32 fn main() { line!() }
33 "#,
34         expect![[r##"
35 #[rustc_builtin_macro]
36 macro_rules! line {() => {}}
37
38 fn main() { 0 }
39 "##]],
40     );
41 }
42
43 #[test]
44 fn test_stringify_expand() {
45     check(
46         r#"
47 #[rustc_builtin_macro]
48 macro_rules! stringify {() => {}}
49
50 fn main() {
51     stringify!(
52         a
53         b
54         c
55     );
56 }
57 "#,
58         expect![[r##"
59 #[rustc_builtin_macro]
60 macro_rules! stringify {() => {}}
61
62 fn main() {
63     "a b c";
64 }
65 "##]],
66     );
67 }
68
69 #[test]
70 fn test_env_expand() {
71     check(
72         r#"
73 #[rustc_builtin_macro]
74 macro_rules! env {() => {}}
75
76 fn main() { env!("TEST_ENV_VAR"); }
77 "#,
78         expect![[r##"
79 #[rustc_builtin_macro]
80 macro_rules! env {() => {}}
81
82 fn main() { "__RA_UNIMPLEMENTED__"; }
83 "##]],
84     );
85 }
86
87 #[test]
88 fn test_option_env_expand() {
89     check(
90         r#"
91 #[rustc_builtin_macro]
92 macro_rules! option_env {() => {}}
93
94 fn main() { option_env!("TEST_ENV_VAR"); }
95 "#,
96         expect![[r##"
97 #[rustc_builtin_macro]
98 macro_rules! option_env {() => {}}
99
100 fn main() { std::option::Option::None:: < &str>; }
101 "##]],
102     );
103 }
104
105 #[test]
106 fn test_file_expand() {
107     check(
108         r#"
109 #[rustc_builtin_macro]
110 macro_rules! file {() => {}}
111
112 fn main() { file!(); }
113 "#,
114         expect![[r##"
115 #[rustc_builtin_macro]
116 macro_rules! file {() => {}}
117
118 fn main() { ""; }
119 "##]],
120     );
121 }
122
123 #[test]
124 fn test_assert_expand() {
125     check(
126         r#"
127 #[rustc_builtin_macro]
128 macro_rules! assert {
129     ($cond:expr) => ({ /* compiler built-in */ });
130     ($cond:expr, $($args:tt)*) => ({ /* compiler built-in */ })
131 }
132
133 fn main() {
134     assert!(true, "{} {:?}", arg1(a, b, c), arg2);
135 }
136 "#,
137         expect![[r##"
138 #[rustc_builtin_macro]
139 macro_rules! assert {
140     ($cond:expr) => ({ /* compiler built-in */ });
141     ($cond:expr, $($args:tt)*) => ({ /* compiler built-in */ })
142 }
143
144 fn main() {
145      {
146         if !true {
147             $crate::panic!("{} {:?}", arg1(a, b, c), arg2);
148         }
149     };
150 }
151 "##]],
152     );
153 }
154
155 #[test]
156 fn test_compile_error_expand() {
157     check(
158         r#"
159 #[rustc_builtin_macro]
160 macro_rules! compile_error {
161     ($msg:expr) => ({ /* compiler built-in */ });
162     ($msg:expr,) => ({ /* compiler built-in */ })
163 }
164
165 // This expands to nothing (since it's in item position), but emits an error.
166 compile_error!("error!");
167 "#,
168         expect![[r##"
169 #[rustc_builtin_macro]
170 macro_rules! compile_error {
171     ($msg:expr) => ({ /* compiler built-in */ });
172     ($msg:expr,) => ({ /* compiler built-in */ })
173 }
174
175 /* error: error! */
176 "##]],
177     );
178 }
179
180 #[test]
181 fn test_format_args_expand() {
182     check(
183         r#"
184 #[rustc_builtin_macro]
185 macro_rules! format_args {
186     ($fmt:expr) => ({ /* compiler built-in */ });
187     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
188 }
189
190 fn main() {
191     format_args!("{} {:?}", arg1(a, b, c), arg2);
192 }
193 "#,
194         expect![[r##"
195 #[rustc_builtin_macro]
196 macro_rules! format_args {
197     ($fmt:expr) => ({ /* compiler built-in */ });
198     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
199 }
200
201 fn main() {
202     unsafe {
203         std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a, b, c)), std::fmt::Display::fmt), std::fmt::ArgumentV1::new(&(arg2), std::fmt::Display::fmt), ])
204     };
205 }
206 "##]],
207     );
208 }
209
210 #[test]
211 fn test_format_args_expand_with_comma_exprs() {
212     check(
213         r#"
214 #[rustc_builtin_macro]
215 macro_rules! format_args {
216     ($fmt:expr) => ({ /* compiler built-in */ });
217     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
218 }
219
220 fn main() {
221     format_args!("{} {:?}", a::<A,B>(), b);
222 }
223 "#,
224         expect![[r##"
225 #[rustc_builtin_macro]
226 macro_rules! format_args {
227     ($fmt:expr) => ({ /* compiler built-in */ });
228     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
229 }
230
231 fn main() {
232     unsafe {
233         std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A, B>()), std::fmt::Display::fmt), std::fmt::ArgumentV1::new(&(b), std::fmt::Display::fmt), ])
234     };
235 }
236 "##]],
237     );
238 }
239
240 #[test]
241 fn test_format_args_expand_with_broken_member_access() {
242     check(
243         r#"
244 #[rustc_builtin_macro]
245 macro_rules! format_args {
246     ($fmt:expr) => ({ /* compiler built-in */ });
247     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
248 }
249
250 fn main() {
251     let _ =
252         // +errors
253         format_args!("{} {:?}", a.);
254 }
255 "#,
256         expect![[r##"
257 #[rustc_builtin_macro]
258 macro_rules! format_args {
259     ($fmt:expr) => ({ /* compiler built-in */ });
260     ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
261 }
262
263 fn main() {
264     let _ =
265         /* parse error: expected field name or number */
266 unsafe {
267             std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a.), std::fmt::Display::fmt), ])
268         };
269 }
270 "##]],
271     );
272 }
273
274 #[test]
275 fn test_include_bytes_expand() {
276     check(
277         r#"
278 #[rustc_builtin_macro]
279 macro_rules! include_bytes {
280     ($file:expr) => {{ /* compiler built-in */ }};
281     ($file:expr,) => {{ /* compiler built-in */ }};
282 }
283
284 fn main() { include_bytes("foo"); }
285 "#,
286         expect![[r##"
287 #[rustc_builtin_macro]
288 macro_rules! include_bytes {
289     ($file:expr) => {{ /* compiler built-in */ }};
290     ($file:expr,) => {{ /* compiler built-in */ }};
291 }
292
293 fn main() { include_bytes("foo"); }
294 "##]],
295     );
296 }
297
298 #[test]
299 fn test_concat_expand() {
300     check(
301         r##"
302 #[rustc_builtin_macro]
303 macro_rules! concat {}
304
305 fn main() { concat!("foo", "r", 0, r#"bar"#, "\n", false); }
306 "##,
307         expect![[r##"
308 #[rustc_builtin_macro]
309 macro_rules! concat {}
310
311 fn main() { "foor0bar\nfalse"; }
312 "##]],
313     );
314 }
315
316 #[test]
317 fn test_concat_with_captured_expr() {
318     check(
319         r##"
320 #[rustc_builtin_macro]
321 macro_rules! concat {}
322
323 macro_rules! surprise {
324     () => { "s" };
325 }
326
327 macro_rules! stuff {
328     ($string:expr) => { concat!($string) };
329 }
330
331 fn main() { concat!(surprise!()); }
332 "##,
333         expect![[r##"
334 #[rustc_builtin_macro]
335 macro_rules! concat {}
336
337 macro_rules! surprise {
338     () => { "s" };
339 }
340
341 macro_rules! stuff {
342     ($string:expr) => { concat!($string) };
343 }
344
345 fn main() { "s"; }
346 "##]],
347     );
348 }
349
350 #[test]
351 fn test_concat_idents_expand() {
352     check(
353         r##"
354 #[rustc_builtin_macro]
355 macro_rules! concat_idents {}
356
357 fn main() { concat_idents!(foo, bar); }
358 "##,
359         expect![[r##"
360 #[rustc_builtin_macro]
361 macro_rules! concat_idents {}
362
363 fn main() { foobar; }
364 "##]],
365     );
366 }