1 // aux-build:expand-expr.rs
2 #![feature(concat_bytes)]
3 extern crate expand_expr;
6 check_expand_expr_file, echo_pm, expand_expr_fail, expand_expr_is, recursive_expand,
9 // Check builtin macros can be expanded.
11 expand_expr_is!(11u32, line!());
12 expand_expr_is!(24u32, column!());
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"##));
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"));
22 "contents: Included file contents\n",
23 concat!("contents: ", include_str!("auxiliary/included-file.txt"))
27 b"contents: Included file contents\n",
28 concat_bytes!(b"contents: ", include_bytes!("auxiliary/included-file.txt"))
31 // Correct value is checked for multiple sources.
32 check_expand_expr_file!(file!());
34 expand_expr_is!("hello", stringify!(hello));
35 expand_expr_is!("10 + 20", stringify!(10 + 20));
37 macro_rules! echo_tts {
38 ($($t:tt)*) => { $($t)* }; //~ ERROR: expected expression, found `$`
41 macro_rules! echo_lit {
47 macro_rules! echo_expr {
53 macro_rules! simple_lit {
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));
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!()));
74 simple_lit!("Hello, World");
80 simple_lit!(-3.14159);
86 simple_lit!("string");
87 simple_lit!(r##"raw string"##);
88 simple_lit!(b"byte string");
89 simple_lit!(br##"raw byte string"##);
93 // Ensure char escapes aren't normalized by expansion
100 simple_lit!(b"\x00");
102 simple_lit!(b'\x00');
105 // Extra tokens after the string literal aren't ignored
106 expand_expr_fail!("string"; hello); //~ ERROR: expected one of `.`, `?`, or an operator, found `;`
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 `$`
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
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"));
124 const _: u32 = recursive_expand!(); //~ ERROR: recursion limit reached while expanding `recursive_expand!`