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

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

macro_rules! MyLocalDerive {
    derive() { $($body:tt)* } => {
        println!("MyLocalDerive: body={:?}", stringify!($($body)*));
    };
    { $($args:tt)* } => {
        println!("MyLocalDerive!({:?})", stringify!($($args)*));
    };
}

trait MyTrait {
    fn name() -> &'static str;
}

macro_rules! MyTrait {
    derive() { struct $name:ident; } => {
        impl MyTrait for $name {
            fn name() -> &'static str {
                stringify!($name)
            }
        }
    };
}

#[derive(MyTrait)]
struct MyGlobalType;

fn main() {
    #[derive(crate::MyExportedDerive)]
    struct _S1;
    #[derive(crate::MyExportedDerive, crate::MyExportedDerive)]
    struct _Twice1;

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

    #[derive(MyExportedDerive)]
    struct _S2;
    #[derive(MyExportedDerive, MyExportedDerive)]
    struct _Twice2;

    MyExportedDerive!();
    MyExportedDerive!(invoked, arguments);

    #[derive(MyLocalDerive)]
    struct _S3;
    #[derive(MyLocalDerive, MyLocalDerive)]
    struct _Twice3;

    MyLocalDerive!();
    MyLocalDerive!(invoked, arguments);

    #[derive(MyTrait)]
    struct MyLocalType;

    println!("MyGlobalType::name(): {}", MyGlobalType::name());
    println!("MyLocalType::name(): {}", MyLocalType::name());
}