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