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