]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/macro-pat.rs
rollup merge of #20608: nikomatsakis/assoc-types-method-dispatch
[rust.git] / src / test / run-pass / macro-pat.rs
1 // Copyright 2012-2014 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 #![feature(macro_rules)]
12
13 macro_rules! mypat(
14     () => (
15         Some('y')
16     )
17 );
18
19 macro_rules! char_x(
20     () => (
21         'x'
22     )
23 );
24
25 macro_rules! some(
26     ($x:pat) => (
27         Some($x)
28     )
29 );
30
31 macro_rules! indirect(
32     () => (
33         some!(char_x!())
34     )
35 );
36
37 macro_rules! ident_pat(
38     ($x:ident) => (
39         $x
40     )
41 );
42
43 fn f(c: Option<char>) -> uint {
44     match c {
45         Some('x') => 1,
46         mypat!() => 2,
47         _ => 3,
48     }
49 }
50
51 pub fn main() {
52     assert_eq!(1u, f(Some('x')));
53     assert_eq!(2u, f(Some('y')));
54     assert_eq!(3u, f(None));
55
56     assert_eq!(1i, match Some('x') {
57         Some(char_x!()) => 1i,
58         _ => 2i,
59     });
60
61     assert_eq!(1i, match Some('x') {
62         some!(char_x!()) => 1i,
63         _ => 2i,
64     });
65
66     assert_eq!(1i, match Some('x') {
67         indirect!() => 1i,
68         _ => 2i,
69     });
70
71     assert_eq!(3i, {
72         let ident_pat!(x) = 2i;
73         x+1i
74     });
75 }