File tree 1 file changed +75
-3
lines changed
1 file changed +75
-3
lines changed Original file line number Diff line number Diff line change 10
10
11
11
#![ allow( unused) ]
12
12
13
- fn main ( ) {
13
+ fn f ( ) {
14
14
let x = 0 ;
15
15
macro_rules! foo { ( ) => {
16
16
assert_eq!( x, 0 ) ;
17
17
} }
18
18
19
19
let x = 1 ;
20
20
foo ! ( ) ;
21
-
22
- g ( ) ;
23
21
}
24
22
25
23
fn g ( ) {
@@ -42,3 +40,77 @@ fn g() {
42
40
assert_eq ! ( m2!( ) , ( 4 , 0 ) ) ;
43
41
assert_eq ! ( m3!( ) , ( 4 , 1 ) ) ;
44
42
}
43
+
44
+ mod foo {
45
+ macro_rules! m {
46
+ ( $f: ident : |$x: ident| $e: expr) => {
47
+ pub fn $f( ) -> ( i32 , i32 ) {
48
+ let x = 0 ;
49
+ let $x = 1 ;
50
+ ( x, $e)
51
+ }
52
+ }
53
+ }
54
+
55
+ m ! ( f: |x| x + 10 ) ;
56
+ }
57
+
58
+ fn interpolated_pattern ( ) {
59
+ let x = 0 ;
60
+ macro_rules! m {
61
+ ( $p: pat, $e: expr) => {
62
+ let $p = 1 ;
63
+ assert_eq!( ( x, $e) , ( 0 , 1 ) ) ;
64
+ }
65
+ }
66
+
67
+ m ! ( x, x) ;
68
+ }
69
+
70
+ fn patterns_in_macro_generated_macros ( ) {
71
+ let x = 0 ;
72
+ macro_rules! m {
73
+ ( $a: expr, $b: expr) => {
74
+ assert_eq!( x, 0 ) ;
75
+ let x = $a;
76
+ macro_rules! n {
77
+ ( ) => {
78
+ ( x, $b)
79
+ }
80
+ }
81
+ }
82
+ }
83
+
84
+ let x = 1 ;
85
+ m ! ( 2 , x) ;
86
+
87
+ let x = 3 ;
88
+ assert_eq ! ( n!( ) , ( 2 , 1 ) ) ;
89
+ }
90
+
91
+ fn match_hygiene ( ) {
92
+ let x = 0 ;
93
+
94
+ macro_rules! m {
95
+ ( $p: pat, $e: expr) => {
96
+ for result in & [ Ok ( 1 ) , Err ( 1 ) ] {
97
+ match * result {
98
+ $p => { assert_eq!( ( $e, x) , ( 1 , 0 ) ) ; }
99
+ Err ( x) => { assert_eq!( ( $e, x) , ( 2 , 1 ) ) ; }
100
+ }
101
+ }
102
+ }
103
+ }
104
+
105
+ let x = 2 ;
106
+ m ! ( Ok ( x) , x) ;
107
+ }
108
+
109
+ fn main ( ) {
110
+ f ( ) ;
111
+ g ( ) ;
112
+ assert_eq ! ( foo:: f( ) , ( 0 , 11 ) ) ;
113
+ interpolated_pattern ( ) ;
114
+ patterns_in_macro_generated_macros ( ) ;
115
+ match_hygiene ( ) ;
116
+ }
You can’t perform that action at this time.
0 commit comments