summaryrefslogtreecommitdiffstats
path: root/tests/ui/macros/stringify.rs
blob: 5cd217df6fc2ff91c814e5c6abb980571141b802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// run-pass
// edition:2021
// compile-flags: --test

#![feature(async_closure)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_trait_impl)]
#![feature(decl_macro)]
#![feature(generators)]
#![feature(more_qualified_paths)]
#![feature(raw_ref_op)]
#![feature(trait_alias)]
#![feature(try_blocks)]
#![feature(type_ascription)]
#![deny(unused_macros)]

macro_rules! stringify_block {
    ($block:block) => {
        stringify!($block)
    };
}

macro_rules! stringify_expr {
    ($expr:expr) => {
        stringify!($expr)
    };
}

macro_rules! stringify_item {
    ($item:item) => {
        stringify!($item)
    };
}

macro_rules! stringify_meta {
    ($meta:meta) => {
        stringify!($meta)
    };
}

macro_rules! stringify_pat {
    ($pat:pat) => {
        stringify!($pat)
    };
}

macro_rules! stringify_path {
    ($path:path) => {
        stringify!($path)
    };
}

macro_rules! stringify_stmt {
    ($stmt:stmt) => {
        stringify!($stmt)
    };
}

macro_rules! stringify_ty {
    ($ty:ty) => {
        stringify!($ty)
    };
}

macro_rules! stringify_vis {
    ($vis:vis) => {
        stringify!($vis)
    };
}

#[test]
fn test_block() {
    assert_eq!(stringify_block!({}), "{}");
    assert_eq!(stringify_block!({ true }), "{ true }");
    assert_eq!(stringify_block!({ return }), "{ return }");
    assert_eq!(
        stringify_block!({
            return;
        }),
        "{ return; }",
    );
    assert_eq!(
        stringify_block!({
            let _;
            true
        }),
        "{ let _; true }",
    );
}

#[test]
fn test_expr() {
    // ExprKind::Box
    assert_eq!(stringify_expr!(box expr), "box expr");

    // ExprKind::Array
    assert_eq!(stringify_expr!([]), "[]");
    assert_eq!(stringify_expr!([true]), "[true]");
    assert_eq!(stringify_expr!([true,]), "[true]");
    assert_eq!(stringify_expr!([true, true]), "[true, true]");

    // ExprKind::Call
    assert_eq!(stringify_expr!(f()), "f()");
    assert_eq!(stringify_expr!(f::<u8>()), "f::<u8>()");
    assert_eq!(stringify_expr!(f::<1>()), "f::<1>()");
    assert_eq!(stringify_expr!(f::<'a, u8, 1>()), "f::<'a, u8, 1>()");
    assert_eq!(stringify_expr!(f(true)), "f(true)");
    assert_eq!(stringify_expr!(f(true,)), "f(true)");
    assert_eq!(stringify_expr!(()()), "()()");

    // ExprKind::MethodCall
    assert_eq!(stringify_expr!(x.f()), "x.f()");
    assert_eq!(stringify_expr!(x.f::<u8>()), "x.f::<u8>()");

    // ExprKind::Tup
    assert_eq!(stringify_expr!(()), "()");
    assert_eq!(stringify_expr!((true,)), "(true,)");
    assert_eq!(stringify_expr!((true, false)), "(true, false)");
    assert_eq!(stringify_expr!((true, false,)), "(true, false)");

    // ExprKind::Binary
    assert_eq!(stringify_expr!(true || false), "true || false");
    assert_eq!(stringify_expr!(true || false && false), "true || false && false");

    // ExprKind::Unary
    assert_eq!(stringify_expr!(*expr), "*expr");
    assert_eq!(stringify_expr!(!expr), "!expr");
    assert_eq!(stringify_expr!(-expr), "-expr");

    // ExprKind::Lit
    assert_eq!(stringify_expr!('x'), "'x'");
    assert_eq!(stringify_expr!(1_000_i8), "1_000_i8");
    assert_eq!(stringify_expr!(1.00000000000000001), "1.00000000000000001");

    // ExprKind::Cast
    assert_eq!(stringify_expr!(expr as T), "expr as T");
    assert_eq!(stringify_expr!(expr as T<u8>), "expr as T<u8>");

    // ExprKind::Type
    assert_eq!(stringify_expr!(expr: T), "expr: T");
    assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");

    // ExprKind::If
    assert_eq!(stringify_expr!(if true {}), "if true {}");
    assert_eq!(
        stringify_expr!(if true {
        } else {
        }),
        "if true {} else {}",
    );
    assert_eq!(
        stringify_expr!(if let true = true {
        } else {
        }),
        "if let true = true {} else {}",
    );
    assert_eq!(
        stringify_expr!(if true {
        } else if false {
        }),
        "if true {} else if false {}",
    );
    assert_eq!(
        stringify_expr!(if true {
        } else if false {
        } else {
        }),
        "if true {} else if false {} else {}",
    );
    assert_eq!(
        stringify_expr!(if true {
            return;
        } else if false {
            0
        } else {
            0
        }),
        "if true { return; } else if false { 0 } else { 0 }",
    );

    // ExprKind::While
    assert_eq!(stringify_expr!(while true {}), "while true {}");
    assert_eq!(stringify_expr!('a: while true {}), "'a: while true {}");
    assert_eq!(stringify_expr!(while let true = true {}), "while let true = true {}");

    // ExprKind::ForLoop
    assert_eq!(stringify_expr!(for _ in x {}), "for _ in x {}");
    assert_eq!(stringify_expr!('a: for _ in x {}), "'a: for _ in x {}");

    // ExprKind::Loop
    assert_eq!(stringify_expr!(loop {}), "loop {}");
    assert_eq!(stringify_expr!('a: loop {}), "'a: loop {}");

    // ExprKind::Match
    assert_eq!(stringify_expr!(match self {}), "match self {}");
    assert_eq!(
        stringify_expr!(match self {
            Ok => 1,
        }),
        "match self { Ok => 1, }",
    );
    assert_eq!(
        stringify_expr!(match self {
            Ok => 1,
            Err => 0,
        }),
        "match self { Ok => 1, Err => 0, }",
    );

    // ExprKind::Closure
    assert_eq!(stringify_expr!(|| {}), "|| {}");
    assert_eq!(stringify_expr!(|x| {}), "|x| {}");
    assert_eq!(stringify_expr!(|x: u8| {}), "|x: u8| {}");
    assert_eq!(stringify_expr!(|| ()), "|| ()");
    assert_eq!(stringify_expr!(move || self), "move || self");
    assert_eq!(stringify_expr!(async || self), "async || self");
    assert_eq!(stringify_expr!(async move || self), "async move || self");
    assert_eq!(stringify_expr!(static || self), "static || self");
    assert_eq!(stringify_expr!(static move || self), "static move || self");
    #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
    assert_eq!(
        stringify_expr!(static async || self),
        "static async || self",
    );
    #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
    assert_eq!(
        stringify_expr!(static async move || self),
        "static async move || self",
    );
    assert_eq!(stringify_expr!(|| -> u8 { self }), "|| -> u8 { self }");
    assert_eq!(stringify_expr!(1 + || {}), "1 + (|| {})"); // ??

    // ExprKind::Block
    assert_eq!(stringify_expr!({}), "{}");
    assert_eq!(stringify_expr!(unsafe {}), "unsafe {}");
    assert_eq!(stringify_expr!('a: {}), "'a: {}");
    assert_eq!(
        stringify_expr!(
            #[attr]
            {}
        ),
        "#[attr] {}",
    );
    assert_eq!(
        stringify_expr!(
            {
                #![attr]
            }
        ),
        "{\n\
        \x20   #![attr]\n\
        }",
    );

    // ExprKind::Async
    assert_eq!(stringify_expr!(async {}), "async {}");
    assert_eq!(stringify_expr!(async move {}), "async move {}");

    // ExprKind::Await
    assert_eq!(stringify_expr!(expr.await), "expr.await");

    // ExprKind::TryBlock
    assert_eq!(stringify_expr!(try {}), "try {}");

    // ExprKind::Assign
    assert_eq!(stringify_expr!(expr = true), "expr = true");

    // ExprKind::AssignOp
    assert_eq!(stringify_expr!(expr += true), "expr += true");

    // ExprKind::Field
    assert_eq!(stringify_expr!(expr.field), "expr.field");
    assert_eq!(stringify_expr!(expr.0), "expr.0");

    // ExprKind::Index
    assert_eq!(stringify_expr!(expr[true]), "expr[true]");

    // ExprKind::Range
    assert_eq!(stringify_expr!(..), "..");
    assert_eq!(stringify_expr!(..hi), "..hi");
    assert_eq!(stringify_expr!(lo..), "lo..");
    assert_eq!(stringify_expr!(lo..hi), "lo..hi");
    assert_eq!(stringify_expr!(..=hi), "..=hi");
    assert_eq!(stringify_expr!(lo..=hi), "lo..=hi");
    assert_eq!(stringify_expr!(-2..=-1), "-2..=-1");

    // ExprKind::Path
    assert_eq!(stringify_expr!(thing), "thing");
    assert_eq!(stringify_expr!(m::thing), "m::thing");
    assert_eq!(stringify_expr!(self::thing), "self::thing");
    assert_eq!(stringify_expr!(crate::thing), "crate::thing");
    assert_eq!(stringify_expr!(Self::thing), "Self::thing");
    assert_eq!(stringify_expr!(<Self as T>::thing), "<Self as T>::thing");
    assert_eq!(stringify_expr!(Self::<'static>), "Self::<'static>");

    // ExprKind::AddrOf
    assert_eq!(stringify_expr!(&expr), "&expr");
    assert_eq!(stringify_expr!(&mut expr), "&mut expr");
    assert_eq!(stringify_expr!(&raw const expr), "&raw const expr");
    assert_eq!(stringify_expr!(&raw mut expr), "&raw mut expr");

    // ExprKind::Break
    assert_eq!(stringify_expr!(break), "break");
    assert_eq!(stringify_expr!(break 'a), "break 'a");
    assert_eq!(stringify_expr!(break true), "break true");
    assert_eq!(stringify_expr!(break 'a true), "break 'a true");

    // ExprKind::Continue
    assert_eq!(stringify_expr!(continue), "continue");
    assert_eq!(stringify_expr!(continue 'a), "continue 'a");

    // ExprKind::Ret
    assert_eq!(stringify_expr!(return), "return");
    assert_eq!(stringify_expr!(return true), "return true");

    // ExprKind::MacCall
    assert_eq!(stringify_expr!(mac!(...)), "mac!(...)");
    assert_eq!(stringify_expr!(mac![...]), "mac![...]");
    assert_eq!(stringify_expr!(mac! { ... }), "mac! { ... }");

    // ExprKind::Struct
    assert_eq!(stringify_expr!(Struct {}), "Struct {}");
    #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
    assert_eq!(stringify_expr!(<Struct as Trait>::Type {}), "<Struct as Trait>::Type {}");
    assert_eq!(stringify_expr!(Struct { .. }), "Struct { .. }");
    assert_eq!(stringify_expr!(Struct { ..base }), "Struct { ..base }");
    assert_eq!(stringify_expr!(Struct { x }), "Struct { x }");
    assert_eq!(stringify_expr!(Struct { x, .. }), "Struct { x, .. }");
    assert_eq!(stringify_expr!(Struct { x, ..base }), "Struct { x, ..base }");
    assert_eq!(stringify_expr!(Struct { x: true }), "Struct { x: true }");
    assert_eq!(stringify_expr!(Struct { x: true, .. }), "Struct { x: true, .. }");
    assert_eq!(stringify_expr!(Struct { x: true, ..base }), "Struct { x: true, ..base }");

    // ExprKind::Repeat
    assert_eq!(stringify_expr!([(); 0]), "[(); 0]");

    // ExprKind::Paren
    assert_eq!(stringify_expr!((expr)), "(expr)");

    // ExprKind::Try
    assert_eq!(stringify_expr!(expr?), "expr?");

    // ExprKind::Yield
    assert_eq!(stringify_expr!(yield), "yield");
    assert_eq!(stringify_expr!(yield true), "yield true");
}

#[test]
fn test_item() {
    // ItemKind::ExternCrate
    assert_eq!(
        stringify_item!(
            extern crate std;
        ),
        "extern crate std;",
    );
    assert_eq!(
        stringify_item!(
            pub extern crate self as std;
        ),
        "pub extern crate self as std;",
    );

    // ItemKind::Use
    assert_eq!(
        stringify_item!(
            pub use crate::{a, b::c};
        ),
        "pub use crate::{a, b::c};",
    );

    // ItemKind::Static
    assert_eq!(
        stringify_item!(
            pub static S: () = {};
        ),
        "pub static S: () = {};",
    );
    assert_eq!(
        stringify_item!(
            static mut S: () = {};
        ),
        "static mut S: () = {};",
    );
    assert_eq!(
        stringify_item!(
            static S: ();
        ),
        "static S: ();",
    );
    assert_eq!(
        stringify_item!(
            static mut S: ();
        ),
        "static mut S: ();",
    );

    // ItemKind::Const
    assert_eq!(
        stringify_item!(
            pub const S: () = {};
        ),
        "pub const S: () = {};",
    );
    assert_eq!(
        stringify_item!(
            const S: ();
        ),
        "const S: ();",
    );

    // ItemKind::Fn
    assert_eq!(
        stringify_item!(
            pub default const async unsafe extern "C" fn f() {}
        ),
        "pub default const async unsafe extern \"C\" fn f() {}",
    );

    // ItemKind::Mod
    assert_eq!(
        stringify_item!(
            pub mod m;
        ),
        "pub mod m;",
    );
    assert_eq!(
        stringify_item!(
            mod m {}
        ),
        "mod m {}",
    );
    assert_eq!(
        stringify_item!(
            unsafe mod m;
        ),
        "unsafe mod m;",
    );
    assert_eq!(
        stringify_item!(
            unsafe mod m {}
        ),
        "unsafe mod m {}",
    );

    // ItemKind::ForeignMod
    assert_eq!(
        stringify_item!(
            extern "C" {}
        ),
        "extern \"C\" {}",
    );
    #[rustfmt::skip]
    assert_eq!(
        stringify_item!(
            pub extern "C" {}
        ),
        "extern \"C\" {}",
    );
    assert_eq!(
        stringify_item!(
            unsafe extern "C++" {}
        ),
        "unsafe extern \"C++\" {}",
    );

    // ItemKind::TyAlias
    #[rustfmt::skip]
    assert_eq!(
        stringify_item!(
            pub default type Type<'a>: Bound
            where
                Self: 'a,
            = T;
        ),
        "pub default type Type<'a>: Bound where Self: 'a = T;",
    );

    // ItemKind::Enum
    assert_eq!(
        stringify_item!(
            pub enum Void {}
        ),
        "pub enum Void {}",
    );
    assert_eq!(
        stringify_item!(
            enum Empty {
                Unit,
                Tuple(),
                Struct {},
            }
        ),
        "enum Empty { Unit, Tuple(), Struct {}, }",
    );
    assert_eq!(
        stringify_item!(
            enum Enum<T>
            where
                T: 'a,
            {
                Unit,
                Tuple(T),
                Struct { t: T },
            }
        ),
        "enum Enum<T> where T: 'a {\n\
        \x20   Unit,\n\
        \x20   Tuple(T),\n\
        \x20   Struct {\n\
        \x20       t: T,\n\
        \x20   },\n\
        }",
    );

    // ItemKind::Struct
    assert_eq!(
        stringify_item!(
            pub struct Unit;
        ),
        "pub struct Unit;",
    );
    assert_eq!(
        stringify_item!(
            struct Tuple();
        ),
        "struct Tuple();",
    );
    assert_eq!(
        stringify_item!(
            struct Tuple(T);
        ),
        "struct Tuple(T);",
    );
    assert_eq!(
        stringify_item!(
            struct Struct {}
        ),
        "struct Struct {}",
    );
    assert_eq!(
        stringify_item!(
            struct Struct<T>
            where
                T: 'a,
            {
                t: T,
            }
        ),
        "struct Struct<T> where T: 'a {\n\
        \x20   t: T,\n\
        }",
    );

    // ItemKind::Union
    assert_eq!(
        stringify_item!(
            pub union Union {}
        ),
        "pub union Union {}",
    );
    assert_eq!(
        stringify_item!(
            union Union<T> where T: 'a {
                t: T,
            }
        ),
        "union Union<T> where T: 'a {\n\
        \x20   t: T,\n\
        }",
    );

    // ItemKind::Trait
    assert_eq!(
        stringify_item!(
            pub unsafe auto trait Send {}
        ),
        "pub unsafe auto trait Send {}",
    );
    assert_eq!(
        stringify_item!(
            trait Trait<'a>: Sized
            where
                Self: 'a,
            {
            }
        ),
        "trait Trait<'a>: Sized where Self: 'a {}",
    );

    // ItemKind::TraitAlias
    assert_eq!(
        stringify_item!(
            pub trait Trait<T> = Sized where T: 'a;
        ),
        "pub trait Trait<T> = Sized where T: 'a;",
    );

    // ItemKind::Impl
    assert_eq!(
        stringify_item!(
            pub impl Struct {}
        ),
        "pub impl Struct {}",
    );
    assert_eq!(
        stringify_item!(
            impl<T> Struct<T> {}
        ),
        "impl<T> Struct<T> {}",
    );
    assert_eq!(
        stringify_item!(
            pub impl Trait for Struct {}
        ),
        "pub impl Trait for Struct {}",
    );
    assert_eq!(
        stringify_item!(
            impl<T> const Trait for T {}
        ),
        "impl<T> const Trait for T {}",
    );
    assert_eq!(
        stringify_item!(
            impl ~const Struct {}
        ),
        "impl Struct {}", // FIXME
    );

    // ItemKind::MacCall
    assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
    assert_eq!(stringify_item!(mac![...];), "mac![...];");
    assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");

    // ItemKind::MacroDef
    assert_eq!(
        stringify_item!(
            macro_rules! stringify {
                () => {};
            }
        ),
        "macro_rules! stringify { () => {} ; }", // FIXME
    );
    assert_eq!(
        stringify_item!(
            pub macro stringify() {}
        ),
        "pub macro stringify { () => {} }",
    );
}

#[test]
fn test_meta() {
    assert_eq!(stringify_meta!(k), "k");
    assert_eq!(stringify_meta!(k = "v"), "k = \"v\"");
    assert_eq!(stringify_meta!(list(k1, k2 = "v")), "list(k1, k2 = \"v\")");
    assert_eq!(stringify_meta!(serde::k), "serde::k");
}

#[test]
fn test_pat() {
    // PatKind::Wild
    assert_eq!(stringify_pat!(_), "_");

    // PatKind::Ident
    assert_eq!(stringify_pat!(_x), "_x");
    assert_eq!(stringify_pat!(ref _x), "ref _x");
    assert_eq!(stringify_pat!(mut _x), "mut _x");
    assert_eq!(stringify_pat!(ref mut _x), "ref mut _x");
    assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");

    // PatKind::Struct
    assert_eq!(stringify_pat!(Struct {}), "Struct {}");
    assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {}");
    assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}");
    assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
    assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
    assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
    assert_eq!(stringify_pat!(Struct { x, .. }), "Struct { x, .. }");
    assert_eq!(stringify_pat!(Struct { x: _x, .. }), "Struct { x: _x, .. }");
    #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
    assert_eq!(
        stringify_pat!(<Struct as Trait>::Type {}),
        "<Struct as Trait>::Type {}",
    );

    // PatKind::TupleStruct
    assert_eq!(stringify_pat!(Tuple()), "Tuple()");
    assert_eq!(stringify_pat!(Tuple::<u8>()), "Tuple::<u8>()");
    assert_eq!(stringify_pat!(Tuple::<'static>()), "Tuple::<'static>()");
    assert_eq!(stringify_pat!(Tuple(x)), "Tuple(x)");
    assert_eq!(stringify_pat!(Tuple(..)), "Tuple(..)");
    assert_eq!(stringify_pat!(Tuple(x, ..)), "Tuple(x, ..)");
    assert_eq!(stringify_pat!(<Struct as Trait>::Type()), "<Struct as Trait>::Type()");

    // PatKind::Or
    assert_eq!(stringify_pat!(true | false), "true | false");
    assert_eq!(stringify_pat!(| true), "true");
    assert_eq!(stringify_pat!(|true| false), "true | false");

    // PatKind::Path
    assert_eq!(stringify_pat!(crate::Path), "crate::Path");
    assert_eq!(stringify_pat!(Path::<u8>), "Path::<u8>");
    assert_eq!(stringify_pat!(Path::<'static>), "Path::<'static>");
    assert_eq!(stringify_pat!(<Struct as Trait>::Type), "<Struct as Trait>::Type");

    // PatKind::Tuple
    assert_eq!(stringify_pat!(()), "()");
    assert_eq!(stringify_pat!((true,)), "(true,)");
    assert_eq!(stringify_pat!((true, false)), "(true, false)");

    // PatKind::Box
    assert_eq!(stringify_pat!(box pat), "box pat");

    // PatKind::Ref
    assert_eq!(stringify_pat!(&pat), "&pat");
    assert_eq!(stringify_pat!(&mut pat), "&mut pat");

    // PatKind::Lit
    assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");

    // PatKind::Range
    assert_eq!(stringify_pat!(..1), "..1");
    assert_eq!(stringify_pat!(0..), "0..");
    assert_eq!(stringify_pat!(0..1), "0..1");
    assert_eq!(stringify_pat!(0..=1), "0..=1");
    assert_eq!(stringify_pat!(-2..=-1), "-2..=-1");

    // PatKind::Slice
    assert_eq!(stringify_pat!([]), "[]");
    assert_eq!(stringify_pat!([true]), "[true]");
    assert_eq!(stringify_pat!([true,]), "[true]");
    assert_eq!(stringify_pat!([true, false]), "[true, false]");

    // PatKind::Rest
    assert_eq!(stringify_pat!(..), "..");

    // PatKind::Paren
    assert_eq!(stringify_pat!((pat)), "(pat)");

    // PatKind::MacCall
    assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
    assert_eq!(stringify_pat!(mac![...]), "mac![...]");
    assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
}

#[test]
fn test_path() {
    assert_eq!(stringify_path!(thing), "thing");
    assert_eq!(stringify_path!(m::thing), "m::thing");
    assert_eq!(stringify_path!(self::thing), "self::thing");
    assert_eq!(stringify_path!(crate::thing), "crate::thing");
    assert_eq!(stringify_path!(Self::thing), "Self::thing");
    assert_eq!(stringify_path!(Self<'static>), "Self<'static>");
    assert_eq!(stringify_path!(Self::<'static>), "Self<'static>");
    assert_eq!(stringify_path!(Self()), "Self()");
    assert_eq!(stringify_path!(Self() -> ()), "Self() -> ()");
}

#[test]
fn test_stmt() {
    // StmtKind::Local
    assert_eq!(stringify_stmt!(let _), "let _;");
    assert_eq!(stringify_stmt!(let x = true), "let x = true;");
    assert_eq!(stringify_stmt!(let x: bool = true), "let x: bool = true;");

    // StmtKind::Item
    assert_eq!(
        stringify_stmt!(
            struct S;
        ),
        "struct S;",
    );

    // StmtKind::Expr
    assert_eq!(stringify_stmt!(loop {}), "loop {}");

    // StmtKind::Semi
    assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");

    // StmtKind::Empty
    assert_eq!(stringify_stmt!(;), ";");

    // StmtKind::MacCall
    assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
    assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
    assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
}

#[test]
fn test_ty() {
    // TyKind::Slice
    assert_eq!(stringify_ty!([T]), "[T]");

    // TyKind::Array
    assert_eq!(stringify_ty!([T; 0]), "[T; 0]");

    // TyKind::Ptr
    assert_eq!(stringify_ty!(*const T), "*const T");
    assert_eq!(stringify_ty!(*mut T), "*mut T");

    // TyKind::Ref
    assert_eq!(stringify_ty!(&T), "&T");
    assert_eq!(stringify_ty!(&mut T), "&mut T");
    assert_eq!(stringify_ty!(&'a T), "&'a T");
    assert_eq!(stringify_ty!(&'a mut T), "&'a mut T");

    // TyKind::BareFn
    assert_eq!(stringify_ty!(fn()), "fn()");
    assert_eq!(stringify_ty!(fn() -> ()), "fn() -> ()");
    assert_eq!(stringify_ty!(fn(u8)), "fn(u8)");
    assert_eq!(stringify_ty!(fn(x: u8)), "fn(x: u8)");
    #[rustfmt::skip]
    assert_eq!(stringify_ty!(for<> fn()), "fn()");
    assert_eq!(stringify_ty!(for<'a> fn()), "for<'a> fn()");

    // TyKind::Never
    assert_eq!(stringify_ty!(!), "!");

    // TyKind::Tup
    assert_eq!(stringify_ty!(()), "()");
    assert_eq!(stringify_ty!((T,)), "(T,)");
    assert_eq!(stringify_ty!((T, U)), "(T, U)");

    // TyKind::Path
    assert_eq!(stringify_ty!(T), "T");
    assert_eq!(stringify_ty!(Ref<'a>), "Ref<'a>");
    assert_eq!(stringify_ty!(PhantomData<T>), "PhantomData<T>");
    assert_eq!(stringify_ty!(PhantomData::<T>), "PhantomData<T>");
    assert_eq!(stringify_ty!(Fn() -> !), "Fn() -> !");
    assert_eq!(stringify_ty!(Fn(u8) -> !), "Fn(u8) -> !");
    assert_eq!(stringify_ty!(<Struct as Trait>::Type), "<Struct as Trait>::Type");

    // TyKind::TraitObject
    assert_eq!(stringify_ty!(dyn Send), "dyn Send");
    assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
    assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
    assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
    assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
    assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");

    // TyKind::ImplTrait
    assert_eq!(stringify_ty!(impl Send), "impl Send");
    assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
    assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
    assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
    assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
    assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");

    // TyKind::Paren
    assert_eq!(stringify_ty!((T)), "(T)");

    // TyKind::Infer
    assert_eq!(stringify_ty!(_), "_");

    // TyKind::MacCall
    assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
    assert_eq!(stringify_ty!(mac![...]), "mac![...]");
    assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
}

#[test]
fn test_vis() {
    // VisibilityKind::Public
    assert_eq!(stringify_vis!(pub), "pub ");

    // VisibilityKind::Restricted
    assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
    assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
    assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
    assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
    assert_eq!(stringify_vis!(pub(in self)), "pub(in self) ");
    assert_eq!(stringify_vis!(pub(in super)), "pub(in super) ");
    assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
    assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
    assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
    assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super::path::to) ");

    // VisibilityKind::Inherited
    // Directly calling `stringify_vis!()` does not work.
    macro_rules! stringify_inherited_vis {
        ($vis:vis struct) => {
            stringify_vis!($vis)
        };
    }
    assert_eq!(stringify_inherited_vis!(struct), "");
}