]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/macro_expansion_tests/mbe/matching.rs
Merge #11151
[rust.git] / crates / hir_def / src / macro_expansion_tests / mbe / matching.rs
1 //! Test that `$var:expr` captures function correctly.
2
3 use expect_test::expect;
4
5 use crate::macro_expansion_tests::check;
6
7 #[test]
8 fn unary_minus_is_a_literal() {
9     check(
10         r#"
11 macro_rules! m { ($x:literal) => (literal!();); ($x:tt) => (not_a_literal!();); }
12 m!(92);
13 m!(-92);
14 m!(-9.2);
15 m!(--92);
16 "#,
17         expect![[r#"
18 macro_rules! m { ($x:literal) => (literal!();); ($x:tt) => (not_a_literal!();); }
19 literal!();
20 literal!();
21 literal!();
22 /* error: leftover tokens */not_a_literal!();
23 "#]],
24     )
25 }
26
27 #[test]
28 fn test_expand_bad_literal() {
29     check(
30         r#"
31 macro_rules! m { ($i:literal) => {}; }
32 m!(&k");
33 "#,
34         expect![[r#"
35 macro_rules! m { ($i:literal) => {}; }
36 /* error: Failed to lower macro args to token tree */"#]],
37     );
38 }
39
40 #[test]
41 fn test_empty_comments() {
42     check(
43         r#"
44 macro_rules! m{ ($fmt:expr) => (); }
45 m!(/**/);
46 "#,
47         expect![[r#"
48 macro_rules! m{ ($fmt:expr) => (); }
49 /* error: expected Expr */
50 "#]],
51     );
52 }
53
54 #[test]
55 fn asi() {
56     // Thanks, Christopher!
57     //
58     // https://internals.rust-lang.org/t/understanding-decisions-behind-semicolons/15181/29
59     check(
60         r#"
61 macro_rules! asi { ($($stmt:stmt)*) => ($($stmt)*); }
62
63 fn main() {
64     asi! {
65         let a = 2
66         let b = 5
67         drop(b-a)
68         println!("{}", a+b)
69     }
70 }
71 "#,
72         expect![[r#"
73 macro_rules! asi { ($($stmt:stmt)*) => ($($stmt)*); }
74
75 fn main() {
76     let a = 2let b = 5drop(b-a)println!("{}", a+b)
77 }
78 "#]],
79     )
80 }
81
82 #[test]
83 fn stmt_boundaries() {
84     // FIXME: this actually works OK under rustc.
85     check(
86         r#"
87 macro_rules! m {
88     ($($s:stmt)*) => (stringify!($($s |)*);)
89 }
90 m!(;;92;let x = 92; loop {};);
91 "#,
92         expect![[r#"
93 macro_rules! m {
94     ($($s:stmt)*) => (stringify!($($s |)*);)
95 }
96 stringify!(;
97 |;
98 |92|;
99 |let x = 92|;
100 |loop {}
101 |;
102 |);
103 "#]],
104     );
105 }
106
107 #[test]
108 fn range_patterns() {
109     // FIXME: rustc thinks there are three patterns here, not one.
110     check(
111         r#"
112 macro_rules! m {
113     ($($p:pat)*) => (stringify!($($p |)*);)
114 }
115 m!(.. .. ..);
116 "#,
117         expect![[r#"
118 macro_rules! m {
119     ($($p:pat)*) => (stringify!($($p |)*);)
120 }
121 stringify!(.. .. ..|);
122 "#]],
123     );
124 }