Skip to content

Commit eef8485

Browse files
committed
More hygiene tests
1 parent 9a6864d commit eef8485

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

src/test/run-pass/hygiene.rs

+75-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010

1111
#![allow(unused)]
1212

13-
fn main() {
13+
fn f() {
1414
let x = 0;
1515
macro_rules! foo { () => {
1616
assert_eq!(x, 0);
1717
} }
1818

1919
let x = 1;
2020
foo!();
21-
22-
g();
2321
}
2422

2523
fn g() {
@@ -42,3 +40,77 @@ fn g() {
4240
assert_eq!(m2!(), (4, 0));
4341
assert_eq!(m3!(), (4, 1));
4442
}
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+
}

0 commit comments

Comments
 (0)