]> git.lizzy.rs Git - rust.git/blob - src/test/ui/proc-macro/expand-expr.rs
Merge commit 'f51aade56f93175dde89177a92e3669ebd8e7592' into clippyup
[rust.git] / src / test / ui / proc-macro / expand-expr.rs
1 // aux-build:expand-expr.rs
2
3 extern crate expand_expr;
4
5 use expand_expr::{
6     check_expand_expr_file, echo_pm, expand_expr_fail, expand_expr_is, recursive_expand,
7 };
8
9 // Check builtin macros can be expanded.
10
11 expand_expr_is!(11u32, line!());
12 expand_expr_is!(24u32, column!());
13
14 expand_expr_is!("Hello, World!", concat!("Hello, ", "World", "!"));
15 expand_expr_is!("int10floats5.3booltrue", concat!("int", 10, "floats", 5.3, "bool", true));
16 expand_expr_is!("Hello", concat!(r##"Hello"##));
17
18 expand_expr_is!("Included file contents\n", include_str!("auxiliary/included-file.txt"));
19 expand_expr_is!(b"Included file contents\n", include_bytes!("auxiliary/included-file.txt"));
20
21 expand_expr_is!(
22     "contents: Included file contents\n",
23     concat!("contents: ", include_str!("auxiliary/included-file.txt"))
24 );
25
26 // Correct value is checked for multiple sources.
27 check_expand_expr_file!(file!());
28
29 expand_expr_is!("hello", stringify!(hello));
30 expand_expr_is!("10 + 20", stringify!(10 + 20));
31
32 macro_rules! echo_tts {
33     ($($t:tt)*) => { $($t)* };  //~ ERROR: expected expression, found `$`
34 }
35
36 macro_rules! echo_lit {
37     ($l:literal) => {
38         $l
39     };
40 }
41
42 macro_rules! echo_expr {
43     ($e:expr) => {
44         $e
45     };
46 }
47
48 macro_rules! simple_lit {
49     ($l:literal) => {
50         expand_expr_is!($l, $l);
51         expand_expr_is!($l, echo_lit!($l));
52         expand_expr_is!($l, echo_expr!($l));
53         expand_expr_is!($l, echo_tts!($l));
54         expand_expr_is!($l, echo_pm!($l));
55         const _: () = {
56             macro_rules! mac {
57                 () => {
58                     $l
59                 };
60             }
61             expand_expr_is!($l, mac!());
62             expand_expr_is!($l, echo_expr!(mac!()));
63             expand_expr_is!($l, echo_tts!(mac!()));
64             expand_expr_is!($l, echo_pm!(mac!()));
65         };
66     };
67 }
68
69 simple_lit!("Hello, World");
70 simple_lit!('c');
71 simple_lit!(b'c');
72 simple_lit!(10);
73 simple_lit!(10.0);
74 simple_lit!(10.0f64);
75 simple_lit!(-3.14159);
76 simple_lit!(-3.5e10);
77 simple_lit!(0xFEED);
78 simple_lit!(-0xFEED);
79 simple_lit!(0b0100);
80 simple_lit!(-0b0100);
81 simple_lit!("string");
82 simple_lit!(r##"raw string"##);
83 simple_lit!(b"byte string");
84 simple_lit!(br##"raw byte string"##);
85 simple_lit!(true);
86 simple_lit!(false);
87
88 // Ensure char escapes aren't normalized by expansion
89 simple_lit!("\u{0}");
90 simple_lit!("\0");
91 simple_lit!("\x00");
92 simple_lit!('\u{0}');
93 simple_lit!('\0');
94 simple_lit!('\x00');
95 simple_lit!(b"\x00");
96 simple_lit!(b"\0");
97 simple_lit!(b'\x00');
98 simple_lit!(b'\0');
99
100 // Extra tokens after the string literal aren't ignored
101 expand_expr_fail!("string"; hello); //~ ERROR: expected one of `.`, `?`, or an operator, found `;`
102
103 // Invalid expressions produce errors in addition to returning `Err(())`.
104 expand_expr_fail!($); //~ ERROR: expected expression, found `$`
105 expand_expr_fail!(echo_tts!($));
106 expand_expr_fail!(echo_pm!($)); //~ ERROR: expected expression, found `$`
107
108 // We get errors reported and recover during macro expansion if the macro
109 // doesn't produce a valid expression.
110 expand_expr_is!("string", echo_tts!("string"; hello)); //~ ERROR: macro expansion ignores token `hello` and any following
111 expand_expr_is!("string", echo_pm!("string"; hello)); //~ ERROR: macro expansion ignores token `;` and any following
112
113 // For now, fail if a non-literal expression is expanded.
114 expand_expr_fail!(arbitrary_expression() + "etc");
115 expand_expr_fail!(echo_tts!(arbitrary_expression() + "etc"));
116 expand_expr_fail!(echo_expr!(arbitrary_expression() + "etc"));
117 expand_expr_fail!(echo_pm!(arbitrary_expression() + "etc"));
118
119 const _: u32 = recursive_expand!(); //~ ERROR: recursion limit reached while expanding `recursive_expand!`
120
121 fn main() {}