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: {}");
252 assert_eq
!(stringify_expr
!(async {}
), "async {}");
253 assert_eq
!(stringify_expr
!(async
move {}
), "async move {}");
256 assert_eq
!(stringify_expr
!(expr
.await
), "expr.await");
258 // ExprKind::TryBlock
259 assert_eq
!(stringify_expr
!(try {}
), "try {}");
262 assert_eq
!(stringify_expr
!(expr
= true), "expr = true");
264 // ExprKind::AssignOp
265 assert_eq
!(stringify_expr
!(expr
+= true), "expr += true");
268 assert_eq
!(stringify_expr
!(expr
.field
), "expr.field");
269 assert_eq
!(stringify_expr
!(expr
.0), "expr.0");
272 assert_eq
!(stringify_expr
!(expr
[true]), "expr[true]");
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");
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>");
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");
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");
304 // ExprKind::Continue
305 assert_eq
!(stringify_expr
!(continue), "continue");
306 assert_eq
!(stringify_expr
!(continue 'a
), "continue 'a");
309 assert_eq
!(stringify_expr
!(return), "return");
310 assert_eq
!(stringify_expr
!(return true), "return true");
313 assert_eq
!(stringify_expr
!(mac
!(...)), "mac!(...)");
314 assert_eq
!(stringify_expr
!(mac
![...]), "mac![...]");
315 assert_eq
!(stringify_expr
!(mac
! { ... }
), "mac! { ... }");
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 }");
331 assert_eq
!(stringify_expr
!([(); 0]), "[(); 0]");
334 assert_eq
!(stringify_expr
!((expr
)), "(expr)");
337 assert_eq
!(stringify_expr
!(expr?
), "expr?");
340 assert_eq
!(stringify_expr
!(yield), "yield");
341 assert_eq
!(stringify_expr
!(yield true), "yield true");
346 // ItemKind::ExternCrate
355 pub extern crate self as std
;
357 "pub extern crate self as std;",
363 pub use crate::{a, b::c}
;
365 "pub use crate::{a, b::c};",
371 pub static S
: () = {}
;
373 "pub static S: () = {};",
377 static mut S
: () = {}
;
379 "static mut S: () = {};",
397 pub const S
: () = {}
;
399 "pub const S: () = {};",
411 pub default const async
unsafe extern "C" fn f() {}
413 "pub default const async unsafe extern \"C\" fn f() {}",
442 // ItemKind::ForeignMod
458 unsafe extern "C++" {}
460 "unsafe extern \"C++\" {}",
467 pub default type Type
<'a
>: Bound
472 "pub default type Type<'a>: Bound where Self: 'a = T;",
490 "enum Empty { Unit, Tuple(), Struct {}, }",
503 "enum Enum<T> where T: 'a {\n\
546 "struct Struct<T> where T: 'a {\n\
556 "pub union Union {}",
560 union Union
<T
> where T
: 'a
{
564 "union Union<T> where T: 'a {\n\
572 pub unsafe auto trait Send {}
574 "pub unsafe auto trait Send {}",
578 trait Trait
<'a
>: Sized
584 "trait Trait<'a>: Sized where Self: 'a {}",
587 // ItemKind::TraitAlias
590 pub trait Trait
<T
> = Sized
where T
: 'a
;
592 "pub trait Trait<T> = Sized where T: 'a;",
600 "pub impl Struct {}",
606 "impl<T> Struct<T> {}",
610 pub impl Trait
for Struct {}
612 "pub impl Trait for Struct {}",
616 impl<T
> const Trait
for T {}
618 "impl<T> const Trait for T {}",
622 impl ~const Struct {}
624 "impl Struct {}", // FIXME
628 assert_eq
!(stringify_item
!(mac
!(...);), "mac!(...);");
629 assert_eq
!(stringify_item
!(mac
![...];), "mac![...];");
630 assert_eq
!(stringify_item
!(mac
! { ... }
), "mac! { ... }");
632 // ItemKind::MacroDef
635 macro_rules
! stringify
{
639 "macro_rules! stringify { () => {} ; }", // FIXME
643 pub macro stringify() {}
645 "pub macro stringify { () => {} }",
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");
660 assert_eq
!(stringify_pat
!(_
), "_");
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 @ _");
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
680 stringify_pat
!(<Struct
as Trait
>::Type {}
),
681 "<Struct as Trait>::Type {}",
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()");
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");
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");
705 assert_eq
!(stringify_pat
!(()), "()");
706 assert_eq
!(stringify_pat
!((true,)), "(true,)");
707 assert_eq
!(stringify_pat
!((true, false)), "(true, false)");
710 assert_eq
!(stringify_pat
!(box pat
), "box pat");
713 assert_eq
!(stringify_pat
!(&pat
), "&pat");
714 assert_eq
!(stringify_pat
!(&mut pat
), "&mut pat");
717 assert_eq
!(stringify_pat
!(1_000_i8), "1_000_i8");
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");
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]");
733 assert_eq
!(stringify_pat
!(..), "..");
736 assert_eq
!(stringify_pat
!((pat
)), "(pat)");
739 assert_eq
!(stringify_pat
!(mac
!(...)), "mac!(...)");
740 assert_eq
!(stringify_pat
!(mac
![...]), "mac![...]");
741 assert_eq
!(stringify_pat
!(mac
! { ... }
), "mac! { ... }");
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() -> ()");
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;");
773 assert_eq
!(stringify_stmt
!(loop {}
), "loop {}");
776 assert_eq
!(stringify_stmt
!(1 + 1), "1 + 1;");
779 assert_eq
!(stringify_stmt
!(;), ";");
782 assert_eq
!(stringify_stmt
!(mac
!(...)), "mac!(...)");
783 assert_eq
!(stringify_stmt
!(mac
![...]), "mac![...]");
784 assert_eq
!(stringify_stmt
!(mac
! { ... }
), "mac! { ... }");
790 assert_eq
!(stringify_ty
!([T
]), "[T]");
793 assert_eq
!(stringify_ty
!([T
; 0]), "[T; 0]");
796 assert_eq
!(stringify_ty
!(*const T
), "*const T");
797 assert_eq
!(stringify_ty
!(*mut T
), "*mut T");
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");
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)");
811 assert_eq
!(stringify_ty
!(for<> fn()), "fn()");
812 assert_eq
!(stringify_ty
!(for<'a
> fn()), "for<'a> fn()");
815 assert_eq
!(stringify_ty
!(!), "!");
818 assert_eq
!(stringify_ty
!(()), "()");
819 assert_eq
!(stringify_ty
!((T
,)), "(T,)");
820 assert_eq
!(stringify_ty
!((T
, U
)), "(T, U)");
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");
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");
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");
848 assert_eq
!(stringify_ty
!((T
)), "(T)");
851 assert_eq
!(stringify_ty
!(_
), "_");
854 assert_eq
!(stringify_ty
!(mac
!(...)), "mac!(...)");
855 assert_eq
!(stringify_ty
!(mac
![...]), "mac![...]");
856 assert_eq
!(stringify_ty
!(mac
! { ... }
), "mac! { ... }");
861 // VisibilityKind::Public
862 assert_eq
!(stringify_vis
!(pub), "pub ");
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) ");
875 // VisibilityKind::Inherited
876 // Directly calling `stringify_vis!()` does not work.
877 macro_rules
! stringify_inherited_vis
{
878 ($vis
:vis
struct) => {
882 assert_eq
!(stringify_inherited_vis
!(struct), "");