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