3 // compile-flags: --test
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)]
18 macro_rules! stringify_block {
24 macro_rules! stringify_expr {
30 macro_rules! stringify_item {
36 macro_rules! stringify_meta {
42 macro_rules! stringify_pat {
48 macro_rules! stringify_path {
54 macro_rules! stringify_stmt {
60 macro_rules! stringify_ty {
66 macro_rules! stringify_vis {
74 assert_eq!(stringify_block!({}), "{}");
75 assert_eq!(stringify_block!({ true }), "{ true }");
76 assert_eq!(stringify_block!({ return }), "{ return }");
95 assert_eq!(stringify_expr!(box expr), "box expr");
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]");
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!(()()), "()()");
112 // ExprKind::MethodCall
113 assert_eq!(stringify_expr!(x.f()), "x.f()");
114 assert_eq!(stringify_expr!(x.f::<u8>()), "x.f::<u8>()");
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)");
123 assert_eq!(stringify_expr!(true || false), "true || false");
124 assert_eq!(stringify_expr!(true || false && false), "true || false && false");
127 assert_eq!(stringify_expr!(*expr), "*expr");
128 assert_eq!(stringify_expr!(!expr), "!expr");
129 assert_eq!(stringify_expr!(-expr), "-expr");
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");
137 assert_eq!(stringify_expr!(expr as T), "expr as T");
138 assert_eq!(stringify_expr!(expr as T<u8>), "expr as T<u8>");
141 assert_eq!(stringify_expr!(expr: T), "expr: T");
142 assert_eq!(stringify_expr!(expr: T<u8>), "expr: T<u8>");
145 assert_eq!(stringify_expr!(if true {}), "if true {}");
147 stringify_expr!(if true {
150 "if true {} else {}",
153 stringify_expr!(if let true = true {
156 "if let true = true {} else {}",
159 stringify_expr!(if true {
162 "if true {} else if false {}",
165 stringify_expr!(if true {
169 "if true {} else if false {} else {}",
172 stringify_expr!(if true {
179 "if true { return; } else if false { 0 } else { 0 }",
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 {}");
188 assert_eq!(stringify_expr!(for _ in x {}), "for _ in x {}");
189 assert_eq!(stringify_expr!('a: for _ in x {}), "'a: for _ in x {}");
192 assert_eq!(stringify_expr!(loop {}), "loop {}");
193 assert_eq!(stringify_expr!('a: loop {}), "'a: loop {}");
196 assert_eq!(stringify_expr!(match self {}), "match self {}");
198 stringify_expr!(match self {
201 "match self { Ok => 1, }",
204 stringify_expr!(match self {
208 "match self { Ok => 1, Err => 0, }",
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
223 stringify_expr!(static async || self),
224 "static async || self",
226 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
228 stringify_expr!(static async move || self),
229 "static async move || self",
231 assert_eq!(stringify_expr!(|| -> u8 { self }), "|| -> u8 { self }");
232 assert_eq!(stringify_expr!(1 + || {}), "1 + (|| {})"); // ??
235 assert_eq!(stringify_expr!({}), "{}");
236 assert_eq!(stringify_expr!(unsafe {}), "unsafe {}");
237 assert_eq!(stringify_expr!('a: {}), "'a: {}");
257 assert_eq!(stringify_expr!(async {}), "async {}");
258 assert_eq!(stringify_expr!(async move {}), "async move {}");
261 assert_eq!(stringify_expr!(expr.await), "expr.await");
263 // ExprKind::TryBlock
264 assert_eq!(stringify_expr!(try {}), "try {}");
267 assert_eq!(stringify_expr!(expr = true), "expr = true");
269 // ExprKind::AssignOp
270 assert_eq!(stringify_expr!(expr += true), "expr += true");
273 assert_eq!(stringify_expr!(expr.field), "expr.field");
274 assert_eq!(stringify_expr!(expr.0), "expr.0");
277 assert_eq!(stringify_expr!(expr[true]), "expr[true]");
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");
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>");
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");
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");
309 // ExprKind::Continue
310 assert_eq!(stringify_expr!(continue), "continue");
311 assert_eq!(stringify_expr!(continue 'a), "continue 'a");
314 assert_eq!(stringify_expr!(return), "return");
315 assert_eq!(stringify_expr!(return true), "return true");
318 assert_eq!(stringify_expr!(mac!(...)), "mac!(...)");
319 assert_eq!(stringify_expr!(mac![...]), "mac![...]");
320 assert_eq!(stringify_expr!(mac! { ... }), "mac! { ... }");
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 }");
336 assert_eq!(stringify_expr!([(); 0]), "[(); 0]");
339 assert_eq!(stringify_expr!((expr)), "(expr)");
342 assert_eq!(stringify_expr!(expr?), "expr?");
345 assert_eq!(stringify_expr!(yield), "yield");
346 assert_eq!(stringify_expr!(yield true), "yield true");
351 // ItemKind::ExternCrate
360 pub extern crate self as std;
362 "pub extern crate self as std;",
368 pub use crate::{a, b::c};
370 "pub use crate::{a, b::c};",
376 pub static S: () = {};
378 "pub static S: () = {};",
382 static mut S: () = {};
384 "static mut S: () = {};",
402 pub const S: () = {};
404 "pub const S: () = {};",
416 pub default const async unsafe extern "C" fn f() {}
418 "pub default const async unsafe extern \"C\" fn f() {}",
447 // ItemKind::ForeignMod
463 unsafe extern "C++" {}
465 "unsafe extern \"C++\" {}",
472 pub default type Type<'a>: Bound
477 "pub default type Type<'a>: Bound where Self: 'a = T;",
495 "enum Empty { Unit, Tuple(), Struct {}, }",
508 "enum Enum<T> where T: 'a {\n\
551 "struct Struct<T> where T: 'a {\n\
561 "pub union Union {}",
565 union Union<T> where T: 'a {
569 "union Union<T> where T: 'a {\n\
577 pub unsafe auto trait Send {}
579 "pub unsafe auto trait Send {}",
583 trait Trait<'a>: Sized
589 "trait Trait<'a>: Sized where Self: 'a {}",
592 // ItemKind::TraitAlias
595 pub trait Trait<T> = Sized where T: 'a;
597 "pub trait Trait<T> = Sized where T: 'a;",
605 "pub impl Struct {}",
611 "impl<T> Struct<T> {}",
615 pub impl Trait for Struct {}
617 "pub impl Trait for Struct {}",
621 impl<T> const Trait for T {}
623 "impl<T> const Trait for T {}",
627 impl ~const Struct {}
629 "impl Struct {}", // FIXME
633 assert_eq!(stringify_item!(mac!(...);), "mac!(...);");
634 assert_eq!(stringify_item!(mac![...];), "mac![...];");
635 assert_eq!(stringify_item!(mac! { ... }), "mac! { ... }");
637 // ItemKind::MacroDef
640 macro_rules! stringify {
644 "macro_rules! stringify { () => {} ; }", // FIXME
648 pub macro stringify() {}
650 "pub macro stringify { () => {} }",
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");
665 assert_eq!(stringify_pat!(_), "_");
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 @ _");
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
685 stringify_pat!(<Struct as Trait>::Type {}),
686 "<Struct as Trait>::Type {}",
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()");
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");
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");
710 assert_eq!(stringify_pat!(()), "()");
711 assert_eq!(stringify_pat!((true,)), "(true,)");
712 assert_eq!(stringify_pat!((true, false)), "(true, false)");
715 assert_eq!(stringify_pat!(box pat), "box pat");
718 assert_eq!(stringify_pat!(&pat), "&pat");
719 assert_eq!(stringify_pat!(&mut pat), "&mut pat");
722 assert_eq!(stringify_pat!(1_000_i8), "1_000_i8");
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");
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]");
738 assert_eq!(stringify_pat!(..), "..");
741 assert_eq!(stringify_pat!((pat)), "(pat)");
744 assert_eq!(stringify_pat!(mac!(...)), "mac!(...)");
745 assert_eq!(stringify_pat!(mac![...]), "mac![...]");
746 assert_eq!(stringify_pat!(mac! { ... }), "mac! { ... }");
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() -> ()");
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;");
778 assert_eq!(stringify_stmt!(loop {}), "loop {}");
781 assert_eq!(stringify_stmt!(1 + 1), "1 + 1;");
784 assert_eq!(stringify_stmt!(;), ";");
787 assert_eq!(stringify_stmt!(mac!(...)), "mac!(...)");
788 assert_eq!(stringify_stmt!(mac![...]), "mac![...]");
789 assert_eq!(stringify_stmt!(mac! { ... }), "mac! { ... }");
795 assert_eq!(stringify_ty!([T]), "[T]");
798 assert_eq!(stringify_ty!([T; 0]), "[T; 0]");
801 assert_eq!(stringify_ty!(*const T), "*const T");
802 assert_eq!(stringify_ty!(*mut T), "*mut T");
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");
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)");
816 assert_eq!(stringify_ty!(for<> fn()), "fn()");
817 assert_eq!(stringify_ty!(for<'a> fn()), "for<'a> fn()");
820 assert_eq!(stringify_ty!(!), "!");
823 assert_eq!(stringify_ty!(()), "()");
824 assert_eq!(stringify_ty!((T,)), "(T,)");
825 assert_eq!(stringify_ty!((T, U)), "(T, U)");
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");
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");
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");
853 assert_eq!(stringify_ty!((T)), "(T)");
856 assert_eq!(stringify_ty!(_), "_");
859 assert_eq!(stringify_ty!(mac!(...)), "mac!(...)");
860 assert_eq!(stringify_ty!(mac![...]), "mac![...]");
861 assert_eq!(stringify_ty!(mac! { ... }), "mac! { ... }");
866 // VisibilityKind::Public
867 assert_eq!(stringify_vis!(pub), "pub ");
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) ");
881 // VisibilityKind::Inherited
882 // Directly calling `stringify_vis!()` does not work.
883 macro_rules! stringify_inherited_vis {
884 ($vis:vis struct) => {
888 assert_eq!(stringify_inherited_vis!(struct), "");