summaryrefslogtreecommitdiffstats
path: root/tests/ui/hygiene/auxiliary/methods.rs
blob: 23b9c61cfc05820cac7060ec9757f70421f03100 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#![feature(decl_macro)]

#[derive(PartialEq, Eq, Debug)]
pub enum Method {
    DefaultMacroCtxt,
    DefaultRootCtxt,
    OverrideMacroCtxt,
    OverrideRootCtxt,
}

#[rustfmt::skip]
macro x($macro_name:ident, $macro2_name:ident, $trait_name:ident, $method_name:ident) {
    pub trait $trait_name {
        fn method(&self) -> Method {
            Method::DefaultMacroCtxt
        }

        fn $method_name(&self) -> Method {
            Method::DefaultRootCtxt
        }
    }

    impl $trait_name for () {}
    impl $trait_name for bool {
        fn method(&self) -> Method {
            Method::OverrideMacroCtxt
        }

        fn $method_name(&self) -> Method {
            Method::OverrideRootCtxt
        }
    }

    #[macro_export]
    macro_rules! $macro_name {
        (check_resolutions) => {
            assert_eq!(().method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&()), Method::DefaultMacroCtxt);
            assert_eq!(().$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&()), Method::DefaultRootCtxt);

            assert_eq!(false.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&false), Method::OverrideMacroCtxt);
            assert_eq!(false.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&false), Method::OverrideRootCtxt);

            assert_eq!('a'.method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&'a'), Method::DefaultMacroCtxt);
            assert_eq!('a'.$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&'a'), Method::DefaultRootCtxt);

            assert_eq!(1i32.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&1i32), Method::OverrideMacroCtxt);
            assert_eq!(1i32.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&1i32), Method::OverrideRootCtxt);

            assert_eq!(1i64.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&1i64), Method::OverrideMacroCtxt);
            assert_eq!(1i64.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&1i64), Method::OverrideRootCtxt);
        };
        (assert_no_override $v:expr) => {
            assert_eq!($v.method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&$v), Method::DefaultMacroCtxt);
            assert_eq!($v.$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&$v), Method::DefaultRootCtxt);
        };
        (assert_override $v:expr) => {
            assert_eq!($v.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&$v), Method::OverrideMacroCtxt);
            assert_eq!($v.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&$v), Method::OverrideRootCtxt);
        };
        (impl for $t:ty) => {
            impl $trait_name for $t {
                fn method(&self) -> Method {
                    Method::OverrideMacroCtxt
                }

                fn $method_name(&self) -> Method {
                    Method::OverrideRootCtxt
                }
            }
        };
    }

    pub macro $macro2_name {
        (check_resolutions) => {
            assert_eq!(().method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&()), Method::DefaultMacroCtxt);
            assert_eq!(().$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&()), Method::DefaultRootCtxt);

            assert_eq!(false.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&false), Method::OverrideMacroCtxt);
            assert_eq!(false.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&false), Method::OverrideRootCtxt);

            assert_eq!('a'.method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&'a'), Method::DefaultMacroCtxt);
            assert_eq!('a'.$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&'a'), Method::DefaultRootCtxt);

            assert_eq!(1i32.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&1i32), Method::OverrideMacroCtxt);
            assert_eq!(1i32.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&1i32), Method::OverrideRootCtxt);

            assert_eq!(1i64.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&1i64), Method::OverrideMacroCtxt);
            assert_eq!(1i64.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&1i64), Method::OverrideRootCtxt);
        },
        (assert_no_override $v:expr) => {
            assert_eq!($v.method(), Method::DefaultMacroCtxt);
            assert_eq!($trait_name::method(&$v), Method::DefaultMacroCtxt);
            assert_eq!($v.$method_name(), Method::DefaultRootCtxt);
            assert_eq!($trait_name::$method_name(&$v), Method::DefaultRootCtxt);
        },
        (assert_override $v:expr) => {
            assert_eq!($v.method(), Method::OverrideMacroCtxt);
            assert_eq!($trait_name::method(&$v), Method::OverrideMacroCtxt);
            assert_eq!($v.$method_name(), Method::OverrideRootCtxt);
            assert_eq!($trait_name::$method_name(&$v), Method::OverrideRootCtxt);
        },
        (impl for $t:ty) => {
            impl $trait_name for $t {
                fn method(&self) -> Method {
                    Method::OverrideMacroCtxt
                }

                fn $method_name(&self) -> Method {
                    Method::OverrideRootCtxt
                }
            }
        }
    }
}

x!(test_trait, test_trait2, MyTrait, method);

impl MyTrait for char {}
test_trait!(impl for i32);
test_trait2!(impl for i64);

pub fn check_crate_local() {
    test_trait!(check_resolutions);
    test_trait2!(check_resolutions);
}

// Check that any comparison of idents at monomorphization time is correct
pub fn check_crate_local_generic<T: MyTrait, U: MyTrait>(t: T, u: U) {
    test_trait!(check_resolutions);
    test_trait2!(check_resolutions);

    test_trait!(assert_no_override t);
    test_trait2!(assert_no_override t);
    test_trait!(assert_override u);
    test_trait2!(assert_override u);
}