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