1 // aux-build:expand-expr.rs
2 // no-remap-src-base: check_expand_expr_file!() fails when enabled.
4 #![feature(concat_bytes)]
5 extern crate expand_expr;
8 check_expand_expr_file, echo_pm, expand_expr_fail, expand_expr_is, recursive_expand,
11 // Check builtin macros can be expanded.
13 expand_expr_is!(13u32, line!());
14 expand_expr_is!(24u32, column!());
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"##));
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"));
24 "contents: Included file contents\n",
25 concat!("contents: ", include_str!("auxiliary/included-file.txt"))
29 b"contents: Included file contents\n",
30 concat_bytes!(b"contents: ", include_bytes!("auxiliary/included-file.txt"))
33 // Correct value is checked for multiple sources.
34 check_expand_expr_file!(file!());
36 expand_expr_is!("hello", stringify!(hello));
37 expand_expr_is!("10 + 20", stringify!(10 + 20));
39 macro_rules! echo_tts {
40 ($($t:tt)*) => { $($t)* }; //~ ERROR: expected expression, found `$`
43 macro_rules! echo_lit {
49 macro_rules! echo_expr {
55 macro_rules! simple_lit {
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));
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!()));
76 simple_lit!("Hello, World");
82 simple_lit!(-3.14159);
88 simple_lit!("string");
89 simple_lit!(r##"raw string"##);
90 simple_lit!(b"byte string");
91 simple_lit!(br##"raw byte string"##);
95 // Ensure char escapes aren't normalized by expansion
102 simple_lit!(b"\x00");
104 simple_lit!(b'\x00');
107 // Extra tokens after the string literal aren't ignored
108 expand_expr_fail!("string"; hello); //~ ERROR: expected one of `.`, `?`, or an operator, found `;`
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 `$`
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
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"));
126 const _: u32 = recursive_expand!(); //~ ERROR: recursion limit reached while expanding `recursive_expand!`
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"),