]> git.proxmox.com Git - rustc.git/blob - src/test/ui/macros/stringify.rs
Merge tag 'debian/1.65.0+dfsg1-1_exp3' into debian/sid
[rustc.git] / src / test / ui / macros / stringify.rs
1 // run-pass
2 // edition:2021
3 // compile-flags: --test
4
5 #![feature(async_closure)]
6 #![feature(box_patterns)]
7 #![feature(box_syntax)]
8 #![feature(const_trait_impl)]
9 #![feature(decl_macro)]
10 #![feature(generators)]
11 #![feature(half_open_range_patterns)]
12 #![feature(more_qualified_paths)]
13 #![feature(raw_ref_op)]
14 #![feature(trait_alias)]
15 #![feature(try_blocks)]
16 #![feature(type_ascription)]
17 #![deny(unused_macros)]
18
19 macro_rules! stringify_block {
20 ($block:block) => {
21 stringify!($block)
22 };
23 }
24
25 macro_rules! stringify_expr {
26 ($expr:expr) => {
27 stringify!($expr)
28 };
29 }
30
31 macro_rules! stringify_item {
32 ($item:item) => {
33 stringify!($item)
34 };
35 }
36
37 macro_rules! stringify_meta {
38 ($meta:meta) => {
39 stringify!($meta)
40 };
41 }
42
43 macro_rules! stringify_pat {
44 ($pat:pat) => {
45 stringify!($pat)
46 };
47 }
48
49 macro_rules! stringify_path {
50 ($path:path) => {
51 stringify!($path)
52 };
53 }
54
55 macro_rules! stringify_stmt {
56 ($stmt:stmt) => {
57 stringify!($stmt)
58 };
59 }
60
61 macro_rules! stringify_ty {
62 ($ty:ty) => {
63 stringify!($ty)
64 };
65 }
66
67 macro_rules! stringify_vis {
68 ($vis:vis) => {
69 stringify!($vis)
70 };
71 }
72
73 #[test]
74 fn test_block() {
75 assert_eq!(stringify_block!({}), "{}");
76 assert_eq!(stringify_block!({ true }), "{ true }");
77 assert_eq!(stringify_block!({ return }), "{ return }");
78 assert_eq!(
79 stringify_block!({
80 return;
81 }),
82 "{ return; }",
83 );
84 assert_eq!(
85 stringify_block!({
86 let _;
87 true
88 }),
89 "{ let _; true }",
90 );
91 }
92
93 #[test]
94 fn test_expr() {
95 // ExprKind::Box
96 assert_eq!(stringify_expr!(box expr), "box expr");
97
98 // ExprKind::Array
99 assert_eq!(stringify_expr!([]), "[]");
100 assert_eq!(stringify_expr!([true]), "[true]");
101 assert_eq!(stringify_expr!([true,]), "[true]");
102 assert_eq!(stringify_expr!([true, true]), "[true, true]");
103
104 // ExprKind::Call
105 assert_eq!(stringify_expr!(f()), "f()");
106 assert_eq!(stringify_expr!(f::<u8>()), "f::<u8>()");
107 assert_eq!(stringify_expr!(f::<1>()), "f::<1>()");
108 assert_eq!(stringify_expr!(f::<'a, u8, 1>()), "f::<'a, u8, 1>()");
109 assert_eq!(stringify_expr!(f(true)), "f(true)");
110 assert_eq!(stringify_expr!(f(true,)), "f(true)");
111 assert_eq!(stringify_expr!(()()), "()()");
112
113 // ExprKind::MethodCall
114 assert_eq!(stringify_expr!(x.f()), "x.f()");
115 assert_eq!(stringify_expr!(x.f::<u8>()), "x.f::<u8>()");
116
117 // ExprKind::Tup
118 assert_eq!(stringify_expr!(()), "()");
119 assert_eq!(stringify_expr!((true,)), "(true,)");
120 assert_eq!(stringify_expr!((true, false)), "(true, false)");
121 assert_eq!(stringify_expr!((true, false,)), "(true, false)");
122
123 // ExprKind::Binary
124 assert_eq!(stringify_expr!(true || false), "true || false");
125 assert_eq!(stringify_expr!(true || false && false), "true || false && false");
126
127 // ExprKind::Unary
128 assert_eq!(stringify_expr!(*expr), "*expr");
129 assert_eq!(stringify_expr!(!expr), "!expr");
130 assert_eq!(stringify_expr!(-expr), "-expr");
131
132 // ExprKind::Lit
133 assert_eq!(stringify_expr!('x'), "'x'");
134 assert_eq!(stringify_expr!(1_000_i8), "1_000_i8");
135 assert_eq!(stringify_expr!(1.00000000000000001), "1.00000000000000001");
136
137 // ExprKind::Cast
138 assert_eq!(stringify_expr!(expr as T), "expr as T");
139 assert_eq!(stringify_expr!(expr as T<u8>), "expr as T<u8>");
140
141 // ExprKind::Type
142 assert_eq!(stringify_expr!(expr: T), "expr: T");
143 assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
144
145 // ExprKind::If
146 assert_eq!(stringify_expr!(if true {}), "if true {}");
147 assert_eq!(
148 stringify_expr!(if true {
149 } else {
150 }),
151 "if true {} else {}",
152 );
153 assert_eq!(
154 stringify_expr!(if let true = true {
155 } else {
156 }),
157 "if let true = true {} else {}",
158 );
159 assert_eq!(
160 stringify_expr!(if true {
161 } else if false {
162 }),
163 "if true {} else if false {}",
164 );
165 assert_eq!(
166 stringify_expr!(if true {
167 } else if false {
168 } else {
169 }),
170 "if true {} else if false {} else {}",
171 );
172 assert_eq!(
173 stringify_expr!(if true {
174 return;
175 } else if false {
176 0
177 } else {
178 0
179 }),
180 "if true { return; } else if false { 0 } else { 0 }",
181 );
182
183 // ExprKind::While
184 assert_eq!(stringify_expr!(while true {}), "while true {}");
185 assert_eq!(stringify_expr!('a: while true {}), "'a: while true {}");
186 assert_eq!(stringify_expr!(while let true = true {}), "while let true = true {}");
187
188 // ExprKind::ForLoop
189 assert_eq!(stringify_expr!(for _ in x {}), "for _ in x {}");
190 assert_eq!(stringify_expr!('a: for _ in x {}), "'a: for _ in x {}");
191
192 // ExprKind::Loop
193 assert_eq!(stringify_expr!(loop {}), "loop {}");
194 assert_eq!(stringify_expr!('a: loop {}), "'a: loop {}");
195
196 // ExprKind::Match
197 assert_eq!(stringify_expr!(match self {}), "match self {}");
198 assert_eq!(
199 stringify_expr!(match self {
200 Ok => 1,
201 }),
202 "match self { Ok => 1, }",
203 );
204 assert_eq!(
205 stringify_expr!(match self {
206 Ok => 1,
207 Err => 0,
208 }),
209 "match self { Ok => 1, Err => 0, }",
210 );
211
212 // ExprKind::Closure
213 assert_eq!(stringify_expr!(|| {}), "|| {}");
214 assert_eq!(stringify_expr!(|x| {}), "|x| {}");
215 assert_eq!(stringify_expr!(|x: u8| {}), "|x: u8| {}");
216 assert_eq!(stringify_expr!(|| ()), "|| ()");
217 assert_eq!(stringify_expr!(move || self), "move || self");
218 assert_eq!(stringify_expr!(async || self), "async || self");
219 assert_eq!(stringify_expr!(async move || self), "async move || self");
220 assert_eq!(stringify_expr!(static || self), "static || self");
221 assert_eq!(stringify_expr!(static move || self), "static move || self");
222 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
223 assert_eq!(
224 stringify_expr!(static async || self),
225 "static async || self",
226 );
227 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
228 assert_eq!(
229 stringify_expr!(static async move || self),
230 "static async move || self",
231 );
232 assert_eq!(stringify_expr!(|| -> u8 { self }), "|| -> u8 { self }");
233 assert_eq!(stringify_expr!(1 + || {}), "1 + (|| {})"); // ??
234
235 // ExprKind::Block
236 assert_eq!(stringify_expr!({}), "{}");
237 assert_eq!(stringify_expr!(unsafe {}), "unsafe {}");
238 assert_eq!(stringify_expr!('a: {}), "'a: {}");
239 assert_eq!(
240 stringify_expr!(
241 #[attr]
242 {}
243 ),
244 "#[attr] {}",
245 );
246 assert_eq!(
247 stringify_expr!(
248 {
249 #![attr]
250 }
251 ),
252 "{\n\
253 \x20 #![attr]\n\
254 }",
255 );
256
257 // ExprKind::Async
258 assert_eq!(stringify_expr!(async {}), "async {}");
259 assert_eq!(stringify_expr!(async move {}), "async move {}");
260
261 // ExprKind::Await
262 assert_eq!(stringify_expr!(expr.await), "expr.await");
263
264 // ExprKind::TryBlock
265 assert_eq!(stringify_expr!(try {}), "try {}");
266
267 // ExprKind::Assign
268 assert_eq!(stringify_expr!(expr = true), "expr = true");
269
270 // ExprKind::AssignOp
271 assert_eq!(stringify_expr!(expr += true), "expr += true");
272
273 // ExprKind::Field
274 assert_eq!(stringify_expr!(expr.field), "expr.field");
275 assert_eq!(stringify_expr!(expr.0), "expr.0");
276
277 // ExprKind::Index
278 assert_eq!(stringify_expr!(expr[true]), "expr[true]");
279
280 // ExprKind::Range
281 assert_eq!(stringify_expr!(..), "..");
282 assert_eq!(stringify_expr!(..hi), "..hi");
283 assert_eq!(stringify_expr!(lo..), "lo..");
284 assert_eq!(stringify_expr!(lo..hi), "lo..hi");
285 assert_eq!(stringify_expr!(..=hi), "..=hi");
286 assert_eq!(stringify_expr!(lo..=hi), "lo..=hi");
287 assert_eq!(stringify_expr!(-2..=-1), "-2..=-1");
288
289 // ExprKind::Path
290 assert_eq!(stringify_expr!(thing), "thing");
291 assert_eq!(stringify_expr!(m::thing), "m::thing");
292 assert_eq!(stringify_expr!(self::thing), "self::thing");
293 assert_eq!(stringify_expr!(crate::thing), "crate::thing");
294 assert_eq!(stringify_expr!(Self::thing), "Self::thing");
295 assert_eq!(stringify_expr!(<Self as T>::thing), "<Self as T>::thing");
296 assert_eq!(stringify_expr!(Self::<'static>), "Self::<'static>");
297
298 // ExprKind::AddrOf
299 assert_eq!(stringify_expr!(&expr), "&expr");
300 assert_eq!(stringify_expr!(&mut expr), "&mut expr");
301 assert_eq!(stringify_expr!(&raw const expr), "&raw const expr");
302 assert_eq!(stringify_expr!(&raw mut expr), "&raw mut expr");
303
304 // ExprKind::Break
305 assert_eq!(stringify_expr!(break), "break");
306 assert_eq!(stringify_expr!(break 'a), "break 'a");
307 assert_eq!(stringify_expr!(break true), "break true");
308 assert_eq!(stringify_expr!(break 'a true), "break 'a true");
309
310 // ExprKind::Continue
311 assert_eq!(stringify_expr!(continue), "continue");
312 assert_eq!(stringify_expr!(continue 'a), "continue 'a");
313
314 // ExprKind::Ret
315 assert_eq!(stringify_expr!(return), "return");
316 assert_eq!(stringify_expr!(return true), "return true");
317
318 // ExprKind::MacCall
319 assert_eq!(stringify_expr!(mac!(...)), "mac!(...)");
320 assert_eq!(stringify_expr!(mac![...]), "mac![...]");
321 assert_eq!(stringify_expr!(mac! { ... }), "mac! { ... }");
322
323 // ExprKind::Struct
324 assert_eq!(stringify_expr!(Struct {}), "Struct {}");
325 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
326 assert_eq!(stringify_expr!(<Struct as Trait>::Type {}), "<Struct as Trait>::Type {}");
327 assert_eq!(stringify_expr!(Struct { .. }), "Struct { .. }");
328 assert_eq!(stringify_expr!(Struct { ..base }), "Struct { ..base }");
329 assert_eq!(stringify_expr!(Struct { x }), "Struct { x }");
330 assert_eq!(stringify_expr!(Struct { x, .. }), "Struct { x, .. }");
331 assert_eq!(stringify_expr!(Struct { x, ..base }), "Struct { x, ..base }");
332 assert_eq!(stringify_expr!(Struct { x: true }), "Struct { x: true }");
333 assert_eq!(stringify_expr!(Struct { x: true, .. }), "Struct { x: true, .. }");
334 assert_eq!(stringify_expr!(Struct { x: true, ..base }), "Struct { x: true, ..base }");
335
336 // ExprKind::Repeat
337 assert_eq!(stringify_expr!([(); 0]), "[(); 0]");
338
339 // ExprKind::Paren
340 assert_eq!(stringify_expr!((expr)), "(expr)");
341
342 // ExprKind::Try
343 assert_eq!(stringify_expr!(expr?), "expr?");
344
345 // ExprKind::Yield
346 assert_eq!(stringify_expr!(yield), "yield");
347 assert_eq!(stringify_expr!(yield true), "yield true");
348 }
349
350 #[test]
351 fn test_item() {
352 // ItemKind::ExternCrate
353 assert_eq!(
354 stringify_item!(
355 extern crate std;
356 ),
357 "extern crate std;",
358 );
359 assert_eq!(
360 stringify_item!(
361 pub extern crate self as std;
362 ),
363 "pub extern crate self as std;",
364 );
365
366 // ItemKind::Use
367 assert_eq!(
368 stringify_item!(
369 pub use crate::{a, b::c};
370 ),
371 "pub use crate::{a, b::c};",
372 );
373
374 // ItemKind::Static
375 assert_eq!(
376 stringify_item!(
377 pub static S: () = {};
378 ),
379 "pub static S: () = {};",
380 );
381 assert_eq!(
382 stringify_item!(
383 static mut S: () = {};
384 ),
385 "static mut S: () = {};",
386 );
387 assert_eq!(
388 stringify_item!(
389 static S: ();
390 ),
391 "static S: ();",
392 );
393 assert_eq!(
394 stringify_item!(
395 static mut S: ();
396 ),
397 "static mut S: ();",
398 );
399
400 // ItemKind::Const
401 assert_eq!(
402 stringify_item!(
403 pub const S: () = {};
404 ),
405 "pub const S: () = {};",
406 );
407 assert_eq!(
408 stringify_item!(
409 const S: ();
410 ),
411 "const S: ();",
412 );
413
414 // ItemKind::Fn
415 assert_eq!(
416 stringify_item!(
417 pub default const async unsafe extern "C" fn f() {}
418 ),
419 "pub default const async unsafe extern \"C\" fn f() {}",
420 );
421
422 // ItemKind::Mod
423 assert_eq!(
424 stringify_item!(
425 pub mod m;
426 ),
427 "pub mod m;",
428 );
429 assert_eq!(
430 stringify_item!(
431 mod m {}
432 ),
433 "mod m {}",
434 );
435 assert_eq!(
436 stringify_item!(
437 unsafe mod m;
438 ),
439 "unsafe mod m;",
440 );
441 assert_eq!(
442 stringify_item!(
443 unsafe mod m {}
444 ),
445 "unsafe mod m {}",
446 );
447
448 // ItemKind::ForeignMod
449 assert_eq!(
450 stringify_item!(
451 extern "C" {}
452 ),
453 "extern \"C\" {}",
454 );
455 #[rustfmt::skip]
456 assert_eq!(
457 stringify_item!(
458 pub extern "C" {}
459 ),
460 "extern \"C\" {}",
461 );
462 assert_eq!(
463 stringify_item!(
464 unsafe extern "C++" {}
465 ),
466 "unsafe extern \"C++\" {}",
467 );
468
469 // ItemKind::TyAlias
470 #[rustfmt::skip]
471 assert_eq!(
472 stringify_item!(
473 pub default type Type<'a>: Bound
474 where
475 Self: 'a,
476 = T;
477 ),
478 "pub default type Type<'a>: Bound where Self: 'a = T;",
479 );
480
481 // ItemKind::Enum
482 assert_eq!(
483 stringify_item!(
484 pub enum Void {}
485 ),
486 "pub enum Void {}",
487 );
488 assert_eq!(
489 stringify_item!(
490 enum Empty {
491 Unit,
492 Tuple(),
493 Struct {},
494 }
495 ),
496 "enum Empty { Unit, Tuple(), Struct {}, }",
497 );
498 assert_eq!(
499 stringify_item!(
500 enum Enum<T>
501 where
502 T: 'a,
503 {
504 Unit,
505 Tuple(T),
506 Struct { t: T },
507 }
508 ),
509 "enum Enum<T> where T: 'a {\n\
510 \x20 Unit,\n\
511 \x20 Tuple(T),\n\
512 \x20 Struct {\n\
513 \x20 t: T,\n\
514 \x20 },\n\
515 }",
516 );
517
518 // ItemKind::Struct
519 assert_eq!(
520 stringify_item!(
521 pub struct Unit;
522 ),
523 "pub struct Unit;",
524 );
525 assert_eq!(
526 stringify_item!(
527 struct Tuple();
528 ),
529 "struct Tuple();",
530 );
531 assert_eq!(
532 stringify_item!(
533 struct Tuple(T);
534 ),
535 "struct Tuple(T);",
536 );
537 assert_eq!(
538 stringify_item!(
539 struct Struct {}
540 ),
541 "struct Struct {}",
542 );
543 assert_eq!(
544 stringify_item!(
545 struct Struct<T>
546 where
547 T: 'a,
548 {
549 t: T,
550 }
551 ),
552 "struct Struct<T> where T: 'a {\n\
553 \x20 t: T,\n\
554 }",
555 );
556
557 // ItemKind::Union
558 assert_eq!(
559 stringify_item!(
560 pub union Union {}
561 ),
562 "pub union Union {}",
563 );
564 assert_eq!(
565 stringify_item!(
566 union Union<T> where T: 'a {
567 t: T,
568 }
569 ),
570 "union Union<T> where T: 'a {\n\
571 \x20 t: T,\n\
572 }",
573 );
574
575 // ItemKind::Trait
576 assert_eq!(
577 stringify_item!(
578 pub unsafe auto trait Send {}
579 ),
580 "pub unsafe auto trait Send {}",
581 );
582 assert_eq!(
583 stringify_item!(
584 trait Trait<'a>: Sized
585 where
586 Self: 'a,
587 {
588 }
589 ),
590 "trait Trait<'a>: Sized where Self: 'a {}",
591 );
592
593 // ItemKind::TraitAlias
594 assert_eq!(
595 stringify_item!(
596 pub trait Trait<T> = Sized where T: 'a;
597 ),
598 "pub trait Trait<T> = Sized where T: 'a;",
599 );
600
601 // ItemKind::Impl
602 assert_eq!(
603 stringify_item!(
604 pub impl Struct {}
605 ),
606 "pub impl Struct {}",
607 );
608 assert_eq!(
609 stringify_item!(
610 impl<T> Struct<T> {}
611 ),
612 "impl<T> Struct<T> {}",
613 );
614 assert_eq!(
615 stringify_item!(
616 pub impl Trait for Struct {}
617 ),
618 "pub impl Trait for Struct {}",
619 );
620 assert_eq!(
621 stringify_item!(
622 impl<T> const Trait for T {}
623 ),
624 "impl<T> const Trait for T {}",
625 );
626 assert_eq!(
627 stringify_item!(
628 impl ~const Struct {}
629 ),
630 "impl Struct {}", // FIXME
631 );
632
633 // ItemKind::MacCall
634 assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
635 assert_eq!(stringify_item!(mac![...];), "mac![...];");
636 assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");
637
638 // ItemKind::MacroDef
639 assert_eq!(
640 stringify_item!(
641 macro_rules! stringify {
642 () => {};
643 }
644 ),
645 "macro_rules! stringify { () => {} ; }", // FIXME
646 );
647 assert_eq!(
648 stringify_item!(
649 pub macro stringify() {}
650 ),
651 "pub macro stringify { () => {} }",
652 );
653 }
654
655 #[test]
656 fn test_meta() {
657 assert_eq!(stringify_meta!(k), "k");
658 assert_eq!(stringify_meta!(k = "v"), "k = \"v\"");
659 assert_eq!(stringify_meta!(list(k1, k2 = "v")), "list(k1, k2 = \"v\")");
660 assert_eq!(stringify_meta!(serde::k), "serde::k");
661 }
662
663 #[test]
664 fn test_pat() {
665 // PatKind::Wild
666 assert_eq!(stringify_pat!(_), "_");
667
668 // PatKind::Ident
669 assert_eq!(stringify_pat!(_x), "_x");
670 assert_eq!(stringify_pat!(ref _x), "ref _x");
671 assert_eq!(stringify_pat!(mut _x), "mut _x");
672 assert_eq!(stringify_pat!(ref mut _x), "ref mut _x");
673 assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");
674
675 // PatKind::Struct
676 assert_eq!(stringify_pat!(Struct {}), "Struct {}");
677 assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {}");
678 assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}");
679 assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
680 assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
681 assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
682 assert_eq!(stringify_pat!(Struct { x, .. }), "Struct { x, .. }");
683 assert_eq!(stringify_pat!(Struct { x: _x, .. }), "Struct { x: _x, .. }");
684 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
685 assert_eq!(
686 stringify_pat!(<Struct as Trait>::Type {}),
687 "<Struct as Trait>::Type {}",
688 );
689
690 // PatKind::TupleStruct
691 assert_eq!(stringify_pat!(Tuple()), "Tuple()");
692 assert_eq!(stringify_pat!(Tuple::<u8>()), "Tuple::<u8>()");
693 assert_eq!(stringify_pat!(Tuple::<'static>()), "Tuple::<'static>()");
694 assert_eq!(stringify_pat!(Tuple(x)), "Tuple(x)");
695 assert_eq!(stringify_pat!(Tuple(..)), "Tuple(..)");
696 assert_eq!(stringify_pat!(Tuple(x, ..)), "Tuple(x, ..)");
697 assert_eq!(stringify_pat!(<Struct as Trait>::Type()), "<Struct as Trait>::Type()");
698
699 // PatKind::Or
700 assert_eq!(stringify_pat!(true | false), "true | false");
701 assert_eq!(stringify_pat!(| true), "true");
702 assert_eq!(stringify_pat!(|true| false), "true | false");
703
704 // PatKind::Path
705 assert_eq!(stringify_pat!(crate::Path), "crate::Path");
706 assert_eq!(stringify_pat!(Path::<u8>), "Path::<u8>");
707 assert_eq!(stringify_pat!(Path::<'static>), "Path::<'static>");
708 assert_eq!(stringify_pat!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
709
710 // PatKind::Tuple
711 assert_eq!(stringify_pat!(()), "()");
712 assert_eq!(stringify_pat!((true,)), "(true,)");
713 assert_eq!(stringify_pat!((true, false)), "(true, false)");
714
715 // PatKind::Box
716 assert_eq!(stringify_pat!(box pat), "box pat");
717
718 // PatKind::Ref
719 assert_eq!(stringify_pat!(&pat), "&pat");
720 assert_eq!(stringify_pat!(&mut pat), "&mut pat");
721
722 // PatKind::Lit
723 assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");
724
725 // PatKind::Range
726 assert_eq!(stringify_pat!(..1), "..1");
727 assert_eq!(stringify_pat!(0..), "0..");
728 assert_eq!(stringify_pat!(0..1), "0..1");
729 assert_eq!(stringify_pat!(0..=1), "0..=1");
730 assert_eq!(stringify_pat!(-2..=-1), "-2..=-1");
731
732 // PatKind::Slice
733 assert_eq!(stringify_pat!([]), "[]");
734 assert_eq!(stringify_pat!([true]), "[true]");
735 assert_eq!(stringify_pat!([true,]), "[true]");
736 assert_eq!(stringify_pat!([true, false]), "[true, false]");
737
738 // PatKind::Rest
739 assert_eq!(stringify_pat!(..), "..");
740
741 // PatKind::Paren
742 assert_eq!(stringify_pat!((pat)), "(pat)");
743
744 // PatKind::MacCall
745 assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
746 assert_eq!(stringify_pat!(mac![...]), "mac![...]");
747 assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
748 }
749
750 #[test]
751 fn test_path() {
752 assert_eq!(stringify_path!(thing), "thing");
753 assert_eq!(stringify_path!(m::thing), "m::thing");
754 assert_eq!(stringify_path!(self::thing), "self::thing");
755 assert_eq!(stringify_path!(crate::thing), "crate::thing");
756 assert_eq!(stringify_path!(Self::thing), "Self::thing");
757 assert_eq!(stringify_path!(Self<'static>), "Self<'static>");
758 assert_eq!(stringify_path!(Self::<'static>), "Self<'static>");
759 assert_eq!(stringify_path!(Self()), "Self()");
760 assert_eq!(stringify_path!(Self() -> ()), "Self() -> ()");
761 }
762
763 #[test]
764 fn test_stmt() {
765 // StmtKind::Local
766 assert_eq!(stringify_stmt!(let _), "let _;");
767 assert_eq!(stringify_stmt!(let x = true), "let x = true;");
768 assert_eq!(stringify_stmt!(let x: bool = true), "let x: bool = true;");
769
770 // StmtKind::Item
771 assert_eq!(
772 stringify_stmt!(
773 struct S;
774 ),
775 "struct S;",
776 );
777
778 // StmtKind::Expr
779 assert_eq!(stringify_stmt!(loop {}), "loop {}");
780
781 // StmtKind::Semi
782 assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");
783
784 // StmtKind::Empty
785 assert_eq!(stringify_stmt!(;), ";");
786
787 // StmtKind::MacCall
788 assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
789 assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
790 assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
791 }
792
793 #[test]
794 fn test_ty() {
795 // TyKind::Slice
796 assert_eq!(stringify_ty!([T]), "[T]");
797
798 // TyKind::Array
799 assert_eq!(stringify_ty!([T; 0]), "[T; 0]");
800
801 // TyKind::Ptr
802 assert_eq!(stringify_ty!(*const T), "*const T");
803 assert_eq!(stringify_ty!(*mut T), "*mut T");
804
805 // TyKind::Rptr
806 assert_eq!(stringify_ty!(&T), "&T");
807 assert_eq!(stringify_ty!(&mut T), "&mut T");
808 assert_eq!(stringify_ty!(&'a T), "&'a T");
809 assert_eq!(stringify_ty!(&'a mut T), "&'a mut T");
810
811 // TyKind::BareFn
812 assert_eq!(stringify_ty!(fn()), "fn()");
813 assert_eq!(stringify_ty!(fn() -> ()), "fn() -> ()");
814 assert_eq!(stringify_ty!(fn(u8)), "fn(u8)");
815 assert_eq!(stringify_ty!(fn(x: u8)), "fn(x: u8)");
816 #[rustfmt::skip]
817 assert_eq!(stringify_ty!(for<> fn()), "fn()");
818 assert_eq!(stringify_ty!(for<'a> fn()), "for<'a> fn()");
819
820 // TyKind::Never
821 assert_eq!(stringify_ty!(!), "!");
822
823 // TyKind::Tup
824 assert_eq!(stringify_ty!(()), "()");
825 assert_eq!(stringify_ty!((T,)), "(T,)");
826 assert_eq!(stringify_ty!((T, U)), "(T, U)");
827
828 // TyKind::Path
829 assert_eq!(stringify_ty!(T), "T");
830 assert_eq!(stringify_ty!(Ref<'a>), "Ref<'a>");
831 assert_eq!(stringify_ty!(PhantomData<T>), "PhantomData<T>");
832 assert_eq!(stringify_ty!(PhantomData::<T>), "PhantomData<T>");
833 assert_eq!(stringify_ty!(Fn() -> !), "Fn() -> !");
834 assert_eq!(stringify_ty!(Fn(u8) -> !), "Fn(u8) -> !");
835 assert_eq!(stringify_ty!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
836
837 // TyKind::TraitObject
838 assert_eq!(stringify_ty!(dyn Send), "dyn Send");
839 assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
840 assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
841 assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
842 assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
843 assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");
844
845 // TyKind::ImplTrait
846 assert_eq!(stringify_ty!(impl Send), "impl Send");
847 assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
848 assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
849 assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
850 assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
851 assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");
852
853 // TyKind::Paren
854 assert_eq!(stringify_ty!((T)), "(T)");
855
856 // TyKind::Infer
857 assert_eq!(stringify_ty!(_), "_");
858
859 // TyKind::MacCall
860 assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
861 assert_eq!(stringify_ty!(mac![...]), "mac![...]");
862 assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
863 }
864
865 #[test]
866 fn test_vis() {
867 // VisibilityKind::Public
868 assert_eq!(stringify_vis!(pub), "pub ");
869
870 // VisibilityKind::Restricted
871 assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
872 assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
873 assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
874 assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
875 assert_eq!(stringify_vis!(pub(in self)), "pub(in self) ");
876 assert_eq!(stringify_vis!(pub(in super)), "pub(in super) ");
877 assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
878 assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
879 assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
880 assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super::path::to) ");
881
882 // VisibilityKind::Inherited
883 // Directly calling `stringify_vis!()` does not work.
884 macro_rules! stringify_inherited_vis {
885 ($vis:vis struct) => {
886 stringify_vis!($vis)
887 };
888 }
889 assert_eq!(stringify_inherited_vis!(struct), "");
890 }