about summary refs log tree commit diff
path: root/tests/ui/macros/macro-rules-attr.rs
blob: 1d6f09b53e1a5433264bc0abc6e2be70b66d0635 (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
//@ run-pass
//@ check-run-results
#![feature(macro_attr)]
#![warn(unused)]

#[macro_export]
macro_rules! exported_attr {
    attr($($args:tt)*) { $($body:tt)* } => {
        println!(
            "exported_attr: args={:?}, body={:?}",
            stringify!($($args)*),
            stringify!($($body)*),
        );
    };
    { $($args:tt)* } => {
        println!("exported_attr!({:?})", stringify!($($args)*));
    };
    attr() {} => {
        unused_rule();
    };
    attr() {} => {
        compile_error!();
    };
    {} => {
        unused_rule();
    };
    {} => {
        compile_error!();
    };
}

macro_rules! local_attr {
    attr($($args:tt)*) { $($body:tt)* } => {
        println!(
            "local_attr: args={:?}, body={:?}",
            stringify!($($args)*),
            stringify!($($body)*),
        );
    };
    { $($args:tt)* } => {
        println!("local_attr!({:?})", stringify!($($args)*));
    };
    attr() {} => { //~ WARN: never used
        unused_rule();
    };
    attr() {} => {
        compile_error!();
    };
    {} => { //~ WARN: never used
        unused_rule();
    };
    {} => {
        compile_error!();
    };
}

fn main() {
    #[crate::exported_attr]
    struct S;
    #[::exported_attr(arguments, key = "value")]
    fn func(_arg: u32) {}
    #[self::exported_attr(1)]
    #[self::exported_attr(2)]
    struct Twice;

    crate::exported_attr!();
    crate::exported_attr!(invoked, arguments);

    #[exported_attr]
    struct S;
    #[exported_attr(arguments, key = "value")]
    fn func(_arg: u32) {}
    #[exported_attr(1)]
    #[exported_attr(2)]
    struct Twice;

    exported_attr!();
    exported_attr!(invoked, arguments);

    #[local_attr]
    struct S;
    #[local_attr(arguments, key = "value")]
    fn func(_arg: u32) {}
    #[local_attr(1)]
    #[local_attr(2)]
    struct Twice;

    local_attr!();
    local_attr!(invoked, arguments);
}