]> git.lizzy.rs Git - rust.git/blob - tests/ui/non_copy_const.rs
Lint against const items which are interior mutable. Fix #1560.
[rust.git] / tests / ui / non_copy_const.rs
1 #![feature(const_string_new, const_vec_new)]
2 #![allow(ref_in_deref, dead_code)]
3
4 use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
5 use std::cell::Cell;
6 use std::sync::Once;
7 use std::borrow::Cow;
8 use std::fmt::Display;
9
10 const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
11 const CELL: Cell<usize> = Cell::new(6); //~ ERROR interior mutable
12 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
13 //~^ ERROR interior mutable
14
15 macro_rules! declare_const {
16     ($name:ident: $ty:ty = $e:expr) => { const $name: $ty = $e; };
17 }
18 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
19
20 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
21
22 const INTEGER: u8 = 8;
23 const STRING: String = String::new();
24 const STR: &str = "012345";
25 const COW: Cow<str> = Cow::Borrowed("abcdef");
26 //^ note: a const item of Cow is used in the `postgres` package.
27
28 const NO_ANN: &Display = &70;
29
30 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
31 //^ there should be no lints on this line
32
33 #[allow(declare_interior_mutable_const)]
34 const ONCE_INIT: Once = Once::new();
35
36 trait Trait<T>: Copy {
37     type NonCopyType;
38
39     const ATOMIC: AtomicUsize; //~ ERROR interior mutable
40     const INTEGER: u64;
41     const STRING: String;
42     const SELF: Self; // (no error)
43     const INPUT: T;
44     //~^ ERROR interior mutable
45     //~| HELP consider requiring `T` to be `Copy`
46     const ASSOC: Self::NonCopyType;
47     //~^ ERROR interior mutable
48     //~| HELP consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
49
50     const AN_INPUT: T = Self::INPUT;
51     //~^ ERROR interior mutable
52     //~| ERROR consider requiring `T` to be `Copy`
53     declare_const!(ANOTHER_INPUT: T = Self::INPUT); //~ ERROR interior mutable
54 }
55
56 trait Trait2 {
57     type CopyType: Copy;
58
59     const SELF_2: Self;
60     //~^ ERROR interior mutable
61     //~| HELP consider requiring `Self` to be `Copy`
62     const ASSOC_2: Self::CopyType; // (no error)
63 }
64
65 // we don't lint impl of traits, because an impl has no power to change the interface.
66 impl Trait<u32> for u64 {
67     type NonCopyType = u16;
68
69     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
70     const INTEGER: u64 = 10;
71     const STRING: String = String::new();
72     const SELF: Self = 11;
73     const INPUT: u32 = 12;
74     const ASSOC: Self::NonCopyType = 13;
75 }
76
77 struct Local<T, U>(T, U);
78
79 impl<T: Trait2 + Trait<u32>, U: Trait2> Local<T, U> {
80     const ASSOC_3: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
81     const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
82     const T_SELF: T = T::SELF_2;
83     const U_SELF: U = U::SELF_2;
84     //~^ ERROR interior mutable
85     //~| HELP consider requiring `U` to be `Copy`
86     const T_ASSOC: T::NonCopyType = T::ASSOC;
87     //~^ ERROR interior mutable
88     //~| HELP consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
89     const U_ASSOC: U::CopyType = U::ASSOC_2;
90 }
91
92 fn main() {
93     ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
94     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
95
96     ATOMIC_USIZE_INIT.store(2, Ordering::SeqCst); //~ ERROR interior mutability
97     assert_eq!(ATOMIC_USIZE_INIT.load(Ordering::SeqCst), 0); //~ ERROR interior mutability
98
99     let _once = ONCE_INIT;
100     let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
101     let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
102     let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
103     let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
104     let _atomic_into_inner = ATOMIC.into_inner();
105     // these should be all fine.
106     let _twice = (ONCE_INIT, ONCE_INIT);
107     let _ref_twice = &(ONCE_INIT, ONCE_INIT);
108     let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
109     let _array_twice = [ONCE_INIT, ONCE_INIT];
110     let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
111     let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
112
113     // referencing projection is still bad.
114     let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
115     let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
116     let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
117     let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
118     let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
119     let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
120     let _ = &ATOMIC_TUPLE.2;
121     let _ = (&&&&ATOMIC_TUPLE).0;
122     let _ = (&&&&ATOMIC_TUPLE).2;
123     let _ = ATOMIC_TUPLE.0;
124     let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
125     let _ = ATOMIC_TUPLE.1.into_iter();
126     let _ = ATOMIC_TUPLE.2;
127     let _ = &{ATOMIC_TUPLE};
128
129     CELL.set(2); //~ ERROR interior mutability
130     assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
131
132     assert_eq!(INTEGER, 8);
133     assert!(STRING.is_empty());
134
135     let a = ATOMIC;
136     a.store(4, Ordering::SeqCst);
137     assert_eq!(a.load(Ordering::SeqCst), 4);
138
139     STATIC_TUPLE.0.store(3, Ordering::SeqCst);
140     assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
141     assert!(STATIC_TUPLE.1.is_empty());
142
143     u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
144     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
145
146     assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
147 }