summaryrefslogtreecommitdiffstats
path: root/src/test/ui/macros/macro-literal.rs
blob: 3c2e71f9c43f3610c7416efb7489c61cc8694876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// run-pass

macro_rules! mtester {
    ($l:literal) => {
        &format!("macro caught literal: {}", $l)
    };
    ($e:expr) => {
        &format!("macro caught expr: {}", $e)
    };
}

macro_rules! two_negative_literals {
    ($l1:literal $l2:literal) => {
        &format!("macro caught literals: {}, {}", $l1, $l2)
    };
}

macro_rules! only_expr {
    ($e:expr) => {
        &format!("macro caught expr: {}", $e)
    };
}

#[allow(unused_macro_rules)]
macro_rules! mtester_dbg {
    ($l:literal) => {
        &format!("macro caught literal: {:?}", $l)
    };
    ($e:expr) => {
        &format!("macro caught expr: {:?}", $e)
    };
}

macro_rules! catch_range {
    ($s:literal ..= $e:literal) => {
        &format!("macro caught literal: {} ..= {}", $s, $e)
    };
    (($s:expr) ..= ($e:expr)) => { // Must use ')' before '..='
        &format!("macro caught expr: {} ..= {}", $s, $e)
    };
}

macro_rules! pat_match {
    ($s:literal ..= $e:literal) => {
        match 3 {
            $s ..= $e => "literal, in range",
            _ => "literal, other",
        }
    };
    ($s:pat) => {
        match 3 {
            $s => "pat, single",
            _ => "pat, other",
        }
    };
}

macro_rules! match_attr {
    (#[$attr:meta] $e:literal) => {
        "attr matched literal"
    };
    (#[$attr:meta] $e:expr) => {
        "attr matched expr"
    };
}

macro_rules! match_produced_attr {
    ($lit: literal) => {
        // Struct with doc comment passed via $literal
        #[doc = $lit]
        struct LiteralProduced;
    };
    ($expr: expr) => {
        struct ExprProduced;
    };
}

macro_rules! test_user {
    ($s:literal, $e:literal) => {
        {
            let mut v = Vec::new();
            for i in $s .. $e {
                v.push(i);
            }
            "literal"
        }
    };
    ($s:expr, $e: expr) => {
        {
            let mut v = Vec::new();
            for i in $s .. $e {
                v.push(i);
            }
            "expr"
        }
    };
}

pub fn main() {
    // Cases where 'literal' catches
    assert_eq!(mtester!("str"), "macro caught literal: str");
    assert_eq!(mtester!(2), "macro caught literal: 2");
    assert_eq!(mtester!(2.2), "macro caught literal: 2.2");
    assert_eq!(mtester!(1u32), "macro caught literal: 1");
    assert_eq!(mtester!(0x32), "macro caught literal: 50");
    assert_eq!(mtester!('c'), "macro caught literal: c");
    assert_eq!(mtester!(-1.2), "macro caught literal: -1.2");
    assert_eq!(two_negative_literals!(-2 -3), "macro caught literals: -2, -3");
    assert_eq!(catch_range!(2 ..= 3), "macro caught literal: 2 ..= 3");
    assert_eq!(match_attr!(#[attr] 1), "attr matched literal");
    assert_eq!(test_user!(10, 20), "literal");
    assert_eq!(mtester!(false), "macro caught literal: false");
    assert_eq!(mtester!(true), "macro caught literal: true");
    match_produced_attr!("a");
    let _a = LiteralProduced;
    assert_eq!(pat_match!(1 ..= 3), "literal, in range");
    assert_eq!(pat_match!(4 ..= 6), "literal, other");

    // Cases where 'expr' catches
    assert_eq!(mtester!((-1.2)), "macro caught expr: -1.2");
    assert_eq!(only_expr!(-1.2), "macro caught expr: -1.2");
    assert_eq!(mtester!((1 + 3)), "macro caught expr: 4");
    assert_eq!(mtester_dbg!(()), "macro caught expr: ()");
    assert_eq!(catch_range!((1 + 1) ..= (2 + 2)), "macro caught expr: 2 ..= 4");
    assert_eq!(match_attr!(#[attr] (1 + 2)), "attr matched expr");
    assert_eq!(test_user!(10, (20 + 2)), "expr");

    match_produced_attr!((3 + 2));
    let _b = ExprProduced;

    // Cases where 'pat' matched
    assert_eq!(pat_match!(3), "pat, single");
    assert_eq!(pat_match!(6), "pat, other");
}