]>
Commit | Line | Data |
---|---|---|
3157f602 XL |
1 | // Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | #![allow(unused)] | |
12 | ||
13 | fn f() { | |
14 | let x = 0; | |
15 | macro_rules! foo { () => { | |
16 | assert_eq!(x, 0); | |
17 | } } | |
18 | ||
19 | let x = 1; | |
20 | foo!(); | |
21 | } | |
22 | ||
23 | fn g() { | |
24 | let x = 0; | |
25 | macro_rules! m { ($x:ident) => { | |
26 | macro_rules! m2 { () => { ($x, x) } } | |
27 | let x = 1; | |
28 | macro_rules! m3 { () => { ($x, x) } } | |
29 | } } | |
30 | ||
31 | let x = 2; | |
32 | m!(x); | |
33 | ||
34 | let x = 3; | |
35 | assert_eq!(m2!(), (2, 0)); | |
36 | assert_eq!(m3!(), (2, 1)); | |
37 | ||
38 | let x = 4; | |
39 | m!(x); | |
40 | assert_eq!(m2!(), (4, 0)); | |
41 | assert_eq!(m3!(), (4, 1)); | |
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 | ||
5bcae85e SL |
109 | fn label_hygiene() { |
110 | 'a: loop { | |
111 | macro_rules! m { () => { break 'a; } } | |
112 | m!(); | |
113 | } | |
114 | } | |
115 | ||
3157f602 XL |
116 | fn main() { |
117 | f(); | |
118 | g(); | |
119 | assert_eq!(foo::f(), (0, 11)); | |
120 | interpolated_pattern(); | |
121 | patterns_in_macro_generated_macros(); | |
122 | match_hygiene(); | |
123 | } |