1 // aux-build:expand-expr.rs
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"))
26 // Correct value is checked for multiple sources.
27 check_expand_expr_file!(file!());
29 expand_expr_is!("hello", stringify!(hello));
30 expand_expr_is!("10 + 20", stringify!(10 + 20));
32 macro_rules! echo_tts {
33 ($($t:tt)*) => { $($t)* }; //~ ERROR: expected expression, found `$`
36 macro_rules! echo_lit {
42 macro_rules! echo_expr {
48 macro_rules! simple_lit {
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));
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!()));
69 simple_lit!("Hello, World");
75 simple_lit!(-3.14159);
81 simple_lit!("string");
82 simple_lit!(r##"raw string"##);
83 simple_lit!(b"byte string");
84 simple_lit!(br##"raw byte string"##);
88 // Ensure char escapes aren't normalized by expansion
100 // Extra tokens after the string literal aren't ignored
101 expand_expr_fail!("string"; hello); //~ ERROR: expected one of `.`, `?`, or an operator, found `;`
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 `$`
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
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"));
119 const _: u32 = recursive_expand!(); //~ ERROR: recursion limit reached while expanding `recursive_expand!`