]> git.lizzy.rs Git - rust.git/blob - src/test/ui/macros/stringify.rs
Format with rust-lang/rust's rustfmt settings
[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] { }", // FIXME
239     );
240     assert_eq!(
241         stringify_expr!(
242             {
243                 #![attr]
244             }
245         ),
246         "{\n    #![attr]\n}",
247     );
248
249     // ExprKind::Async
250     assert_eq!(stringify_expr!(async {}), "async {}");
251     assert_eq!(stringify_expr!(async move {}), "async move {}");
252
253     // ExprKind::Await
254     assert_eq!(stringify_expr!(expr.await), "expr.await");
255
256     // ExprKind::TryBlock
257     assert_eq!(stringify_expr!(try {}), "try  {}"); // FIXME
258
259     // ExprKind::Assign
260     assert_eq!(stringify_expr!(expr = true), "expr = true");
261
262     // ExprKind::AssignOp
263     assert_eq!(stringify_expr!(expr += true), "expr += true");
264
265     // ExprKind::Field
266     assert_eq!(stringify_expr!(expr.field), "expr.field");
267     assert_eq!(stringify_expr!(expr.0), "expr.0");
268
269     // ExprKind::Index
270     assert_eq!(stringify_expr!(expr[true]), "expr[true]");
271
272     // ExprKind::Range
273     assert_eq!(stringify_expr!(..), "..");
274     assert_eq!(stringify_expr!(..hi), "..hi");
275     assert_eq!(stringify_expr!(lo..), "lo..");
276     assert_eq!(stringify_expr!(lo..hi), "lo..hi");
277     assert_eq!(stringify_expr!(..=hi), "..=hi");
278     assert_eq!(stringify_expr!(lo..=hi), "lo..=hi");
279     assert_eq!(stringify_expr!(-2..=-1), "-2..=-1");
280
281     // ExprKind::Path
282     assert_eq!(stringify_expr!(thing), "thing");
283     assert_eq!(stringify_expr!(m::thing), "m::thing");
284     assert_eq!(stringify_expr!(self::thing), "self::thing");
285     assert_eq!(stringify_expr!(crate::thing), "crate::thing");
286     assert_eq!(stringify_expr!(Self::thing), "Self::thing");
287     assert_eq!(stringify_expr!(<Self as T>::thing), "<Self as T>::thing");
288     assert_eq!(stringify_expr!(Self::<'static>), "Self::<'static>");
289
290     // ExprKind::AddrOf
291     assert_eq!(stringify_expr!(&expr), "&expr");
292     assert_eq!(stringify_expr!(&mut expr), "&mut expr");
293     assert_eq!(stringify_expr!(&raw const expr), "&raw const expr");
294     assert_eq!(stringify_expr!(&raw mut expr), "&raw mut expr");
295
296     // ExprKind::Break
297     assert_eq!(stringify_expr!(break), "break");
298     assert_eq!(stringify_expr!(break 'a), "break 'a");
299     assert_eq!(stringify_expr!(break true), "break true");
300     assert_eq!(stringify_expr!(break 'a true), "break 'a true");
301
302     // ExprKind::Continue
303     assert_eq!(stringify_expr!(continue), "continue");
304     assert_eq!(stringify_expr!(continue 'a), "continue 'a");
305
306     // ExprKind::Ret
307     assert_eq!(stringify_expr!(return), "return");
308     assert_eq!(stringify_expr!(return true), "return true");
309
310     // ExprKind::MacCall
311     assert_eq!(stringify_expr!(mac!(...)), "mac!(...)");
312     assert_eq!(stringify_expr!(mac![...]), "mac![...]");
313     assert_eq!(stringify_expr!(mac! { ... }), "mac! { ... }");
314
315     // ExprKind::Struct
316     assert_eq!(stringify_expr!(Struct {}), "Struct{}"); // FIXME
317     #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
318     assert_eq!(stringify_expr!(<Struct as Trait>::Type {}), "<Struct as Trait>::Type{}");
319     assert_eq!(stringify_expr!(Struct { .. }), "Struct{..}"); // FIXME
320     assert_eq!(stringify_expr!(Struct { ..base }), "Struct{..base}"); // FIXME
321     assert_eq!(stringify_expr!(Struct { x }), "Struct{x,}");
322     assert_eq!(stringify_expr!(Struct { x, .. }), "Struct{x, ..}");
323     assert_eq!(stringify_expr!(Struct { x, ..base }), "Struct{x, ..base}");
324     assert_eq!(stringify_expr!(Struct { x: true }), "Struct{x: true,}");
325     assert_eq!(stringify_expr!(Struct { x: true, .. }), "Struct{x: true, ..}");
326     assert_eq!(stringify_expr!(Struct { x: true, ..base }), "Struct{x: true, ..base}");
327
328     // ExprKind::Repeat
329     assert_eq!(stringify_expr!([(); 0]), "[(); 0]");
330
331     // ExprKind::Paren
332     assert_eq!(stringify_expr!((expr)), "(expr)");
333
334     // ExprKind::Try
335     assert_eq!(stringify_expr!(expr?), "expr?");
336
337     // ExprKind::Yield
338     assert_eq!(stringify_expr!(yield), "yield");
339     assert_eq!(stringify_expr!(yield true), "yield true");
340 }
341
342 #[test]
343 fn test_item() {
344     // ItemKind::ExternCrate
345     assert_eq!(
346         stringify_item!(
347             extern crate std;
348         ),
349         "extern crate std;",
350     );
351     assert_eq!(
352         stringify_item!(
353             pub extern crate self as std;
354         ),
355         "pub extern crate self as std;",
356     );
357
358     // ItemKind::Use
359     assert_eq!(
360         stringify_item!(
361             pub use crate::{a, b::c};
362         ),
363         "pub use crate::{a, b::c};",
364     );
365
366     // ItemKind::Static
367     assert_eq!(
368         stringify_item!(
369             pub static S: () = {};
370         ),
371         "pub static S: () = {};",
372     );
373     assert_eq!(
374         stringify_item!(
375             static mut S: () = {};
376         ),
377         "static mut S: () = {};",
378     );
379     assert_eq!(
380         stringify_item!(
381             static S: ();
382         ),
383         "static S: () ;", // FIXME
384     );
385     assert_eq!(
386         stringify_item!(
387             static mut S: ();
388         ),
389         "static mut S: () ;",
390     );
391
392     // ItemKind::Const
393     assert_eq!(
394         stringify_item!(
395             pub const S: () = {};
396         ),
397         "pub const S: () = {};",
398     );
399     assert_eq!(
400         stringify_item!(
401             const S: ();
402         ),
403         "const S: () ;", // FIXME
404     );
405
406     // ItemKind::Fn
407     assert_eq!(
408         stringify_item!(
409             pub default const async unsafe extern "C" fn f() {}
410         ),
411         "pub default const async unsafe extern \"C\" fn f() {}",
412     );
413
414     // ItemKind::Mod
415     assert_eq!(
416         stringify_item!(
417             pub mod m;
418         ),
419         "pub mod m;",
420     );
421     assert_eq!(
422         stringify_item!(
423             mod m {}
424         ),
425         "mod m {}",
426     );
427     assert_eq!(
428         stringify_item!(
429             unsafe mod m;
430         ),
431         "unsafe mod m;",
432     );
433     assert_eq!(
434         stringify_item!(
435             unsafe mod m {}
436         ),
437         "unsafe mod m {}",
438     );
439
440     // ItemKind::ForeignMod
441     assert_eq!(
442         stringify_item!(
443             extern "C" {}
444         ),
445         "extern \"C\" {}",
446     );
447     #[rustfmt::skip]
448     assert_eq!(
449         stringify_item!(
450             pub extern "C" {}
451         ),
452         "extern \"C\" {}",
453     );
454     assert_eq!(
455         stringify_item!(
456             unsafe extern "C++" {}
457         ),
458         "unsafe extern \"C++\" {}",
459     );
460
461     // ItemKind::TyAlias
462     #[rustfmt::skip]
463     assert_eq!(
464         stringify_item!(
465             pub default type Type<'a>: Bound
466             where
467                 Self: 'a,
468             = T;
469         ),
470         "pub default type Type<'a>: Bound where Self: 'a = T;",
471     );
472
473     // ItemKind::Enum
474     assert_eq!(
475         stringify_item!(
476             pub enum Void {}
477         ),
478         "pub enum Void {}",
479     );
480     assert_eq!(
481         stringify_item!(
482             enum Empty {
483                 Unit,
484                 Tuple(),
485                 Struct {},
486             }
487         ),
488         "enum Empty { Unit, Tuple(), Struct {}, }",
489     );
490     assert_eq!(
491         stringify_item!(
492             enum Enum<T>
493             where
494                 T: 'a,
495             {
496                 Unit,
497                 Tuple(T),
498                 Struct { t: T },
499             }
500         ),
501         "enum Enum<T> where T: 'a {\n    Unit,\n    Tuple(T),\n    Struct {\n        t: T,\n    },\n}",
502     );
503
504     // ItemKind::Struct
505     assert_eq!(
506         stringify_item!(
507             pub struct Unit;
508         ),
509         "pub struct Unit;",
510     );
511     assert_eq!(
512         stringify_item!(
513             struct Tuple();
514         ),
515         "struct Tuple();",
516     );
517     assert_eq!(
518         stringify_item!(
519             struct Tuple(T);
520         ),
521         "struct Tuple(T);",
522     );
523     assert_eq!(
524         stringify_item!(
525             struct Struct {}
526         ),
527         "struct Struct {}",
528     );
529     assert_eq!(
530         stringify_item!(
531             struct Struct<T>
532             where
533                 T: 'a,
534             {
535                 t: T,
536             }
537         ),
538         "struct Struct<T> where T: 'a {\n    t: T,\n}",
539     );
540
541     // ItemKind::Union
542     assert_eq!(
543         stringify_item!(
544             pub union Union {}
545         ),
546         "pub union Union {}",
547     );
548     assert_eq!(
549         stringify_item!(
550             union Union<T> where T: 'a {
551                 t: T,
552             }
553         ),
554         "union Union<T> where T: 'a {\n    t: T,\n}",
555     );
556
557     // ItemKind::Trait
558     assert_eq!(
559         stringify_item!(
560             pub unsafe auto trait Send {}
561         ),
562         "pub unsafe auto trait Send {}",
563     );
564     assert_eq!(
565         stringify_item!(
566             trait Trait<'a>: Sized
567             where
568                 Self: 'a,
569             {
570             }
571         ),
572         "trait Trait<'a>: Sized where Self: 'a {}",
573     );
574
575     // ItemKind::TraitAlias
576     assert_eq!(
577         stringify_item!(
578             pub trait Trait<T> = Sized where T: 'a;
579         ),
580         "", // FIXME
581     );
582
583     // ItemKind::Impl
584     assert_eq!(
585         stringify_item!(
586             pub impl Struct {}
587         ),
588         "pub impl Struct {}",
589     );
590     assert_eq!(
591         stringify_item!(
592             impl<T> Struct<T> {}
593         ),
594         "impl <T> Struct<T> {}", // FIXME
595     );
596     assert_eq!(
597         stringify_item!(
598             pub impl Trait for Struct {}
599         ),
600         "pub impl Trait for Struct {}",
601     );
602     assert_eq!(
603         stringify_item!(
604             impl ~const Struct {}
605         ),
606         "impl Struct {}", // FIXME
607     );
608
609     // ItemKind::MacCall
610     assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
611     assert_eq!(stringify_item!(mac![...];), "mac![...];");
612     assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");
613
614     // ItemKind::MacroDef
615     assert_eq!(
616         stringify_item!(
617             macro_rules! stringify {
618                 () => {};
619             }
620         ),
621         "macro_rules! stringify { () => {} ; }", // FIXME
622     );
623     assert_eq!(
624         stringify_item!(
625             pub macro stringify() {}
626         ),
627         "pub macro stringify { () => {} }",
628     );
629 }
630
631 #[test]
632 fn test_meta() {
633     assert_eq!(stringify_meta!(k), "k");
634     assert_eq!(stringify_meta!(k = "v"), "k = \"v\"");
635     assert_eq!(stringify_meta!(list(k1, k2 = "v")), "list(k1, k2 = \"v\")");
636     assert_eq!(stringify_meta!(serde::k), "serde::k");
637 }
638
639 #[test]
640 fn test_pat() {
641     // PatKind::Wild
642     assert_eq!(stringify_pat!(_), "_");
643
644     // PatKind::Ident
645     assert_eq!(stringify_pat!(_x), "_x");
646     assert_eq!(stringify_pat!(ref _x), "ref _x");
647     assert_eq!(stringify_pat!(mut _x), "mut _x");
648     assert_eq!(stringify_pat!(ref mut _x), "ref mut _x");
649     assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");
650
651     // PatKind::Struct
652     assert_eq!(stringify_pat!(Struct {}), "Struct {  }"); // FIXME
653     assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {  }");
654     assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {  }");
655     assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
656     assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
657     assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
658     assert_eq!(stringify_pat!(Struct { x, .. }), "Struct { x, .. }");
659     assert_eq!(stringify_pat!(Struct { x: _x, .. }), "Struct { x: _x, .. }");
660     #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
661     assert_eq!(
662         stringify_pat!(<Struct as Trait>::Type {}),
663         "<Struct as Trait>::Type {  }",
664     );
665
666     // PatKind::TupleStruct
667     assert_eq!(stringify_pat!(Tuple()), "Tuple()");
668     assert_eq!(stringify_pat!(Tuple::<u8>()), "Tuple::<u8>()");
669     assert_eq!(stringify_pat!(Tuple::<'static>()), "Tuple::<'static>()");
670     assert_eq!(stringify_pat!(Tuple(x)), "Tuple(x)");
671     assert_eq!(stringify_pat!(Tuple(..)), "Tuple(..)");
672     assert_eq!(stringify_pat!(Tuple(x, ..)), "Tuple(x, ..)");
673     assert_eq!(stringify_pat!(<Struct as Trait>::Type()), "<Struct as Trait>::Type()");
674
675     // PatKind::Or
676     assert_eq!(stringify_pat!(true | false), "true | false");
677     assert_eq!(stringify_pat!(| true), "true");
678     assert_eq!(stringify_pat!(|true| false), "true | false");
679
680     // PatKind::Path
681     assert_eq!(stringify_pat!(crate::Path), "crate::Path");
682     assert_eq!(stringify_pat!(Path::<u8>), "Path::<u8>");
683     assert_eq!(stringify_pat!(Path::<'static>), "Path::<'static>");
684     assert_eq!(stringify_pat!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
685
686     // PatKind::Tuple
687     assert_eq!(stringify_pat!(()), "()");
688     assert_eq!(stringify_pat!((true,)), "(true,)");
689     assert_eq!(stringify_pat!((true, false)), "(true, false)");
690
691     // PatKind::Box
692     assert_eq!(stringify_pat!(box pat), "box pat");
693
694     // PatKind::Ref
695     assert_eq!(stringify_pat!(&pat), "&pat");
696     assert_eq!(stringify_pat!(&mut pat), "&mut pat");
697
698     // PatKind::Lit
699     assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");
700
701     // PatKind::Range
702     assert_eq!(stringify_pat!(..1), "..1");
703     assert_eq!(stringify_pat!(0..), "0 .."); // FIXME
704     assert_eq!(stringify_pat!(0..1), "0 ..1");
705     assert_eq!(stringify_pat!(0..=1), "0 ..=1");
706     assert_eq!(stringify_pat!(-2..=-1), "-2 ..=-1");
707
708     // PatKind::Slice
709     assert_eq!(stringify_pat!([]), "[]");
710     assert_eq!(stringify_pat!([true]), "[true]");
711     assert_eq!(stringify_pat!([true,]), "[true]");
712     assert_eq!(stringify_pat!([true, false]), "[true, false]");
713
714     // PatKind::Rest
715     assert_eq!(stringify_pat!(..), "..");
716
717     // PatKind::Paren
718     assert_eq!(stringify_pat!((pat)), "(pat)");
719
720     // PatKind::MacCall
721     assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
722     assert_eq!(stringify_pat!(mac![...]), "mac![...]");
723     assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
724 }
725
726 #[test]
727 fn test_path() {
728     assert_eq!(stringify_path!(thing), "thing");
729     assert_eq!(stringify_path!(m::thing), "m::thing");
730     assert_eq!(stringify_path!(self::thing), "self::thing");
731     assert_eq!(stringify_path!(crate::thing), "crate::thing");
732     assert_eq!(stringify_path!(Self::thing), "Self::thing");
733     assert_eq!(stringify_path!(Self<'static>), "Self<'static>");
734     assert_eq!(stringify_path!(Self::<'static>), "Self<'static>");
735     assert_eq!(stringify_path!(Self()), "Self()");
736     assert_eq!(stringify_path!(Self() -> ()), "Self() -> ()");
737 }
738
739 #[test]
740 fn test_stmt() {
741     // StmtKind::Local
742     assert_eq!(stringify_stmt!(let _), "let _;");
743     assert_eq!(stringify_stmt!(let x = true), "let x = true;");
744     assert_eq!(stringify_stmt!(let x: bool = true), "let x: bool = true;");
745
746     // StmtKind::Item
747     assert_eq!(
748         stringify_stmt!(
749             struct S;
750         ),
751         "struct S;",
752     );
753
754     // StmtKind::Expr
755     assert_eq!(stringify_stmt!(loop {}), "loop {}");
756
757     // StmtKind::Semi
758     assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");
759
760     // StmtKind::Empty
761     assert_eq!(stringify_stmt!(;), ";");
762
763     // StmtKind::MacCall
764     assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
765     assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
766     assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
767 }
768
769 #[test]
770 fn test_ty() {
771     // TyKind::Slice
772     assert_eq!(stringify_ty!([T]), "[T]");
773
774     // TyKind::Array
775     assert_eq!(stringify_ty!([T; 0]), "[T; 0]");
776
777     // TyKind::Ptr
778     assert_eq!(stringify_ty!(*const T), "*const T");
779     assert_eq!(stringify_ty!(*mut T), "*mut T");
780
781     // TyKind::Rptr
782     assert_eq!(stringify_ty!(&T), "&T");
783     assert_eq!(stringify_ty!(&mut T), "&mut T");
784     assert_eq!(stringify_ty!(&'a T), "&'a T");
785     assert_eq!(stringify_ty!(&'a mut T), "&'a mut T");
786
787     // TyKind::BareFn
788     assert_eq!(stringify_ty!(fn()), "fn()");
789     assert_eq!(stringify_ty!(fn() -> ()), "fn() -> ()");
790     assert_eq!(stringify_ty!(fn(u8)), "fn(u8)");
791     assert_eq!(stringify_ty!(fn(x: u8)), "fn(x: u8)");
792     #[rustfmt::skip]
793     assert_eq!(stringify_ty!(for<> fn()), "fn()");
794     assert_eq!(stringify_ty!(for<'a> fn()), "for<'a>fn()"); // FIXME
795
796     // TyKind::Never
797     assert_eq!(stringify_ty!(!), "!");
798
799     // TyKind::Tup
800     assert_eq!(stringify_ty!(()), "()");
801     assert_eq!(stringify_ty!((T,)), "(T,)");
802     assert_eq!(stringify_ty!((T, U)), "(T, U)");
803
804     // TyKind::Path
805     assert_eq!(stringify_ty!(T), "T");
806     assert_eq!(stringify_ty!(Ref<'a>), "Ref<'a>");
807     assert_eq!(stringify_ty!(PhantomData<T>), "PhantomData<T>");
808     assert_eq!(stringify_ty!(PhantomData::<T>), "PhantomData<T>");
809     assert_eq!(stringify_ty!(Fn() -> !), "Fn() -> !");
810     assert_eq!(stringify_ty!(Fn(u8) -> !), "Fn(u8) -> !");
811     assert_eq!(stringify_ty!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
812
813     // TyKind::TraitObject
814     assert_eq!(stringify_ty!(dyn Send), "dyn Send");
815     assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
816     assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
817     assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
818     assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
819     assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");
820
821     // TyKind::ImplTrait
822     assert_eq!(stringify_ty!(impl Send), "impl Send");
823     assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
824     assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
825     assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
826     assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
827     assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");
828
829     // TyKind::Paren
830     assert_eq!(stringify_ty!((T)), "(T)");
831
832     // TyKind::Infer
833     assert_eq!(stringify_ty!(_), "_");
834
835     // TyKind::MacCall
836     assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
837     assert_eq!(stringify_ty!(mac![...]), "mac![...]");
838     assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
839 }
840
841 #[test]
842 fn test_vis() {
843     // VisibilityKind::Public
844     assert_eq!(stringify_vis!(pub), "pub ");
845
846     // VisibilityKind::Crate
847     assert_eq!(stringify_vis!(crate), "crate ");
848
849     // VisibilityKind::Restricted
850     assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
851     assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
852     assert_eq!(stringify_vis!(pub(in self)), "pub(self) ");
853     assert_eq!(stringify_vis!(pub(in super)), "pub(super) ");
854     assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
855     assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
856     assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
857     assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super::path::to) ");
858
859     // VisibilityKind::Inherited
860     // Directly calling `stringify_vis!()` does not work.
861     macro_rules! stringify_inherited_vis {
862         ($vis:vis struct) => {
863             stringify_vis!($vis)
864         };
865     }
866     assert_eq!(stringify_inherited_vis!(struct), "");
867 }