summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/borrow_interior_mutable_const/traits.rs
blob: 06b5d62e8f9a816706e03fb174c9a28a5e95189f (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#![warn(clippy::borrow_interior_mutable_const)]
#![allow(clippy::declare_interior_mutable_const)]

// this file replicates its `declare` counterpart. Please see it for more discussions.

use std::borrow::Cow;
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};

trait ConcreteTypes {
    const ATOMIC: AtomicUsize;
    const STRING: String;

    fn function() {
        let _ = &Self::ATOMIC; //~ ERROR interior mutable
        let _ = &Self::STRING;
    }
}

impl ConcreteTypes for u64 {
    const ATOMIC: AtomicUsize = AtomicUsize::new(9);
    const STRING: String = String::new();

    fn function() {
        // Lint this again since implementers can choose not to borrow it.
        let _ = &Self::ATOMIC; //~ ERROR interior mutable
        let _ = &Self::STRING;
    }
}

// a helper trait used below
trait ConstDefault {
    const DEFAULT: Self;
}

trait GenericTypes<T, U> {
    const TO_REMAIN_GENERIC: T;
    const TO_BE_CONCRETE: U;

    fn function() {
        let _ = &Self::TO_REMAIN_GENERIC;
    }
}

impl<T: ConstDefault> GenericTypes<T, AtomicUsize> for Vec<T> {
    const TO_REMAIN_GENERIC: T = T::DEFAULT;
    const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);

    fn function() {
        let _ = &Self::TO_REMAIN_GENERIC;
        let _ = &Self::TO_BE_CONCRETE; //~ ERROR interior mutable
    }
}

// a helper type used below
pub struct Wrapper<T>(T);

trait AssocTypes {
    type ToBeFrozen;
    type ToBeUnfrozen;
    type ToBeGenericParam;

    const TO_BE_FROZEN: Self::ToBeFrozen;
    const TO_BE_UNFROZEN: Self::ToBeUnfrozen;
    const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen>;
    const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam>;

    fn function() {
        let _ = &Self::TO_BE_FROZEN;
        let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
    }
}

impl<T: ConstDefault> AssocTypes for Vec<T> {
    type ToBeFrozen = u16;
    type ToBeUnfrozen = AtomicUsize;
    type ToBeGenericParam = T;

    const TO_BE_FROZEN: Self::ToBeFrozen = 12;
    const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
    const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
    const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam> = Wrapper(T::DEFAULT);

    fn function() {
        let _ = &Self::TO_BE_FROZEN;
        let _ = &Self::TO_BE_UNFROZEN; //~ ERROR interior mutable
        let _ = &Self::WRAPPED_TO_BE_UNFROZEN; //~ ERROR interior mutable
        let _ = &Self::WRAPPED_TO_BE_GENERIC_PARAM;
    }
}

// a helper trait used below
trait AssocTypesHelper {
    type NotToBeBounded;
    type ToBeBounded;

    const NOT_TO_BE_BOUNDED: Self::NotToBeBounded;
}

trait AssocTypesFromGenericParam<T>
where
    T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
{
    const NOT_BOUNDED: T::NotToBeBounded;
    const BOUNDED: T::ToBeBounded;

    fn function() {
        let _ = &Self::NOT_BOUNDED;
        let _ = &Self::BOUNDED; //~ ERROR interior mutable
    }
}

impl<T> AssocTypesFromGenericParam<T> for Vec<T>
where
    T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
{
    const NOT_BOUNDED: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
    const BOUNDED: T::ToBeBounded = AtomicUsize::new(15);

    fn function() {
        let _ = &Self::NOT_BOUNDED;
        let _ = &Self::BOUNDED; //~ ERROR interior mutable
    }
}

trait SelfType: Sized {
    const SELF: Self;
    const WRAPPED_SELF: Option<Self>;

    fn function() {
        let _ = &Self::SELF;
        let _ = &Self::WRAPPED_SELF;
    }
}

impl SelfType for u64 {
    const SELF: Self = 16;
    const WRAPPED_SELF: Option<Self> = Some(20);

    fn function() {
        let _ = &Self::SELF;
        let _ = &Self::WRAPPED_SELF;
    }
}

impl SelfType for AtomicUsize {
    const SELF: Self = AtomicUsize::new(17);
    const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));

    fn function() {
        let _ = &Self::SELF; //~ ERROR interior mutable
        let _ = &Self::WRAPPED_SELF; //~ ERROR interior mutable
    }
}

trait BothOfCellAndGeneric<T> {
    const DIRECT: Cell<T>;
    const INDIRECT: Cell<*const T>;

    fn function() {
        let _ = &Self::DIRECT;
        let _ = &Self::INDIRECT; //~ ERROR interior mutable
    }
}

impl<T: ConstDefault> BothOfCellAndGeneric<T> for Vec<T> {
    const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
    const INDIRECT: Cell<*const T> = Cell::new(std::ptr::null());

    fn function() {
        let _ = &Self::DIRECT;
        let _ = &Self::INDIRECT; //~ ERROR interior mutable
    }
}

struct Local<T>(T);

impl<T> Local<T>
where
    T: ConstDefault + AssocTypesHelper<ToBeBounded = AtomicUsize>,
{
    const ATOMIC: AtomicUsize = AtomicUsize::new(18);
    const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");

    const GENERIC_TYPE: T = T::DEFAULT;

    const ASSOC_TYPE: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
    const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);

    fn function() {
        let _ = &Self::ATOMIC; //~ ERROR interior mutable
        let _ = &Self::COW;
        let _ = &Self::GENERIC_TYPE;
        let _ = &Self::ASSOC_TYPE;
        let _ = &Self::BOUNDED_ASSOC_TYPE; //~ ERROR interior mutable
    }
}

fn main() {
    u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
    assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
}