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