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