]> git.proxmox.com Git - rustc.git/blob - src/test/ui/macros/stringify.rs
New upstream version 1.63.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(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 {}");
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 { .. }");
322 assert_eq!(stringify_expr!(Struct { ..base }), "Struct { ..base }");
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 "pub trait Trait<T> = Sized where T: 'a;",
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> {}",
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<T> const Trait for T {}
617 ),
618 "impl<T> const Trait for T {}",
619 );
620 assert_eq!(
621 stringify_item!(
622 impl ~const Struct {}
623 ),
624 "impl Struct {}", // FIXME
625 );
626
627 // ItemKind::MacCall
628 assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
629 assert_eq!(stringify_item!(mac![...];), "mac![...];");
630 assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");
631
632 // ItemKind::MacroDef
633 assert_eq!(
634 stringify_item!(
635 macro_rules! stringify {
636 () => {};
637 }
638 ),
639 "macro_rules! stringify { () => {} ; }", // FIXME
640 );
641 assert_eq!(
642 stringify_item!(
643 pub macro stringify() {}
644 ),
645 "pub macro stringify { () => {} }",
646 );
647 }
648
649 #[test]
650 fn test_meta() {
651 assert_eq!(stringify_meta!(k), "k");
652 assert_eq!(stringify_meta!(k = "v"), "k = \"v\"");
653 assert_eq!(stringify_meta!(list(k1, k2 = "v")), "list(k1, k2 = \"v\")");
654 assert_eq!(stringify_meta!(serde::k), "serde::k");
655 }
656
657 #[test]
658 fn test_pat() {
659 // PatKind::Wild
660 assert_eq!(stringify_pat!(_), "_");
661
662 // PatKind::Ident
663 assert_eq!(stringify_pat!(_x), "_x");
664 assert_eq!(stringify_pat!(ref _x), "ref _x");
665 assert_eq!(stringify_pat!(mut _x), "mut _x");
666 assert_eq!(stringify_pat!(ref mut _x), "ref mut _x");
667 assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");
668
669 // PatKind::Struct
670 assert_eq!(stringify_pat!(Struct {}), "Struct {}");
671 assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {}");
672 assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}");
673 assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
674 assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
675 assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
676 assert_eq!(stringify_pat!(Struct { x, .. }), "Struct { x, .. }");
677 assert_eq!(stringify_pat!(Struct { x: _x, .. }), "Struct { x: _x, .. }");
678 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
679 assert_eq!(
680 stringify_pat!(<Struct as Trait>::Type {}),
681 "<Struct as Trait>::Type {}",
682 );
683
684 // PatKind::TupleStruct
685 assert_eq!(stringify_pat!(Tuple()), "Tuple()");
686 assert_eq!(stringify_pat!(Tuple::<u8>()), "Tuple::<u8>()");
687 assert_eq!(stringify_pat!(Tuple::<'static>()), "Tuple::<'static>()");
688 assert_eq!(stringify_pat!(Tuple(x)), "Tuple(x)");
689 assert_eq!(stringify_pat!(Tuple(..)), "Tuple(..)");
690 assert_eq!(stringify_pat!(Tuple(x, ..)), "Tuple(x, ..)");
691 assert_eq!(stringify_pat!(<Struct as Trait>::Type()), "<Struct as Trait>::Type()");
692
693 // PatKind::Or
694 assert_eq!(stringify_pat!(true | false), "true | false");
695 assert_eq!(stringify_pat!(| true), "true");
696 assert_eq!(stringify_pat!(|true| false), "true | false");
697
698 // PatKind::Path
699 assert_eq!(stringify_pat!(crate::Path), "crate::Path");
700 assert_eq!(stringify_pat!(Path::<u8>), "Path::<u8>");
701 assert_eq!(stringify_pat!(Path::<'static>), "Path::<'static>");
702 assert_eq!(stringify_pat!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
703
704 // PatKind::Tuple
705 assert_eq!(stringify_pat!(()), "()");
706 assert_eq!(stringify_pat!((true,)), "(true,)");
707 assert_eq!(stringify_pat!((true, false)), "(true, false)");
708
709 // PatKind::Box
710 assert_eq!(stringify_pat!(box pat), "box pat");
711
712 // PatKind::Ref
713 assert_eq!(stringify_pat!(&pat), "&pat");
714 assert_eq!(stringify_pat!(&mut pat), "&mut pat");
715
716 // PatKind::Lit
717 assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");
718
719 // PatKind::Range
720 assert_eq!(stringify_pat!(..1), "..1");
721 assert_eq!(stringify_pat!(0..), "0..");
722 assert_eq!(stringify_pat!(0..1), "0..1");
723 assert_eq!(stringify_pat!(0..=1), "0..=1");
724 assert_eq!(stringify_pat!(-2..=-1), "-2..=-1");
725
726 // PatKind::Slice
727 assert_eq!(stringify_pat!([]), "[]");
728 assert_eq!(stringify_pat!([true]), "[true]");
729 assert_eq!(stringify_pat!([true,]), "[true]");
730 assert_eq!(stringify_pat!([true, false]), "[true, false]");
731
732 // PatKind::Rest
733 assert_eq!(stringify_pat!(..), "..");
734
735 // PatKind::Paren
736 assert_eq!(stringify_pat!((pat)), "(pat)");
737
738 // PatKind::MacCall
739 assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
740 assert_eq!(stringify_pat!(mac![...]), "mac![...]");
741 assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
742 }
743
744 #[test]
745 fn test_path() {
746 assert_eq!(stringify_path!(thing), "thing");
747 assert_eq!(stringify_path!(m::thing), "m::thing");
748 assert_eq!(stringify_path!(self::thing), "self::thing");
749 assert_eq!(stringify_path!(crate::thing), "crate::thing");
750 assert_eq!(stringify_path!(Self::thing), "Self::thing");
751 assert_eq!(stringify_path!(Self<'static>), "Self<'static>");
752 assert_eq!(stringify_path!(Self::<'static>), "Self<'static>");
753 assert_eq!(stringify_path!(Self()), "Self()");
754 assert_eq!(stringify_path!(Self() -> ()), "Self() -> ()");
755 }
756
757 #[test]
758 fn test_stmt() {
759 // StmtKind::Local
760 assert_eq!(stringify_stmt!(let _), "let _;");
761 assert_eq!(stringify_stmt!(let x = true), "let x = true;");
762 assert_eq!(stringify_stmt!(let x: bool = true), "let x: bool = true;");
763
764 // StmtKind::Item
765 assert_eq!(
766 stringify_stmt!(
767 struct S;
768 ),
769 "struct S;",
770 );
771
772 // StmtKind::Expr
773 assert_eq!(stringify_stmt!(loop {}), "loop {}");
774
775 // StmtKind::Semi
776 assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");
777
778 // StmtKind::Empty
779 assert_eq!(stringify_stmt!(;), ";");
780
781 // StmtKind::MacCall
782 assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
783 assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
784 assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
785 }
786
787 #[test]
788 fn test_ty() {
789 // TyKind::Slice
790 assert_eq!(stringify_ty!([T]), "[T]");
791
792 // TyKind::Array
793 assert_eq!(stringify_ty!([T; 0]), "[T; 0]");
794
795 // TyKind::Ptr
796 assert_eq!(stringify_ty!(*const T), "*const T");
797 assert_eq!(stringify_ty!(*mut T), "*mut T");
798
799 // TyKind::Rptr
800 assert_eq!(stringify_ty!(&T), "&T");
801 assert_eq!(stringify_ty!(&mut T), "&mut T");
802 assert_eq!(stringify_ty!(&'a T), "&'a T");
803 assert_eq!(stringify_ty!(&'a mut T), "&'a mut T");
804
805 // TyKind::BareFn
806 assert_eq!(stringify_ty!(fn()), "fn()");
807 assert_eq!(stringify_ty!(fn() -> ()), "fn() -> ()");
808 assert_eq!(stringify_ty!(fn(u8)), "fn(u8)");
809 assert_eq!(stringify_ty!(fn(x: u8)), "fn(x: u8)");
810 #[rustfmt::skip]
811 assert_eq!(stringify_ty!(for<> fn()), "fn()");
812 assert_eq!(stringify_ty!(for<'a> fn()), "for<'a> fn()");
813
814 // TyKind::Never
815 assert_eq!(stringify_ty!(!), "!");
816
817 // TyKind::Tup
818 assert_eq!(stringify_ty!(()), "()");
819 assert_eq!(stringify_ty!((T,)), "(T,)");
820 assert_eq!(stringify_ty!((T, U)), "(T, U)");
821
822 // TyKind::Path
823 assert_eq!(stringify_ty!(T), "T");
824 assert_eq!(stringify_ty!(Ref<'a>), "Ref<'a>");
825 assert_eq!(stringify_ty!(PhantomData<T>), "PhantomData<T>");
826 assert_eq!(stringify_ty!(PhantomData::<T>), "PhantomData<T>");
827 assert_eq!(stringify_ty!(Fn() -> !), "Fn() -> !");
828 assert_eq!(stringify_ty!(Fn(u8) -> !), "Fn(u8) -> !");
829 assert_eq!(stringify_ty!(<Struct as Trait>::Type), "<Struct as Trait>::Type");
830
831 // TyKind::TraitObject
832 assert_eq!(stringify_ty!(dyn Send), "dyn Send");
833 assert_eq!(stringify_ty!(dyn Send + 'a), "dyn Send + 'a");
834 assert_eq!(stringify_ty!(dyn 'a + Send), "dyn 'a + Send");
835 assert_eq!(stringify_ty!(dyn ?Sized), "dyn ?Sized");
836 assert_eq!(stringify_ty!(dyn ~const Clone), "dyn Clone"); // FIXME
837 assert_eq!(stringify_ty!(dyn for<'a> Send), "dyn for<'a> Send");
838
839 // TyKind::ImplTrait
840 assert_eq!(stringify_ty!(impl Send), "impl Send");
841 assert_eq!(stringify_ty!(impl Send + 'a), "impl Send + 'a");
842 assert_eq!(stringify_ty!(impl 'a + Send), "impl 'a + Send");
843 assert_eq!(stringify_ty!(impl ?Sized), "impl ?Sized");
844 assert_eq!(stringify_ty!(impl ~const Clone), "impl Clone"); // FIXME
845 assert_eq!(stringify_ty!(impl for<'a> Send), "impl for<'a> Send");
846
847 // TyKind::Paren
848 assert_eq!(stringify_ty!((T)), "(T)");
849
850 // TyKind::Infer
851 assert_eq!(stringify_ty!(_), "_");
852
853 // TyKind::MacCall
854 assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
855 assert_eq!(stringify_ty!(mac![...]), "mac![...]");
856 assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
857 }
858
859 #[test]
860 fn test_vis() {
861 // VisibilityKind::Public
862 assert_eq!(stringify_vis!(pub), "pub ");
863
864 // VisibilityKind::Restricted
865 assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
866 assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
867 assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
868 assert_eq!(stringify_vis!(pub(in self)), "pub(self) ");
869 assert_eq!(stringify_vis!(pub(in super)), "pub(super) ");
870 assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
871 assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
872 assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
873 assert_eq!(stringify_vis!(pub(in super::path::to)), "pub(in super::path::to) ");
874
875 // VisibilityKind::Inherited
876 // Directly calling `stringify_vis!()` does not work.
877 macro_rules! stringify_inherited_vis {
878 ($vis:vis struct) => {
879 stringify_vis!($vis)
880 };
881 }
882 assert_eq!(stringify_inherited_vis!(struct), "");
883 }