3 // compile-flags: --test
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)]
13 macro_rules! stringify_block {
19 macro_rules! stringify_expr {
25 macro_rules! stringify_item {
31 macro_rules! stringify_meta {
37 macro_rules! stringify_pat {
43 macro_rules! stringify_path {
49 macro_rules! stringify_stmt {
55 macro_rules! stringify_ty {
61 macro_rules! stringify_vis {
69 assert_eq!(stringify_block!({}), "{}");
70 assert_eq!(stringify_block!({ true }), "{ true }");
71 assert_eq!(stringify_block!({ return }), "{ return }");
90 assert_eq!(stringify_expr!(box expr), "box expr");
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]");
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!(()()), "()()");
107 // ExprKind::MethodCall
108 assert_eq!(stringify_expr!(x.f()), "x.f()");
109 assert_eq!(stringify_expr!(x.f::<u8>()), "x.f::<u8>()");
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)");
118 assert_eq!(stringify_expr!(true || false), "true || false");
119 assert_eq!(stringify_expr!(true || false && false), "true || false && false");
122 assert_eq!(stringify_expr!(*expr), "*expr");
123 assert_eq!(stringify_expr!(!expr), "!expr");
124 assert_eq!(stringify_expr!(-expr), "-expr");
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");
132 assert_eq!(stringify_expr!(expr as T), "expr as T");
133 assert_eq!(stringify_expr!(expr as T<u8>), "expr as T<u8>");
136 assert_eq!(stringify_expr!(expr: T), "expr: T");
137 assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
140 assert_eq!(stringify_expr!(if true {}), "if true {}");
142 stringify_expr!(if true {
145 "if true {} else {}",
148 stringify_expr!(if let true = true {
151 "if let true = true {} else {}",
154 stringify_expr!(if true {
157 "if true {} else if false {}",
160 stringify_expr!(if true {
164 "if true {} else if false {} else {}",
167 stringify_expr!(if true {
174 "if true { return; } else if false { 0 } else { 0 }",
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 {}");
183 assert_eq!(stringify_expr!(for _ in x {}), "for _ in x {}");
184 assert_eq!(stringify_expr!('a: for _ in x {}), "'a: for _ in x {}");
187 assert_eq!(stringify_expr!(loop {}), "loop {}");
188 assert_eq!(stringify_expr!('a: loop {}), "'a: loop {}");
191 assert_eq!(stringify_expr!(match self {}), "match self {}");
193 stringify_expr!(match self {
196 "match self { Ok => 1, }",
199 stringify_expr!(match self {
203 "match self { Ok => 1, Err => 0, }",
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
218 stringify_expr!(static async || self),
219 "static async || self",
221 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
223 stringify_expr!(static async move || self),
224 "static async move || self",
226 assert_eq!(stringify_expr!(|| -> u8 { self }), "|| -> u8 { self }");
227 assert_eq!(stringify_expr!(1 + || {}), "1 + (|| {})"); // ??
230 assert_eq!(stringify_expr!({}), "{}");
231 assert_eq!(stringify_expr!(unsafe {}), "unsafe {}");
232 assert_eq!(stringify_expr!('a: {}), "'a: {}");
238 "#[attr] { }", // FIXME
250 assert_eq!(stringify_expr!(async {}), "async {}");
251 assert_eq!(stringify_expr!(async move {}), "async move {}");
254 assert_eq!(stringify_expr!(expr.await), "expr.await");
256 // ExprKind::TryBlock
257 assert_eq!(stringify_expr!(try {}), "try {}"); // FIXME
260 assert_eq!(stringify_expr!(expr = true), "expr = true");
262 // ExprKind::AssignOp
263 assert_eq!(stringify_expr!(expr += true), "expr += true");
266 assert_eq!(stringify_expr!(expr.field), "expr.field");
267 assert_eq!(stringify_expr!(expr.0), "expr.0");
270 assert_eq!(stringify_expr!(expr[true]), "expr[true]");
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");
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>");
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");
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");
302 // ExprKind::Continue
303 assert_eq!(stringify_expr!(continue), "continue");
304 assert_eq!(stringify_expr!(continue 'a), "continue 'a");
307 assert_eq!(stringify_expr!(return), "return");
308 assert_eq!(stringify_expr!(return true), "return true");
311 assert_eq!(stringify_expr!(mac!(...)), "mac!(...)");
312 assert_eq!(stringify_expr!(mac![...]), "mac![...]");
313 assert_eq!(stringify_expr!(mac! { ... }), "mac! { ... }");
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}");
329 assert_eq!(stringify_expr!([(); 0]), "[(); 0]");
332 assert_eq!(stringify_expr!((expr)), "(expr)");
335 assert_eq!(stringify_expr!(expr?), "expr?");
338 assert_eq!(stringify_expr!(yield), "yield");
339 assert_eq!(stringify_expr!(yield true), "yield true");
344 // ItemKind::ExternCrate
353 pub extern crate self as std;
355 "pub extern crate self as std;",
361 pub use crate::{a, b::c};
363 "pub use crate::{a, b::c};",
369 pub static S: () = {};
371 "pub static S: () = {};",
375 static mut S: () = {};
377 "static mut S: () = {};",
383 "static S: () ;", // FIXME
389 "static mut S: () ;",
395 pub const S: () = {};
397 "pub const S: () = {};",
403 "const S: () ;", // FIXME
409 pub default const async unsafe extern "C" fn f() {}
411 "pub default const async unsafe extern \"C\" fn f() {}",
440 // ItemKind::ForeignMod
456 unsafe extern "C++" {}
458 "unsafe extern \"C++\" {}",
465 pub default type Type<'a>: Bound
470 "pub default type Type<'a>: Bound where Self: 'a = T;",
488 "enum Empty { Unit, Tuple(), Struct {}, }",
501 "enum Enum<T> where T: 'a {\n Unit,\n Tuple(T),\n Struct {\n t: T,\n },\n}",
538 "struct Struct<T> where T: 'a {\n t: T,\n}",
546 "pub union Union {}",
550 union Union<T> where T: 'a {
554 "union Union<T> where T: 'a {\n t: T,\n}",
560 pub unsafe auto trait Send {}
562 "pub unsafe auto trait Send {}",
566 trait Trait<'a>: Sized
572 "trait Trait<'a>: Sized where Self: 'a {}",
575 // ItemKind::TraitAlias
578 pub trait Trait<T> = Sized where T: 'a;
588 "pub impl Struct {}",
594 "impl <T> Struct<T> {}", // FIXME
598 pub impl Trait for Struct {}
600 "pub impl Trait for Struct {}",
604 impl ~const Struct {}
606 "impl Struct {}", // FIXME
610 assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
611 assert_eq!(stringify_item!(mac![...];), "mac![...];");
612 assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");
614 // ItemKind::MacroDef
617 macro_rules! stringify {
621 "macro_rules! stringify { () => {} ; }", // FIXME
625 pub macro stringify() {}
627 "pub macro stringify { () => {} }",
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");
642 assert_eq!(stringify_pat!(_), "_");
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 @ _");
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
662 stringify_pat!(<Struct as Trait>::Type {}),
663 "<Struct as Trait>::Type { }",
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()");
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");
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");
687 assert_eq!(stringify_pat!(()), "()");
688 assert_eq!(stringify_pat!((true,)), "(true,)");
689 assert_eq!(stringify_pat!((true, false)), "(true, false)");
692 assert_eq!(stringify_pat!(box pat), "box pat");
695 assert_eq!(stringify_pat!(&pat), "&pat");
696 assert_eq!(stringify_pat!(&mut pat), "&mut pat");
699 assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");
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");
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]");
715 assert_eq!(stringify_pat!(..), "..");
718 assert_eq!(stringify_pat!((pat)), "(pat)");
721 assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
722 assert_eq!(stringify_pat!(mac![...]), "mac![...]");
723 assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
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() -> ()");
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;");
755 assert_eq!(stringify_stmt!(loop {}), "loop {}");
758 assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");
761 assert_eq!(stringify_stmt!(;), ";");
764 assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
765 assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
766 assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
772 assert_eq!(stringify_ty!([T]), "[T]");
775 assert_eq!(stringify_ty!([T; 0]), "[T; 0]");
778 assert_eq!(stringify_ty!(*const T), "*const T");
779 assert_eq!(stringify_ty!(*mut T), "*mut T");
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");
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)");
793 assert_eq!(stringify_ty!(for<> fn()), "fn()");
794 assert_eq!(stringify_ty!(for<'a> fn()), "for<'a>fn()"); // FIXME
797 assert_eq!(stringify_ty!(!), "!");
800 assert_eq!(stringify_ty!(()), "()");
801 assert_eq!(stringify_ty!((T,)), "(T,)");
802 assert_eq!(stringify_ty!((T, U)), "(T, U)");
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");
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");
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");
830 assert_eq!(stringify_ty!((T)), "(T)");
833 assert_eq!(stringify_ty!(_), "_");
836 assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
837 assert_eq!(stringify_ty!(mac![...]), "mac![...]");
838 assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
843 // VisibilityKind::Public
844 assert_eq!(stringify_vis!(pub), "pub ");
846 // VisibilityKind::Crate
847 assert_eq!(stringify_vis!(crate), "crate ");
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) ");
859 // VisibilityKind::Inherited
860 // Directly calling `stringify_vis!()` does not work.
861 macro_rules! stringify_inherited_vis {
862 ($vis:vis struct) => {
866 assert_eq!(stringify_inherited_vis!(struct), "");