1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const)]
4 // this file replicates its `declare` counterpart. Please see it for more discussions.
8 use std::sync::atomic::{AtomicUsize, Ordering};
11 const ATOMIC: AtomicUsize;
15 let _ = &Self::ATOMIC; //~ ERROR interior mutable
16 let _ = &Self::STRING;
20 impl ConcreteTypes for u64 {
21 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
22 const STRING: String = String::new();
25 // Lint this again since implementers can choose not to borrow it.
26 let _ = &Self::ATOMIC; //~ ERROR interior mutable
27 let _ = &Self::STRING;
31 // a helper trait used below
36 trait GenericTypes<T, U> {
37 const TO_REMAIN_GENERIC: T;
38 const TO_BE_CONCRETE: U;
41 let _ = &Self::TO_REMAIN_GENERIC;
45 impl<T: ConstDefault> GenericTypes<T, AtomicUsize> for Vec<T> {
46 const TO_REMAIN_GENERIC: T = T::DEFAULT;
47 const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
50 let _ = &Self::TO_REMAIN_GENERIC;
51 let _ = &Self::TO_BE_CONCRETE; //~ ERROR interior mutable
55 // a helper type used below
56 pub struct Wrapper<T>(T);
61 type ToBeGenericParam;
63 const TO_BE_FROZEN: Self::ToBeFrozen;
64 const TO_BE_UNFROZEN: Self::ToBeUnfrozen;
65 const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen>;
66 const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam>;
69 let _ = &Self::TO_BE_FROZEN;
70 let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
74 impl<T: ConstDefault> AssocTypes for Vec<T> {
75 type ToBeFrozen = u16;
76 type ToBeUnfrozen = AtomicUsize;
77 type ToBeGenericParam = T;
79 const TO_BE_FROZEN: Self::ToBeFrozen = 12;
80 const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
81 const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
82 const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam> = Wrapper(T::DEFAULT);
85 let _ = &Self::TO_BE_FROZEN;
86 let _ = &Self::TO_BE_UNFROZEN; //~ ERROR interior mutable
87 let _ = &Self::WRAPPED_TO_BE_UNFROZEN; //~ ERROR interior mutable
88 let _ = &Self::WRAPPED_TO_BE_GENERIC_PARAM;
92 // a helper trait used below
93 trait AssocTypesHelper {
97 const NOT_TO_BE_BOUNDED: Self::NotToBeBounded;
100 trait AssocTypesFromGenericParam<T>
102 T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
104 const NOT_BOUNDED: T::NotToBeBounded;
105 const BOUNDED: T::ToBeBounded;
108 let _ = &Self::NOT_BOUNDED;
109 let _ = &Self::BOUNDED; //~ ERROR interior mutable
113 impl<T> AssocTypesFromGenericParam<T> for Vec<T>
115 T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
117 const NOT_BOUNDED: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
118 const BOUNDED: T::ToBeBounded = AtomicUsize::new(15);
121 let _ = &Self::NOT_BOUNDED;
122 let _ = &Self::BOUNDED; //~ ERROR interior mutable
126 trait SelfType: Sized {
128 const WRAPPED_SELF: Option<Self>;
132 let _ = &Self::WRAPPED_SELF;
136 impl SelfType for u64 {
137 const SELF: Self = 16;
138 const WRAPPED_SELF: Option<Self> = Some(20);
142 let _ = &Self::WRAPPED_SELF;
146 impl SelfType for AtomicUsize {
147 const SELF: Self = AtomicUsize::new(17);
148 const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
151 let _ = &Self::SELF; //~ ERROR interior mutable
152 let _ = &Self::WRAPPED_SELF; //~ ERROR interior mutable
156 trait BothOfCellAndGeneric<T> {
157 const DIRECT: Cell<T>;
158 const INDIRECT: Cell<*const T>;
161 let _ = &Self::DIRECT;
162 let _ = &Self::INDIRECT; //~ ERROR interior mutable
166 impl<T: ConstDefault> BothOfCellAndGeneric<T> for Vec<T> {
167 const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
168 const INDIRECT: Cell<*const T> = Cell::new(std::ptr::null());
171 let _ = &Self::DIRECT;
172 let _ = &Self::INDIRECT; //~ ERROR interior mutable
180 T: ConstDefault + AssocTypesHelper<ToBeBounded = AtomicUsize>,
182 const ATOMIC: AtomicUsize = AtomicUsize::new(18);
183 const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
185 const GENERIC_TYPE: T = T::DEFAULT;
187 const ASSOC_TYPE: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
188 const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);
191 let _ = &Self::ATOMIC; //~ ERROR interior mutable
193 let _ = &Self::GENERIC_TYPE;
194 let _ = &Self::ASSOC_TYPE;
195 let _ = &Self::BOUNDED_ASSOC_TYPE; //~ ERROR interior mutable
200 u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
201 assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability