]> git.lizzy.rs Git - rust.git/blob - src/test/ui/proc-macro/expand-expr.rs
Rollup merge of #104158 - Ayush1325:executable, r=Mark-Simulacrum
[rust.git] / src / test / ui / proc-macro / expand-expr.rs
1 // aux-build:expand-expr.rs
2 #![feature(concat_bytes)]
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 expand_expr_is!(
27     b"contents: Included file contents\n",
28     concat_bytes!(b"contents: ", include_bytes!("auxiliary/included-file.txt"))
29 );
30
31 // Correct value is checked for multiple sources.
32 check_expand_expr_file!(file!());
33
34 expand_expr_is!("hello", stringify!(hello));
35 expand_expr_is!("10 + 20", stringify!(10 + 20));
36
37 macro_rules! echo_tts {
38     ($($t:tt)*) => { $($t)* };  //~ ERROR: expected expression, found `$`
39 }
40
41 macro_rules! echo_lit {
42     ($l:literal) => {
43         $l
44     };
45 }
46
47 macro_rules! echo_expr {
48     ($e:expr) => {
49         $e
50     };
51 }
52
53 macro_rules! simple_lit {
54     ($l:literal) => {
55         expand_expr_is!($l, $l);
56         expand_expr_is!($l, echo_lit!($l));
57         expand_expr_is!($l, echo_expr!($l));
58         expand_expr_is!($l, echo_tts!($l));
59         expand_expr_is!($l, echo_pm!($l));
60         const _: () = {
61             macro_rules! mac {
62                 () => {
63                     $l
64                 };
65             }
66             expand_expr_is!($l, mac!());
67             expand_expr_is!($l, echo_expr!(mac!()));
68             expand_expr_is!($l, echo_tts!(mac!()));
69             expand_expr_is!($l, echo_pm!(mac!()));
70         };
71     };
72 }
73
74 simple_lit!("Hello, World");
75 simple_lit!('c');
76 simple_lit!(b'c');
77 simple_lit!(10);
78 simple_lit!(10.0);
79 simple_lit!(10.0f64);
80 simple_lit!(-3.14159);
81 simple_lit!(-3.5e10);
82 simple_lit!(0xFEED);
83 simple_lit!(-0xFEED);
84 simple_lit!(0b0100);
85 simple_lit!(-0b0100);
86 simple_lit!("string");
87 simple_lit!(r##"raw string"##);
88 simple_lit!(b"byte string");
89 simple_lit!(br##"raw byte string"##);
90 simple_lit!(true);
91 simple_lit!(false);
92
93 // Ensure char escapes aren't normalized by expansion
94 simple_lit!("\u{0}");
95 simple_lit!("\0");
96 simple_lit!("\x00");
97 simple_lit!('\u{0}');
98 simple_lit!('\0');
99 simple_lit!('\x00');
100 simple_lit!(b"\x00");
101 simple_lit!(b"\0");
102 simple_lit!(b'\x00');
103 simple_lit!(b'\0');
104
105 // Extra tokens after the string literal aren't ignored
106 expand_expr_fail!("string"; hello); //~ ERROR: expected one of `.`, `?`, or an operator, found `;`
107
108 // Invalid expressions produce errors in addition to returning `Err(())`.
109 expand_expr_fail!($); //~ ERROR: expected expression, found `$`
110 expand_expr_fail!(echo_tts!($));
111 expand_expr_fail!(echo_pm!($)); //~ ERROR: expected expression, found `$`
112
113 // We get errors reported and recover during macro expansion if the macro
114 // doesn't produce a valid expression.
115 expand_expr_is!("string", echo_tts!("string"; hello)); //~ ERROR: macro expansion ignores token `hello` and any following
116 expand_expr_is!("string", echo_pm!("string"; hello)); //~ ERROR: macro expansion ignores token `;` and any following
117
118 // For now, fail if a non-literal expression is expanded.
119 expand_expr_fail!(arbitrary_expression() + "etc");
120 expand_expr_fail!(echo_tts!(arbitrary_expression() + "etc"));
121 expand_expr_fail!(echo_expr!(arbitrary_expression() + "etc"));
122 expand_expr_fail!(echo_pm!(arbitrary_expression() + "etc"));
123
124 const _: u32 = recursive_expand!(); //~ ERROR: recursion limit reached while expanding `recursive_expand!`
125
126 fn main() {}