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(half_open_range_patterns)]
12 #![feature(more_qualified_paths)]
13 #![feature(raw_ref_op)]
14 #![feature(trait_alias)]
15 #![feature(try_blocks)]
16 #![feature(type_ascription)]
17 #![deny(unused_macros)]
19 macro_rules
! stringify_block
{
25 macro_rules
! stringify_expr
{
31 macro_rules
! stringify_item
{
37 macro_rules
! stringify_meta
{
43 macro_rules
! stringify_pat
{
49 macro_rules
! stringify_path
{
55 macro_rules
! stringify_stmt
{
61 macro_rules
! stringify_ty
{
67 macro_rules
! stringify_vis
{
75 assert_eq
!(stringify_block
!({}
), "{}");
76 assert_eq
!(stringify_block
!({ true }
), "{ true }");
77 assert_eq
!(stringify_block
!({ return }
), "{ return }");
96 assert_eq
!(stringify_expr
!(box expr
), "box expr");
99 assert_eq
!(stringify_expr
!([]), "[]");
100 assert_eq
!(stringify_expr
!([true]), "[true]");
101 assert_eq
!(stringify_expr
!([true,]), "[true]");
102 assert_eq
!(stringify_expr
!([true, true]), "[true, true]");
105 assert_eq
!(stringify_expr
!(f()), "f()");
106 assert_eq
!(stringify_expr
!(f
::<u8>()), "f::<u8>()");
107 assert_eq
!(stringify_expr
!(f
::<1>()), "f::<1>()");
108 assert_eq
!(stringify_expr
!(f
::<'a
, u8, 1>()), "f::<'a, u8, 1>()");
109 assert_eq
!(stringify_expr
!(f(true)), "f(true)");
110 assert_eq
!(stringify_expr
!(f(true,)), "f(true)");
111 assert_eq
!(stringify_expr
!(()()), "()()");
113 // ExprKind::MethodCall
114 assert_eq
!(stringify_expr
!(x
.f()), "x.f()");
115 assert_eq
!(stringify_expr
!(x
.f
::<u8>()), "x.f::<u8>()");
118 assert_eq
!(stringify_expr
!(()), "()");
119 assert_eq
!(stringify_expr
!((true,)), "(true,)");
120 assert_eq
!(stringify_expr
!((true, false)), "(true, false)");
121 assert_eq
!(stringify_expr
!((true, false,)), "(true, false)");
124 assert_eq
!(stringify_expr
!(true || false), "true || false");
125 assert_eq
!(stringify_expr
!(true || false && false), "true || false && false");
128 assert_eq
!(stringify_expr
!(*expr
), "*expr");
129 assert_eq
!(stringify_expr
!(!expr
), "!expr");
130 assert_eq
!(stringify_expr
!(-expr
), "-expr");
133 assert_eq
!(stringify_expr
!('x'
), "'x'");
134 assert_eq
!(stringify_expr
!(1_000_i8), "1_000_i8");
135 assert_eq
!(stringify_expr
!(1.00000000000000001), "1.00000000000000001");
138 assert_eq
!(stringify_expr
!(expr
as T
), "expr as T");
139 assert_eq
!(stringify_expr
!(expr
as T
<u8>), "expr as T<u8>");
142 assert_eq
!(stringify_expr
!(expr
: T
), "expr: T");
143 assert_eq
!(stringify_expr
!(expr
: T
<u8>), "expr: T<u8>");
146 assert_eq
!(stringify_expr
!(if true {}
), "if true {}");
148 stringify_expr
!(if true {
151 "if true {} else {}",
154 stringify_expr
!(if let true = true {
157 "if let true = true {} else {}",
160 stringify_expr
!(if true {
163 "if true {} else if false {}",
166 stringify_expr
!(if true {
170 "if true {} else if false {} else {}",
173 stringify_expr
!(if true {
180 "if true { return; } else if false { 0 } else { 0 }",
184 assert_eq
!(stringify_expr
!(while true {}
), "while true {}");
185 assert_eq
!(stringify_expr
!('a
: while true {}
), "'a: while true {}");
186 assert_eq
!(stringify_expr
!(while let true = true {}
), "while let true = true {}");
189 assert_eq
!(stringify_expr
!(for _
in x {}
), "for _ in x {}");
190 assert_eq
!(stringify_expr
!('a
: for _
in x {}
), "'a: for _ in x {}");
193 assert_eq
!(stringify_expr
!(loop {}
), "loop {}");
194 assert_eq
!(stringify_expr
!('a
: loop {}
), "'a: loop {}");
197 assert_eq
!(stringify_expr
!(match self {}
), "match self {}");
199 stringify_expr
!(match self {
202 "match self { Ok => 1, }",
205 stringify_expr
!(match self {
209 "match self { Ok => 1, Err => 0, }",
213 assert_eq
!(stringify_expr
!(|| {}
), "|| {}");
214 assert_eq
!(stringify_expr
!(|x
| {}
), "|x| {}");
215 assert_eq
!(stringify_expr
!(|x
: u8| {}
), "|x: u8| {}");
216 assert_eq
!(stringify_expr
!(|| ()), "|| ()");
217 assert_eq
!(stringify_expr
!(move || self), "move || self");
218 assert_eq
!(stringify_expr
!(async
|| self), "async || self");
219 assert_eq
!(stringify_expr
!(async
move || self), "async move || self");
220 assert_eq
!(stringify_expr
!(static || self), "static || self");
221 assert_eq
!(stringify_expr
!(static move || self), "static move || self");
222 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
224 stringify_expr
!(static async
|| self),
225 "static async || self",
227 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5149
229 stringify_expr
!(static async
move || self),
230 "static async move || self",
232 assert_eq
!(stringify_expr
!(|| -> u8 { self }
), "|| -> u8 { self }");
233 assert_eq
!(stringify_expr
!(1 + || {}
), "1 + (|| {})"); // ??
236 assert_eq
!(stringify_expr
!({}
), "{}");
237 assert_eq
!(stringify_expr
!(unsafe {}
), "unsafe {}");
238 assert_eq
!(stringify_expr
!('a
: {}
), "'a: {}");
258 assert_eq
!(stringify_expr
!(async {}
), "async {}");
259 assert_eq
!(stringify_expr
!(async
move {}
), "async move {}");
262 assert_eq
!(stringify_expr
!(expr
.await
), "expr.await");
264 // ExprKind::TryBlock
265 assert_eq
!(stringify_expr
!(try {}
), "try {}");
268 assert_eq
!(stringify_expr
!(expr
= true), "expr = true");
270 // ExprKind::AssignOp
271 assert_eq
!(stringify_expr
!(expr
+= true), "expr += true");
274 assert_eq
!(stringify_expr
!(expr
.field
), "expr.field");
275 assert_eq
!(stringify_expr
!(expr
.0), "expr.0");
278 assert_eq
!(stringify_expr
!(expr
[true]), "expr[true]");
281 assert_eq
!(stringify_expr
!(..), "..");
282 assert_eq
!(stringify_expr
!(..hi
), "..hi");
283 assert_eq
!(stringify_expr
!(lo
..), "lo..");
284 assert_eq
!(stringify_expr
!(lo
..hi
), "lo..hi");
285 assert_eq
!(stringify_expr
!(..=hi
), "..=hi");
286 assert_eq
!(stringify_expr
!(lo
..=hi
), "lo..=hi");
287 assert_eq
!(stringify_expr
!(-2..=-1), "-2..=-1");
290 assert_eq
!(stringify_expr
!(thing
), "thing");
291 assert_eq
!(stringify_expr
!(m
::thing
), "m::thing");
292 assert_eq
!(stringify_expr
!(self::thing
), "self::thing");
293 assert_eq
!(stringify_expr
!(crate::thing
), "crate::thing");
294 assert_eq
!(stringify_expr
!(Self::thing
), "Self::thing");
295 assert_eq
!(stringify_expr
!(<Self as T
>::thing
), "<Self as T>::thing");
296 assert_eq
!(stringify_expr
!(Self::<'
static>), "Self::<'static>");
299 assert_eq
!(stringify_expr
!(&expr
), "&expr");
300 assert_eq
!(stringify_expr
!(&mut expr
), "&mut expr");
301 assert_eq
!(stringify_expr
!(&raw
const expr
), "&raw const expr");
302 assert_eq
!(stringify_expr
!(&raw
mut expr
), "&raw mut expr");
305 assert_eq
!(stringify_expr
!(break), "break");
306 assert_eq
!(stringify_expr
!(break 'a
), "break 'a");
307 assert_eq
!(stringify_expr
!(break true), "break true");
308 assert_eq
!(stringify_expr
!(break 'a
true), "break 'a true");
310 // ExprKind::Continue
311 assert_eq
!(stringify_expr
!(continue), "continue");
312 assert_eq
!(stringify_expr
!(continue 'a
), "continue 'a");
315 assert_eq
!(stringify_expr
!(return), "return");
316 assert_eq
!(stringify_expr
!(return true), "return true");
319 assert_eq
!(stringify_expr
!(mac
!(...)), "mac!(...)");
320 assert_eq
!(stringify_expr
!(mac
![...]), "mac![...]");
321 assert_eq
!(stringify_expr
!(mac
! { ... }
), "mac! { ... }");
324 assert_eq
!(stringify_expr
!(Struct {}
), "Struct {}");
325 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
326 assert_eq
!(stringify_expr
!(<Struct
as Trait
>::Type {}
), "<Struct as Trait>::Type {}");
327 assert_eq
!(stringify_expr
!(Struct { .. }
), "Struct { .. }");
328 assert_eq
!(stringify_expr
!(Struct { ..base }
), "Struct { ..base }");
329 assert_eq
!(stringify_expr
!(Struct { x }
), "Struct { x }");
330 assert_eq
!(stringify_expr
!(Struct { x, .. }
), "Struct { x, .. }");
331 assert_eq
!(stringify_expr
!(Struct { x, ..base }
), "Struct { x, ..base }");
332 assert_eq
!(stringify_expr
!(Struct { x: true }
), "Struct { x: true }");
333 assert_eq
!(stringify_expr
!(Struct { x: true, .. }
), "Struct { x: true, .. }");
334 assert_eq
!(stringify_expr
!(Struct { x: true, ..base }
), "Struct { x: true, ..base }");
337 assert_eq
!(stringify_expr
!([(); 0]), "[(); 0]");
340 assert_eq
!(stringify_expr
!((expr
)), "(expr)");
343 assert_eq
!(stringify_expr
!(expr?
), "expr?");
346 assert_eq
!(stringify_expr
!(yield), "yield");
347 assert_eq
!(stringify_expr
!(yield true), "yield true");
352 // ItemKind::ExternCrate
361 pub extern crate self as std
;
363 "pub extern crate self as std;",
369 pub use crate::{a, b::c}
;
371 "pub use crate::{a, b::c};",
377 pub static S
: () = {}
;
379 "pub static S: () = {};",
383 static mut S
: () = {}
;
385 "static mut S: () = {};",
403 pub const S
: () = {}
;
405 "pub const S: () = {};",
417 pub default const async
unsafe extern "C" fn f() {}
419 "pub default const async unsafe extern \"C\" fn f() {}",
448 // ItemKind::ForeignMod
464 unsafe extern "C++" {}
466 "unsafe extern \"C++\" {}",
473 pub default type Type
<'a
>: Bound
478 "pub default type Type<'a>: Bound where Self: 'a = T;",
496 "enum Empty { Unit, Tuple(), Struct {}, }",
509 "enum Enum<T> where T: 'a {\n\
552 "struct Struct<T> where T: 'a {\n\
562 "pub union Union {}",
566 union Union
<T
> where T
: 'a
{
570 "union Union<T> where T: 'a {\n\
578 pub unsafe auto trait Send {}
580 "pub unsafe auto trait Send {}",
584 trait Trait
<'a
>: Sized
590 "trait Trait<'a>: Sized where Self: 'a {}",
593 // ItemKind::TraitAlias
596 pub trait Trait
<T
> = Sized
where T
: 'a
;
598 "pub trait Trait<T> = Sized where T: 'a;",
606 "pub impl Struct {}",
612 "impl<T> Struct<T> {}",
616 pub impl Trait
for Struct {}
618 "pub impl Trait for Struct {}",
622 impl<T
> const Trait
for T {}
624 "impl<T> const Trait for T {}",
628 impl ~const Struct {}
630 "impl Struct {}", // FIXME
634 assert_eq
!(stringify_item
!(mac
!(...);), "mac!(...);");
635 assert_eq
!(stringify_item
!(mac
![...];), "mac![...];");
636 assert_eq
!(stringify_item
!(mac
! { ... }
), "mac! { ... }");
638 // ItemKind::MacroDef
641 macro_rules
! stringify
{
645 "macro_rules! stringify { () => {} ; }", // FIXME
649 pub macro stringify() {}
651 "pub macro stringify { () => {} }",
657 assert_eq
!(stringify_meta
!(k
), "k");
658 assert_eq
!(stringify_meta
!(k
= "v"), "k = \"v\"");
659 assert_eq
!(stringify_meta
!(list(k1
, k2
= "v")), "list(k1, k2 = \"v\")");
660 assert_eq
!(stringify_meta
!(serde
::k
), "serde::k");
666 assert_eq
!(stringify_pat
!(_
), "_");
669 assert_eq
!(stringify_pat
!(_x
), "_x");
670 assert_eq
!(stringify_pat
!(ref _x
), "ref _x");
671 assert_eq
!(stringify_pat
!(mut _x
), "mut _x");
672 assert_eq
!(stringify_pat
!(ref mut _x
), "ref mut _x");
673 assert_eq
!(stringify_pat
!(ref mut _x @ _
), "ref mut _x @ _");
676 assert_eq
!(stringify_pat
!(Struct {}
), "Struct {}");
677 assert_eq
!(stringify_pat
!(Struct
::<u8> {}
), "Struct::<u8> {}");
678 assert_eq
!(stringify_pat
!(Struct
::<'
static> {}
), "Struct::<'static> {}");
679 assert_eq
!(stringify_pat
!(Struct { x }
), "Struct { x }");
680 assert_eq
!(stringify_pat
!(Struct { x: _x }
), "Struct { x: _x }");
681 assert_eq
!(stringify_pat
!(Struct { .. }
), "Struct { .. }");
682 assert_eq
!(stringify_pat
!(Struct { x, .. }
), "Struct { x, .. }");
683 assert_eq
!(stringify_pat
!(Struct { x: _x, .. }
), "Struct { x: _x, .. }");
684 #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
686 stringify_pat
!(<Struct
as Trait
>::Type {}
),
687 "<Struct as Trait>::Type {}",
690 // PatKind::TupleStruct
691 assert_eq
!(stringify_pat
!(Tuple()), "Tuple()");
692 assert_eq
!(stringify_pat
!(Tuple
::<u8>()), "Tuple::<u8>()");
693 assert_eq
!(stringify_pat
!(Tuple
::<'
static>()), "Tuple::<'static>()");
694 assert_eq
!(stringify_pat
!(Tuple(x
)), "Tuple(x)");
695 assert_eq
!(stringify_pat
!(Tuple(..)), "Tuple(..)");
696 assert_eq
!(stringify_pat
!(Tuple(x
, ..)), "Tuple(x, ..)");
697 assert_eq
!(stringify_pat
!(<Struct
as Trait
>::Type()), "<Struct as Trait>::Type()");
700 assert_eq
!(stringify_pat
!(true | false), "true | false");
701 assert_eq
!(stringify_pat
!(| true), "true");
702 assert_eq
!(stringify_pat
!(|true| false), "true | false");
705 assert_eq
!(stringify_pat
!(crate::Path
), "crate::Path");
706 assert_eq
!(stringify_pat
!(Path
::<u8>), "Path::<u8>");
707 assert_eq
!(stringify_pat
!(Path
::<'
static>), "Path::<'static>");
708 assert_eq
!(stringify_pat
!(<Struct
as Trait
>::Type
), "<Struct as Trait>::Type");
711 assert_eq
!(stringify_pat
!(()), "()");
712 assert_eq
!(stringify_pat
!((true,)), "(true,)");
713 assert_eq
!(stringify_pat
!((true, false)), "(true, false)");
716 assert_eq
!(stringify_pat
!(box pat
), "box pat");
719 assert_eq
!(stringify_pat
!(&pat
), "&pat");
720 assert_eq
!(stringify_pat
!(&mut pat
), "&mut pat");
723 assert_eq
!(stringify_pat
!(1_000_i8), "1_000_i8");
726 assert_eq
!(stringify_pat
!(..1), "..1");
727 assert_eq
!(stringify_pat
!(0..), "0..");
728 assert_eq
!(stringify_pat
!(0..1), "0..1");
729 assert_eq
!(stringify_pat
!(0..=1), "0..=1");
730 assert_eq
!(stringify_pat
!(-2..=-1), "-2..=-1");
733 assert_eq
!(stringify_pat
!([]), "[]");
734 assert_eq
!(stringify_pat
!([true]), "[true]");
735 assert_eq
!(stringify_pat
!([true,]), "[true]");
736 assert_eq
!(stringify_pat
!([true, false]), "[true, false]");
739 assert_eq
!(stringify_pat
!(..), "..");
742 assert_eq
!(stringify_pat
!((pat
)), "(pat)");
745 assert_eq
!(stringify_pat
!(mac
!(...)), "mac!(...)");
746 assert_eq
!(stringify_pat
!(mac
![...]), "mac![...]");
747 assert_eq
!(stringify_pat
!(mac
! { ... }
), "mac! { ... }");
752 assert_eq
!(stringify_path
!(thing
), "thing");
753 assert_eq
!(stringify_path
!(m
::thing
), "m::thing");
754 assert_eq
!(stringify_path
!(self::thing
), "self::thing");
755 assert_eq
!(stringify_path
!(crate::thing
), "crate::thing");
756 assert_eq
!(stringify_path
!(Self::thing
), "Self::thing");
757 assert_eq
!(stringify_path
!(Self<'
static>), "Self<'static>");
758 assert_eq
!(stringify_path
!(Self::<'
static>), "Self<'static>");
759 assert_eq
!(stringify_path
!(Self()), "Self()");
760 assert_eq
!(stringify_path
!(Self() -> ()), "Self() -> ()");
766 assert_eq
!(stringify_stmt
!(let _
), "let _;");
767 assert_eq
!(stringify_stmt
!(let x
= true), "let x = true;");
768 assert_eq
!(stringify_stmt
!(let x
: bool
= true), "let x: bool = true;");
779 assert_eq
!(stringify_stmt
!(loop {}
), "loop {}");
782 assert_eq
!(stringify_stmt
!(1 + 1), "1 + 1;");
785 assert_eq
!(stringify_stmt
!(;), ";");
788 assert_eq
!(stringify_stmt
!(mac
!(...)), "mac!(...)");
789 assert_eq
!(stringify_stmt
!(mac
![...]), "mac![...]");
790 assert_eq
!(stringify_stmt
!(mac
! { ... }
), "mac! { ... }");
796 assert_eq
!(stringify_ty
!([T
]), "[T]");
799 assert_eq
!(stringify_ty
!([T
; 0]), "[T; 0]");
802 assert_eq
!(stringify_ty
!(*const T
), "*const T");
803 assert_eq
!(stringify_ty
!(*mut T
), "*mut T");
806 assert_eq
!(stringify_ty
!(&T
), "&T");
807 assert_eq
!(stringify_ty
!(&mut T
), "&mut T");
808 assert_eq
!(stringify_ty
!(&'a T
), "&'a T");
809 assert_eq
!(stringify_ty
!(&'a
mut T
), "&'a mut T");
812 assert_eq
!(stringify_ty
!(fn()), "fn()");
813 assert_eq
!(stringify_ty
!(fn() -> ()), "fn() -> ()");
814 assert_eq
!(stringify_ty
!(fn(u8)), "fn(u8)");
815 assert_eq
!(stringify_ty
!(fn(x
: u8)), "fn(x: u8)");
817 assert_eq
!(stringify_ty
!(for<> fn()), "fn()");
818 assert_eq
!(stringify_ty
!(for<'a
> fn()), "for<'a> fn()");
821 assert_eq
!(stringify_ty
!(!), "!");
824 assert_eq
!(stringify_ty
!(()), "()");
825 assert_eq
!(stringify_ty
!((T
,)), "(T,)");
826 assert_eq
!(stringify_ty
!((T
, U
)), "(T, U)");
829 assert_eq
!(stringify_ty
!(T
), "T");
830 assert_eq
!(stringify_ty
!(Ref
<'a
>), "Ref<'a>");
831 assert_eq
!(stringify_ty
!(PhantomData
<T
>), "PhantomData<T>");
832 assert_eq
!(stringify_ty
!(PhantomData
::<T
>), "PhantomData<T>");
833 assert_eq
!(stringify_ty
!(Fn() -> !), "Fn() -> !");
834 assert_eq
!(stringify_ty
!(Fn(u8) -> !), "Fn(u8) -> !");
835 assert_eq
!(stringify_ty
!(<Struct
as Trait
>::Type
), "<Struct as Trait>::Type");
837 // TyKind::TraitObject
838 assert_eq
!(stringify_ty
!(dyn Send
), "dyn Send");
839 assert_eq
!(stringify_ty
!(dyn Send
+ 'a
), "dyn Send + 'a");
840 assert_eq
!(stringify_ty
!(dyn 'a
+ Send
), "dyn 'a + Send");
841 assert_eq
!(stringify_ty
!(dyn ?Sized
), "dyn ?Sized");
842 assert_eq
!(stringify_ty
!(dyn ~const Clone
), "dyn Clone"); // FIXME
843 assert_eq
!(stringify_ty
!(dyn for<'a
> Send
), "dyn for<'a> Send");
846 assert_eq
!(stringify_ty
!(impl Send
), "impl Send");
847 assert_eq
!(stringify_ty
!(impl Send
+ 'a
), "impl Send + 'a");
848 assert_eq
!(stringify_ty
!(impl 'a
+ Send
), "impl 'a + Send");
849 assert_eq
!(stringify_ty
!(impl ?Sized
), "impl ?Sized");
850 assert_eq
!(stringify_ty
!(impl ~const Clone
), "impl Clone"); // FIXME
851 assert_eq
!(stringify_ty
!(impl for<'a
> Send
), "impl for<'a> Send");
854 assert_eq
!(stringify_ty
!((T
)), "(T)");
857 assert_eq
!(stringify_ty
!(_
), "_");
860 assert_eq
!(stringify_ty
!(mac
!(...)), "mac!(...)");
861 assert_eq
!(stringify_ty
!(mac
![...]), "mac![...]");
862 assert_eq
!(stringify_ty
!(mac
! { ... }
), "mac! { ... }");
867 // VisibilityKind::Public
868 assert_eq
!(stringify_vis
!(pub), "pub ");
870 // VisibilityKind::Restricted
871 assert_eq
!(stringify_vis
!(pub(crate)), "pub(crate) ");
872 assert_eq
!(stringify_vis
!(pub(self)), "pub(self) ");
873 assert_eq
!(stringify_vis
!(pub(super)), "pub(super) ");
874 assert_eq
!(stringify_vis
!(pub(in crate)), "pub(in crate) ");
875 assert_eq
!(stringify_vis
!(pub(in self)), "pub(in self) ");
876 assert_eq
!(stringify_vis
!(pub(in super)), "pub(in super) ");
877 assert_eq
!(stringify_vis
!(pub(in path
::to
)), "pub(in path::to) ");
878 assert_eq
!(stringify_vis
!(pub(in ::path
::to
)), "pub(in ::path::to) ");
879 assert_eq
!(stringify_vis
!(pub(in self::path
::to
)), "pub(in self::path::to) ");
880 assert_eq
!(stringify_vis
!(pub(in super::path
::to
)), "pub(in super::path::to) ");
882 // VisibilityKind::Inherited
883 // Directly calling `stringify_vis!()` does not work.
884 macro_rules
! stringify_inherited_vis
{
885 ($vis
:vis
struct) => {
889 assert_eq
!(stringify_inherited_vis
!(struct), "");