2021-12-23 15:02:31 -06:00
|
|
|
//@ run-pass
|
|
|
|
//@ edition:2021
|
|
|
|
//@ compile-flags: --test
|
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
#![allow(incomplete_features)]
|
2021-12-23 15:02:31 -06:00
|
|
|
#![feature(async_closure)]
|
2023-10-03 14:06:17 -05:00
|
|
|
#![feature(auto_traits)]
|
2022-08-17 06:52:47 -05:00
|
|
|
#![feature(box_patterns)]
|
2021-12-23 15:02:31 -06:00
|
|
|
#![feature(const_trait_impl)]
|
2023-10-19 16:46:28 -05:00
|
|
|
#![feature(coroutines)]
|
2023-10-23 23:04:46 -05:00
|
|
|
#![feature(decl_macro)]
|
2023-10-19 17:55:29 -05:00
|
|
|
#![feature(explicit_tail_calls)]
|
2023-11-30 01:02:05 -06:00
|
|
|
#![feature(if_let_guard)]
|
|
|
|
#![feature(let_chains)]
|
2021-12-23 15:02:31 -06:00
|
|
|
#![feature(more_qualified_paths)]
|
2023-11-21 19:30:43 -06:00
|
|
|
#![feature(never_patterns)]
|
2022-08-17 06:52:47 -05:00
|
|
|
#![feature(trait_alias)]
|
|
|
|
#![feature(try_blocks)]
|
|
|
|
#![feature(type_ascription)]
|
2023-10-19 17:55:29 -05:00
|
|
|
#![feature(yeet_expr)]
|
2021-12-23 15:02:31 -06:00
|
|
|
#![deny(unused_macros)]
|
|
|
|
|
2023-10-18 23:53:01 -05:00
|
|
|
// These macros force the use of AST pretty-printing by converting the input to
|
|
|
|
// a particular fragment specifier.
|
|
|
|
macro_rules! block { ($block:block) => { stringify!($block) }; }
|
|
|
|
macro_rules! expr { ($expr:expr) => { stringify!($expr) }; }
|
|
|
|
macro_rules! item { ($item:item) => { stringify!($item) }; }
|
|
|
|
macro_rules! meta { ($meta:meta) => { stringify!($meta) }; }
|
|
|
|
macro_rules! pat { ($pat:pat) => { stringify!($pat) }; }
|
|
|
|
macro_rules! path { ($path:path) => { stringify!($path) }; }
|
|
|
|
macro_rules! stmt { ($stmt:stmt) => { stringify!($stmt) }; }
|
|
|
|
macro_rules! ty { ($ty:ty) => { stringify!($ty) }; }
|
|
|
|
macro_rules! vis { ($vis:vis) => { stringify!($vis) }; }
|
|
|
|
|
|
|
|
macro_rules! c1 {
|
|
|
|
($frag:ident, [$($tt:tt)*], $s:literal) => {
|
2024-05-16 01:26:37 -05:00
|
|
|
// Prior to #125174:
|
|
|
|
// - the first of these two lines created a `TokenKind::Interpolated`
|
|
|
|
// that was printed by the AST pretty printer;
|
|
|
|
// - the second of these two lines created a token stream that was
|
|
|
|
// printed by the TokenStream pretty printer.
|
|
|
|
//
|
|
|
|
// Now they are both printed by the TokenStream pretty printer. But it
|
|
|
|
// doesn't hurt to keep both assertions to ensure this remains true.
|
|
|
|
//
|
|
|
|
// (This also explains the name `c1`. There used to be a `c2` macro for
|
|
|
|
// cases where the two pretty printers produced different output.)
|
2023-10-18 23:53:01 -05:00
|
|
|
assert_eq!($frag!($($tt)*), $s);
|
|
|
|
assert_eq!(stringify!($($tt)*), $s);
|
2021-12-23 15:02:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_block() {
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(block, [ {} ], "{}");
|
|
|
|
c1!(block, [ { true } ], "{ true }");
|
|
|
|
c1!(block, [ { return } ], "{ return }");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(block, [ { return; } ], "{ return; }");
|
|
|
|
c1!(block,
|
2023-10-18 23:53:01 -05:00
|
|
|
[ {
|
2021-12-23 15:02:31 -06:00
|
|
|
let _;
|
|
|
|
true
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
2023-08-07 20:43:44 -05:00
|
|
|
"{ let _; true }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on vanilla blocks.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_expr() {
|
|
|
|
// ExprKind::Array
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ [] ], "[]");
|
|
|
|
c1!(expr, [ [true] ], "[true]");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ [true,] ], "[true,]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ [true, true] ], "[true, true]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// ExprKind::ConstBlock
|
|
|
|
// FIXME: todo
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// ExprKind::Call
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ f() ], "f()");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ f::<u8>() ], "f::<u8>()");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ f :: < u8>( ) ], "f :: < u8>()");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ f::<1>() ], "f::<1>()");
|
|
|
|
c1!(expr, [ f::<'a, u8, 1>() ], "f::<'a, u8, 1>()");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ f(true) ], "f(true)");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ f(true,) ], "f(true,)");
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(expr, [ ()() ], "()()");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::MethodCall
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ x.f() ], "x.f()");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ x.f::<u8>() ], "x.f::<u8>()");
|
|
|
|
c1!(expr, [ x.collect::<Vec<_>>() ], "x.collect::<Vec<_>>()");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Tup
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ () ], "()");
|
|
|
|
c1!(expr, [ (true,) ], "(true,)");
|
|
|
|
c1!(expr, [ (true, false) ], "(true, false)");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ (true, false,) ], "(true, false,)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Binary
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ true || false ], "true || false");
|
|
|
|
c1!(expr, [ true || false && false ], "true || false && false");
|
2023-10-23 23:04:46 -05:00
|
|
|
c1!(expr, [ a < 1 && 2 < b && c > 3 && 4 > d ], "a < 1 && 2 < b && c > 3 && 4 > d");
|
2024-01-21 22:58:36 -06:00
|
|
|
c1!(expr, [ a & b & !c ], "a & b & !c");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ a + b * c - d + -1 * -2 - -3], "a + b * c - d + -1 * -2 - -3");
|
2024-01-21 22:58:36 -06:00
|
|
|
c1!(expr, [ x = !y ], "x = !y");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Unary
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ *expr ], "*expr");
|
|
|
|
c1!(expr, [ !expr ], "!expr");
|
|
|
|
c1!(expr, [ -expr ], "-expr");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Lit
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ 'x' ], "'x'");
|
|
|
|
c1!(expr, [ 1_000_i8 ], "1_000_i8");
|
|
|
|
c1!(expr, [ 1.00000000000000001 ], "1.00000000000000001");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Cast
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ expr as T ], "expr as T");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ expr as T<u8> ], "expr as T<u8>");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// ExprKind::Type: there is no syntax for type ascription.
|
|
|
|
|
|
|
|
// ExprKind::Let
|
|
|
|
c1!(expr, [ if let Some(a) = b { c } else { d } ], "if let Some(a) = b { c } else { d }");
|
2023-11-30 01:02:05 -06:00
|
|
|
c1!(expr, [ if let _ = true && false {} ], "if let _ = true && false {}");
|
|
|
|
c1!(expr, [ if let _ = (true && false) {} ], "if let _ = (true && false) {}");
|
2024-01-21 22:58:36 -06:00
|
|
|
c1!(expr,
|
2023-11-30 01:02:05 -06:00
|
|
|
[ match () { _ if let _ = Struct {} => {} } ],
|
2024-01-21 22:58:36 -06:00
|
|
|
"match () { _ if let _ = Struct {} => {} }"
|
2023-11-30 01:02:05 -06:00
|
|
|
);
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::If
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ if true {} ], "if true {}");
|
2024-01-21 22:58:36 -06:00
|
|
|
c1!(expr, [ if !true {} ], "if !true {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ if ::std::blah() { } else { } ], "if ::std::blah() {} else {}");
|
2023-10-23 23:04:46 -05:00
|
|
|
c1!(expr, [ if let true = true {} else {} ], "if let true = true {} else {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr,
|
|
|
|
[ if true {
|
2021-12-23 15:02:31 -06:00
|
|
|
} else if false {
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
|
|
|
"if true {} else if false {}"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr,
|
|
|
|
[ if true {
|
2021-12-23 15:02:31 -06:00
|
|
|
} else if false {
|
|
|
|
} else {
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
|
|
|
"if true {} else if false {} else {}"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr,
|
2023-10-18 23:53:01 -05:00
|
|
|
[ if true {
|
2021-12-23 15:02:31 -06:00
|
|
|
return;
|
|
|
|
} else if false {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
0
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
2023-08-07 20:43:44 -05:00
|
|
|
"if true { return; } else if false { 0 } else { 0 }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ExprKind::While
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ while true {} ], "while true {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ 'a: while true {} ], "'a: while true {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ while let true = true {} ], "while let true = true {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::ForLoop
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ for _ in x {} ], "for _ in x {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ 'a: for _ in x {} ], "'a: for _ in x {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Loop
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ loop {} ], "loop {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ 'a: loop {} ], "'a: loop {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Match
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ match self {} ], "match self {}");
|
|
|
|
c1!(expr,
|
|
|
|
[ match self {
|
2021-12-23 15:02:31 -06:00
|
|
|
Ok => 1,
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
|
|
|
"match self { Ok => 1, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr,
|
|
|
|
[ match self {
|
2021-12-23 15:02:31 -06:00
|
|
|
Ok => 1,
|
|
|
|
Err => 0,
|
2023-10-18 23:53:01 -05:00
|
|
|
} ],
|
|
|
|
"match self { Ok => 1, Err => 0, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ExprKind::Closure
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ || {} ], "|| {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ |x| {} ], "|x| {}");
|
|
|
|
c1!(expr, [ |x: u8| {} ], "|x: u8| {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ || () ], "|| ()");
|
|
|
|
c1!(expr, [ move || self ], "move || self");
|
|
|
|
c1!(expr, [ async || self ], "async || self");
|
|
|
|
c1!(expr, [ async move || self ], "async move || self");
|
|
|
|
c1!(expr, [ static || self ], "static || self");
|
|
|
|
c1!(expr, [ static move || self ], "static move || self");
|
2023-10-23 23:04:46 -05:00
|
|
|
c1!(expr, [ static async || self ], "static async || self");
|
|
|
|
c1!(expr, [ static async move || self ], "static async move || self");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ || -> u8 { self } ], "|| -> u8 { self }");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ 1 + || {} ], "1 + || {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Block
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ {} ], "{}");
|
|
|
|
c1!(expr, [ unsafe {} ], "unsafe {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ 'a: {} ], "'a: {}");
|
2023-10-23 23:04:46 -05:00
|
|
|
c1!(expr, [ #[attr] {} ], "#[attr] {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
{
|
|
|
|
#![attr]
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"{ #![attr] }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ExprKind::Async
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ async {} ], "async {}");
|
|
|
|
c1!(expr, [ async move {} ], "async move {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Await
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ expr.await ], "expr.await");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::TryBlock
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ try {} ], "try {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Assign
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ expr = true ], "expr = true");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::AssignOp
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ expr += true ], "expr += true");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Field
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ expr.field ], "expr.field");
|
|
|
|
c1!(expr, [ expr.0 ], "expr.0");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Index
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ expr[true] ], "expr[true]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Range
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ .. ], "..");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ ..hi ], "..hi");
|
|
|
|
c1!(expr, [ lo.. ], "lo..");
|
|
|
|
c1!(expr, [ lo..hi ], "lo..hi");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(expr, [ lo .. hi ], "lo .. hi");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ ..=hi ], "..=hi");
|
|
|
|
c1!(expr, [ lo..=hi ], "lo..=hi");
|
|
|
|
c1!(expr, [ -2..=-1 ], "-2..=-1");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// ExprKind::Underscore
|
|
|
|
// FIXME: todo
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// ExprKind::Path
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ thing ], "thing");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ m::thing ], "m::thing");
|
|
|
|
c1!(expr, [ self::thing ], "self::thing");
|
|
|
|
c1!(expr, [ crate::thing ], "crate::thing");
|
|
|
|
c1!(expr, [ Self::thing ], "Self::thing");
|
|
|
|
c1!(expr, [ <Self as T>::thing ], "<Self as T>::thing");
|
|
|
|
c1!(expr, [ Self::<'static> ], "Self::<'static>");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::AddrOf
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ &expr ], "&expr");
|
|
|
|
c1!(expr, [ &mut expr ], "&mut expr");
|
|
|
|
c1!(expr, [ &raw const expr ], "&raw const expr");
|
|
|
|
c1!(expr, [ &raw mut expr ], "&raw mut expr");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Break
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ break ], "break");
|
|
|
|
c1!(expr, [ break 'a ], "break 'a");
|
|
|
|
c1!(expr, [ break true ], "break true");
|
|
|
|
c1!(expr, [ break 'a true ], "break 'a true");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Continue
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ continue ], "continue");
|
|
|
|
c1!(expr, [ continue 'a ], "continue 'a");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Ret
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ return ], "return");
|
|
|
|
c1!(expr, [ return true ], "return true");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// ExprKind::InlineAsm: untestable because this test works pre-expansion.
|
|
|
|
|
|
|
|
// ExprKind::OffsetOf: untestable because this test works pre-expansion.
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// ExprKind::MacCall
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ mac!(...) ], "mac!(...)");
|
|
|
|
c1!(expr, [ mac![...] ], "mac![...]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ mac! { ... } ], "mac! { ... }");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Struct
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ Struct {} ], "Struct {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ <Struct as Trait>::Type {} ], "<Struct as Trait>::Type {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ Struct { .. } ], "Struct { .. }");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ Struct { ..base } ], "Struct { ..base }");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ Struct { x } ], "Struct { x }");
|
|
|
|
c1!(expr, [ Struct { x, .. } ], "Struct { x, .. }");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ Struct { x, ..base } ], "Struct { x, ..base }");
|
|
|
|
c1!(expr, [ Struct { x: true } ], "Struct { x: true }");
|
|
|
|
c1!(expr, [ Struct { x: true, .. } ], "Struct { x: true, .. }");
|
|
|
|
c1!(expr, [ Struct { x: true, ..base } ], "Struct { x: true, ..base }");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Repeat
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(expr, [ [(); 0] ], "[(); 0]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Paren
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ (expr) ], "(expr)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Try
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(expr, [ expr? ], "expr?");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ExprKind::Yield
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(expr, [ yield ], "yield");
|
|
|
|
c1!(expr, [ yield true ], "yield true");
|
2023-10-19 17:55:29 -05:00
|
|
|
|
|
|
|
// ExprKind::Yeet
|
|
|
|
c1!(expr, [ do yeet ], "do yeet");
|
|
|
|
c1!(expr, [ do yeet 0 ], "do yeet 0");
|
|
|
|
|
|
|
|
// ExprKind::Become
|
|
|
|
// FIXME: todo
|
|
|
|
|
|
|
|
// ExprKind::IncludedBytes
|
|
|
|
// FIXME: todo
|
|
|
|
|
|
|
|
// ExprKind::FormatArgs: untestable because this test works pre-expansion.
|
|
|
|
|
|
|
|
// ExprKind::Err: untestable.
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Ones involving attributes.
|
|
|
|
c1!(expr, [ #[aa] 1 ], "#[aa] 1");
|
|
|
|
c1!(expr, [ #[aa] #[bb] x ], "#[aa] #[bb] x");
|
2024-08-06 02:16:40 -05:00
|
|
|
c1!(expr, [ #[aa] 1 + 2 ], "#[aa] 1 + 2");
|
|
|
|
c1!(expr, [ #[aa] x + 2 ], "#[aa] x + 2");
|
|
|
|
c1!(expr, [ #[aa] 1 / #[bb] 2 ], "#[aa] 1 / #[bb] 2");
|
|
|
|
c1!(expr, [ #[aa] x / #[bb] 2 ], "#[aa] x / #[bb] 2");
|
2024-08-05 15:28:31 -05:00
|
|
|
c1!(expr, [ 1 << #[bb] 2 ], "1 << #[bb] 2");
|
|
|
|
c1!(expr, [ x << #[bb] 2 ], "x << #[bb] 2");
|
|
|
|
c1!(expr, [ #[aa] (1 + 2) ], "#[aa] (1 + 2)");
|
|
|
|
c1!(expr, [ #[aa] #[bb] (x + 2) ], "#[aa] #[bb] (x + 2)");
|
|
|
|
c1!(expr, [ #[aa] x[0].p ], "#[aa] x[0].p");
|
|
|
|
c1!(expr, [ #[aa] { #![bb] 0 } ], "#[aa] { #![bb] 0 }");
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_item() {
|
|
|
|
// ItemKind::ExternCrate
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ extern crate std; ], "extern crate std;");
|
|
|
|
c1!(item, [ pub extern crate self as std; ], "pub extern crate self as std;");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::Use
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item, [ pub use crate::{a, b::c}; ], "pub use crate::{ a, b::c };"); // FIXME
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ pub use A::*; ], "pub use A::*;");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::Static
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(item, [ pub static S: () = {}; ], "pub static S: () = {};");
|
|
|
|
c1!(item, [ static mut S: () = {}; ], "static mut S: () = {};");
|
|
|
|
c1!(item, [ static S: (); ], "static S: ();");
|
|
|
|
c1!(item, [ static mut S: (); ], "static mut S: ();");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::Const
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(item, [ pub const S: () = {}; ], "pub const S: () = {};");
|
|
|
|
c1!(item, [ const S: (); ], "const S: ();");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::Fn
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item,
|
|
|
|
[ pub default const async unsafe extern "C" fn f() {} ],
|
|
|
|
"pub default const async unsafe extern \"C\" fn f() {}"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ fn g<T>(t: Vec<Vec<Vec<T>>>) {} ], "fn g<T>(t: Vec<Vec<Vec<T>>>) {}");
|
|
|
|
c1!(item,
|
2023-10-23 23:04:46 -05:00
|
|
|
[ fn h<'a>(t: &'a Vec<Cell<dyn D>>) {} ],
|
2023-08-07 20:43:44 -05:00
|
|
|
"fn h<'a>(t: &'a Vec<Cell<dyn D>>) {}"
|
2023-10-23 23:04:46 -05:00
|
|
|
);
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::Mod
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ pub mod m; ], "pub mod m;");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ mod m {} ], "mod m {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ unsafe mod m; ], "unsafe mod m;");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ unsafe mod m {} ], "unsafe mod m {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::ForeignMod
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ extern "C" {} ], "extern \"C\" {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item, [ pub extern "C" {} ], "pub extern \"C\" {}");
|
2023-10-23 23:04:46 -05:00
|
|
|
c1!(item, [ unsafe extern "C++" {} ], "unsafe extern \"C++\" {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// ItemKind::GlobalAsm: untestable because this test works pre-expansion.
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// ItemKind::TyAlias
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
pub default type Type<'a>: Bound
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= T;
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"pub default type Type<'a>: Bound where Self: 'a, = T;"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::Enum
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ pub enum Void {} ], "pub enum Void {}");
|
|
|
|
c1!(item,
|
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
enum Empty {
|
|
|
|
Unit,
|
|
|
|
Tuple(),
|
|
|
|
Struct {},
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
|
|
|
"enum Empty { Unit, Tuple(), Struct {}, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 18:53:30 -06:00
|
|
|
enum Enum<T>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
2021-12-23 15:02:31 -06:00
|
|
|
Unit,
|
|
|
|
Tuple(T),
|
|
|
|
Struct { t: T },
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"enum Enum<T> where T: 'a, { Unit, Tuple(T), Struct { t: T }, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::Struct
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ pub struct Unit; ], "pub struct Unit;");
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(item, [ struct Tuple(); ], "struct Tuple();");
|
|
|
|
c1!(item, [ struct Tuple(T); ], "struct Tuple(T);");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ struct Struct {} ], "struct Struct {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
struct Struct<T>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
{
|
|
|
|
t: T,
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"struct Struct<T> where T: 'a, { t: T, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::Union
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ pub union Union {} ], "pub union Union {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
union Union<T> where T: 'a {
|
|
|
|
t: T,
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"union Union<T> where T: 'a { t: T, }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::Trait
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ pub unsafe auto trait Send {} ], "pub unsafe auto trait Send {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
trait Trait<'a>: Sized
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
{
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-08-07 20:43:44 -05:00
|
|
|
"trait Trait<'a>: Sized where Self: 'a, {}"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::TraitAlias
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item,
|
2023-10-18 23:53:01 -05:00
|
|
|
[ pub trait Trait<T> = Sized where T: 'a; ],
|
2023-08-07 20:43:44 -05:00
|
|
|
"pub trait Trait<T> = Sized where T: 'a;"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// ItemKind::Impl
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ pub impl Struct {} ], "pub impl Struct {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ impl<T> Struct<T> {} ], "impl<T> Struct<T> {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ pub impl Trait for Struct {} ], "pub impl Trait for Struct {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(item, [ impl<T> const Trait for T {} ], "impl<T> const Trait for T {}");
|
|
|
|
c1!(item, [ impl ~const Struct {} ], "impl ~const Struct {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::MacCall
|
2023-10-11 23:36:14 -05:00
|
|
|
c1!(item, [ mac!(...); ], "mac!(...);");
|
|
|
|
c1!(item, [ mac![...]; ], "mac![...];");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item, [ mac! { ... } ], "mac! { ... }");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// ItemKind::MacroDef
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(item,
|
|
|
|
[
|
2021-12-23 15:02:31 -06:00
|
|
|
macro_rules! stringify {
|
|
|
|
() => {};
|
|
|
|
}
|
2023-10-18 23:53:01 -05:00
|
|
|
],
|
2023-10-11 23:36:14 -05:00
|
|
|
"macro_rules! stringify { () => {}; }"
|
2021-12-23 15:02:31 -06:00
|
|
|
);
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(item, [ pub macro stringify() {} ], "pub macro stringify() {}");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Ones involving attributes.
|
|
|
|
c1!(item, [ #[aa] mod m; ], "#[aa] mod m;");
|
|
|
|
c1!(item, [ mod m { #![bb] } ], "mod m { #![bb] }");
|
|
|
|
c1!(item, [ #[aa] mod m { #![bb] } ], "#[aa] mod m { #![bb] }");
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_meta() {
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(meta, [ k ], "k");
|
|
|
|
c1!(meta, [ k = "v" ], "k = \"v\"");
|
|
|
|
c1!(meta, [ list(k1, k2 = "v") ], "list(k1, k2 = \"v\")");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(meta, [ serde::k ], "serde::k");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on metas.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pat() {
|
|
|
|
// PatKind::Wild
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ _ ], "_");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Ident
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ _x ], "_x");
|
|
|
|
c1!(pat, [ ref _x ], "ref _x");
|
|
|
|
c1!(pat, [ mut _x ], "mut _x");
|
|
|
|
c1!(pat, [ ref mut _x ], "ref mut _x");
|
|
|
|
c1!(pat, [ ref mut _x @ _ ], "ref mut _x @ _");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Struct
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ Struct {} ], "Struct {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ Struct::<u8> {} ], "Struct::<u8> {}");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(pat, [ Struct ::< u8 > {} ], "Struct ::< u8 > {}");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ Struct::<'static> {} ], "Struct::<'static> {}");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ Struct { x } ], "Struct { x }");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ Struct { x: _x } ], "Struct { x: _x }");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ Struct { .. } ], "Struct { .. }");
|
|
|
|
c1!(pat, [ Struct { x, .. } ], "Struct { x, .. }");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ Struct { x: _x, .. } ], "Struct { x: _x, .. }");
|
|
|
|
c1!(pat, [ <Struct as Trait>::Type {} ], "<Struct as Trait>::Type {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::TupleStruct
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ Tuple() ], "Tuple()");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ Tuple::<u8>() ], "Tuple::<u8>()");
|
|
|
|
c1!(pat, [ Tuple::<'static>() ], "Tuple::<'static>()");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ Tuple(x) ], "Tuple(x)");
|
|
|
|
c1!(pat, [ Tuple(..) ], "Tuple(..)");
|
|
|
|
c1!(pat, [ Tuple(x, ..) ], "Tuple(x, ..)");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ <Struct as Trait>::Type() ], "<Struct as Trait>::Type()");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Or
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ true | false ], "true | false");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(pat, [ | true ], "| true");
|
|
|
|
c1!(pat, [ |true| false ], "|true| false");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Path
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ crate::Path ], "crate::Path");
|
|
|
|
c1!(pat, [ Path::<u8> ], "Path::<u8>");
|
|
|
|
c1!(pat, [ Path::<'static> ], "Path::<'static>");
|
|
|
|
c1!(pat, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Tuple
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ () ], "()");
|
|
|
|
c1!(pat, [ (true,) ], "(true,)");
|
|
|
|
c1!(pat, [ (true, false) ], "(true, false)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Box
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ box pat ], "box pat");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Ref
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ &pat ], "&pat");
|
|
|
|
c1!(pat, [ &mut pat ], "&mut pat");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Lit
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ 1_000_i8 ], "1_000_i8");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Range
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ ..1 ], "..1");
|
|
|
|
c1!(pat, [ 0.. ], "0..");
|
|
|
|
c1!(pat, [ 0..1 ], "0..1");
|
|
|
|
c1!(pat, [ 0..=1 ], "0..=1");
|
|
|
|
c1!(pat, [ -2..=-1 ], "-2..=-1");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Slice
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ [] ], "[]");
|
|
|
|
c1!(pat, [ [true] ], "[true]");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(pat, [ [true,] ], "[true,]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ [true, false] ], "[true, false]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::Rest
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ .. ], "..");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-11-21 19:30:43 -06:00
|
|
|
// PatKind::Never
|
|
|
|
c1!(pat, [ Some(!) ], "Some(!)");
|
|
|
|
c1!(pat, [ None | Some(!) ], "None | Some(!)");
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// PatKind::Paren
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ (pat) ], "(pat)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// PatKind::MacCall
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(pat, [ mac!(...) ], "mac!(...)");
|
|
|
|
c1!(pat, [ mac![...] ], "mac![...]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(pat, [ mac! { ... } ], "mac! { ... }");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on patterns.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path() {
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(path, [ thing ], "thing");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(path, [ m::thing ], "m::thing");
|
|
|
|
c1!(path, [ self::thing ], "self::thing");
|
|
|
|
c1!(path, [ crate::thing ], "crate::thing");
|
|
|
|
c1!(path, [ Self::thing ], "Self::thing");
|
|
|
|
c1!(path, [ Self<'static> ], "Self<'static>");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(path, [ Self::<'static> ], "Self::<'static>");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(path, [ Self() ], "Self()");
|
|
|
|
c1!(path, [ Self() -> () ], "Self() -> ()");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on paths.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stmt() {
|
|
|
|
// StmtKind::Local
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(stmt, [ let _ ], "let _");
|
|
|
|
c1!(stmt, [ let x = true ], "let x = true");
|
|
|
|
c1!(stmt, [ let x: bool = true ], "let x: bool = true");
|
|
|
|
c1!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2)");
|
|
|
|
c1!(stmt, [ let (a, b): (u32, u32) = (1, 2) ], "let (a, b): (u32, u32) = (1, 2)");
|
|
|
|
c1!(stmt, [ let _ = f() else { return; } ], "let _ = f() else { return; }");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// StmtKind::Item
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(stmt, [ struct S; ], "struct S;");
|
2023-10-19 17:55:29 -05:00
|
|
|
c1!(stmt, [ struct S {} ], "struct S {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// StmtKind::Expr
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(stmt, [ loop {} ], "loop {}");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// StmtKind::Semi
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(stmt, [ 1 + 1 ], "1 + 1");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// StmtKind::Empty
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(stmt, [ ; ], ";");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// StmtKind::MacCall
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(stmt, [ mac!(...) ], "mac!(...)");
|
|
|
|
c1!(stmt, [ mac![...] ], "mac![...]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(stmt, [ mac! { ... } ], "mac! { ... }");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Ones involving attributes.
|
|
|
|
c1!(stmt, [ #[aa] 1 ], "#[aa] 1");
|
|
|
|
c1!(stmt, [ #[aa] #[bb] x ], "#[aa] #[bb] x");
|
2024-08-06 02:16:40 -05:00
|
|
|
c1!(stmt, [ #[aa] 1 as u32 ], "#[aa] 1 as u32");
|
|
|
|
c1!(stmt, [ #[aa] x as u32 ], "#[aa] x as u32");
|
|
|
|
c1!(stmt, [ #[aa] 1 .. #[bb] 2 ], "#[aa] 1 .. #[bb] 2");
|
|
|
|
c1!(stmt, [ #[aa] x .. #[bb] 2 ], "#[aa] x .. #[bb] 2");
|
2024-08-05 15:28:31 -05:00
|
|
|
c1!(stmt, [ 1 || #[bb] 2 ], "1 || #[bb] 2");
|
|
|
|
c1!(stmt, [ x || #[bb] 2 ], "x || #[bb] 2");
|
|
|
|
c1!(stmt, [ #[aa] (1 + 2) ], "#[aa] (1 + 2)");
|
|
|
|
c1!(stmt, [ #[aa] #[bb] (x + 2) ], "#[aa] #[bb] (x + 2)");
|
|
|
|
c1!(stmt, [ #[aa] x[0].p ], "#[aa] x[0].p");
|
|
|
|
c1!(stmt, [ #[aa] { #![bb] 0 } ], "#[aa] { #![bb] 0 }");
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ty() {
|
|
|
|
// TyKind::Slice
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ [T] ], "[T]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::Array
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ [T; 0] ], "[T; 0]");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::Ptr
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ *const T ], "*const T");
|
|
|
|
c1!(ty, [ *mut T ], "*mut T");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2022-12-28 11:06:11 -06:00
|
|
|
// TyKind::Ref
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ &T ], "&T");
|
|
|
|
c1!(ty, [ &mut T ], "&mut T");
|
|
|
|
c1!(ty, [ &'a T ], "&'a T");
|
|
|
|
c1!(ty, [ &'a mut [T] ], "&'a mut [T]");
|
|
|
|
c1!(ty, [ &A<B<C<D<E>>>> ], "&A<B<C<D<E>>>>");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(ty, [ &A<B<C<D<E> > > > ], "&A<B<C<D<E> > > >");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::BareFn
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ fn() ], "fn()");
|
|
|
|
c1!(ty, [ fn() -> () ], "fn() -> ()");
|
|
|
|
c1!(ty, [ fn(u8) ], "fn(u8)");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ fn(x: u8) ], "fn(x: u8)");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(ty, [ for<> fn() ], "for<> fn()");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ for<'a> fn() ], "for<'a> fn()");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::Never
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ ! ], "!");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::Tup
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ () ], "()");
|
|
|
|
c1!(ty, [ (T,) ], "(T,)");
|
|
|
|
c1!(ty, [ (T, U) ], "(T, U)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// TyKind::AnonStruct: untestable in isolation.
|
|
|
|
|
|
|
|
// TyKind::AnonUnion: untestable in isolation.
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// TyKind::Path
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ T ], "T");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ Ref<'a> ], "Ref<'a>");
|
|
|
|
c1!(ty, [ PhantomData<T> ], "PhantomData<T>");
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(ty, [ PhantomData::<T> ], "PhantomData::<T>");
|
2024-01-21 22:58:36 -06:00
|
|
|
c1!(ty, [ Fn() -> ! ], "Fn() -> !");
|
|
|
|
c1!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::TraitObject
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ dyn Send ], "dyn Send");
|
|
|
|
c1!(ty, [ dyn Send + 'a ], "dyn Send + 'a");
|
|
|
|
c1!(ty, [ dyn 'a + Send ], "dyn 'a + Send");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ dyn ?Sized ], "dyn ?Sized");
|
|
|
|
c1!(ty, [ dyn ~const Clone ], "dyn ~const Clone");
|
|
|
|
c1!(ty, [ dyn for<'a> Send ], "dyn for<'a> Send");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::ImplTrait
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ impl Send ], "impl Send");
|
|
|
|
c1!(ty, [ impl Send + 'a ], "impl Send + 'a");
|
|
|
|
c1!(ty, [ impl 'a + Send ], "impl 'a + Send");
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ impl ?Sized ], "impl ?Sized");
|
|
|
|
c1!(ty, [ impl ~const Clone ], "impl ~const Clone");
|
|
|
|
c1!(ty, [ impl for<'a> Send ], "impl for<'a> Send");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::Paren
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ (T) ], "(T)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// TyKind::Typeof: unused for now.
|
|
|
|
|
2021-12-23 15:02:31 -06:00
|
|
|
// TyKind::Infer
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ _ ], "_");
|
2023-10-23 23:04:46 -05:00
|
|
|
|
2023-10-19 17:55:29 -05:00
|
|
|
// TyKind::ImplicitSelf: there is no syntax for this.
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// TyKind::MacCall
|
2023-08-07 20:43:44 -05:00
|
|
|
c1!(ty, [ mac!(...) ], "mac!(...)");
|
|
|
|
c1!(ty, [ mac![...] ], "mac![...]");
|
2023-10-18 23:53:01 -05:00
|
|
|
c1!(ty, [ mac! { ... } ], "mac! { ... }");
|
2023-10-19 17:55:29 -05:00
|
|
|
|
|
|
|
// TyKind::Err: untestable.
|
|
|
|
|
|
|
|
// TyKind::CVarArgs
|
|
|
|
// FIXME: todo
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on types.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vis() {
|
|
|
|
// VisibilityKind::Public
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(vis, [ pub ], "pub");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// VisibilityKind::Restricted
|
2024-05-16 01:26:37 -05:00
|
|
|
c1!(vis, [ pub(crate) ], "pub(crate)");
|
|
|
|
c1!(vis, [ pub(self) ], "pub(self)");
|
|
|
|
c1!(vis, [ pub(super) ], "pub(super)");
|
|
|
|
c1!(vis, [ pub(in crate) ], "pub(in crate)");
|
|
|
|
c1!(vis, [ pub(in self) ], "pub(in self)");
|
|
|
|
c1!(vis, [ pub(in super) ], "pub(in super)");
|
|
|
|
c1!(vis, [ pub(in path::to) ], "pub(in path::to)");
|
|
|
|
c1!(vis, [ pub(in ::path::to) ], "pub(in ::path::to)");
|
|
|
|
c1!(vis, [ pub(in self::path::to) ], "pub(in self::path::to)");
|
|
|
|
c1!(vis, [ pub(in super::path::to) ], "pub(in super::path::to)");
|
2021-12-23 15:02:31 -06:00
|
|
|
|
|
|
|
// VisibilityKind::Inherited
|
2023-10-18 23:53:01 -05:00
|
|
|
// This one is different because directly calling `vis!` does not work.
|
|
|
|
macro_rules! inherited_vis { ($vis:vis struct) => { vis!($vis) }; }
|
|
|
|
assert_eq!(inherited_vis!(struct), "");
|
|
|
|
assert_eq!(stringify!(), "");
|
2024-08-05 15:28:31 -05:00
|
|
|
|
|
|
|
// Attributes are not allowed on visibilities.
|
2021-12-23 15:02:31 -06:00
|
|
|
}
|
2023-10-31 16:35:13 -05:00
|
|
|
|
|
|
|
macro_rules! p {
|
|
|
|
([$($tt:tt)*], $s:literal) => {
|
|
|
|
assert_eq!(stringify!($($tt)*), $s);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_punct() {
|
2023-08-07 20:43:44 -05:00
|
|
|
// For all these cases, we should preserve spaces between the tokens.
|
2023-10-31 16:35:13 -05:00
|
|
|
// Otherwise, any old proc macro that parses pretty-printed code might glue
|
|
|
|
// together tokens that shouldn't be glued.
|
|
|
|
p!([ = = < < <= <= == == != != >= >= > > ], "= = < < <= <= == == != != >= >= > >");
|
2024-01-21 22:58:36 -06:00
|
|
|
p!([ && && & & || || | | ! ! ], "&& && & & || || | | ! !");
|
2023-10-31 16:35:13 -05:00
|
|
|
p!([ ~ ~ @ @ # # ], "~ ~ @ @ # #");
|
2024-01-21 22:58:36 -06:00
|
|
|
p!([ . . .. .. ... ... ..= ..=], ". . .. .. ... ... ..= ..=");
|
|
|
|
p!([ , , ; ; : : :: :: ], ", , ; ; : : :: ::");
|
2023-10-31 16:35:13 -05:00
|
|
|
p!([ -> -> <- <- => =>], "-> -> <- <- => =>");
|
2024-01-21 22:58:36 -06:00
|
|
|
p!([ $ $ ? ? ' ' ], "$ $ ? ? ' '");
|
2023-10-31 16:35:13 -05:00
|
|
|
p!([ + + += += - - -= -= * * *= *= / / /= /= ], "+ + += += - - -= -= * * *= *= / / /= /=");
|
|
|
|
p!([ % % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>= ],
|
|
|
|
"% % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>=");
|
2024-01-21 22:58:36 -06:00
|
|
|
p!([ +! ?= |> >>@ --> <-- $$ =====> ], "+! ?= |> >>@ --> <-- $$ =====>");
|
|
|
|
p!([ ,; ;, ** @@ $+$ >< <> ?? +== ], ",; ;, ** @@ $+$ >< <> ?? +==");
|
2023-08-07 20:43:44 -05:00
|
|
|
p!([ :#!@|$=&*,+;*~? ], ":#!@|$=&*,+;*~?");
|
2023-10-31 16:35:13 -05:00
|
|
|
}
|