]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const_let_eq_float.rs
Rollup merge of #64603 - gilescope:unused-lifetime-warning, r=matthewjasper
[rust.git] / src / test / ui / consts / const_let_eq_float.rs
1 // build-pass (FIXME(62277): could be check-pass?)
2
3 #![feature(const_fn)]
4
5 struct Foo<T>(T);
6 struct Bar<T> { x: T }
7 struct W(f32);
8 struct A { a: f32 }
9
10 const fn basics((a,): (f32,)) -> f32 {
11     // Deferred assignment:
12     let b: f32;
13     b = a + 1.0;
14
15     // Immediate assignment:
16     let c: f32 = b + 1.0;
17
18     // Mutables:
19     let mut d: f32 = c + 1.0;
20     d = d + 1.0;
21     // +4 so far.
22
23     // No effect statements work:
24     ; ;
25     1;
26
27     // Array projection
28     let mut arr: [f32; 1] = [0.0];
29     arr[0] = 1.0;
30     d = d + arr[0];
31     // +5
32
33     // Field projection:
34     let mut foo: Foo<f32> = Foo(0.0);
35     let mut bar: Bar<f32> = Bar { x: 0.0 };
36     foo.0 = 1.0;
37     bar.x = 1.0;
38     d = d + foo.0 + bar.x;
39     // +7
40
41     // Array + Field projection:
42     let mut arr: [Foo<f32>; 1] = [Foo(0.0)];
43     arr[0].0 = 1.0;
44     d = d + arr[0].0;
45     let mut arr: [Bar<f32>; 1] = [Bar { x: 0.0 }];
46     arr[0].x = 1.0;
47     d = d + arr[0].x;
48     // +9
49
50     // Field + Array projection:
51     let mut arr: Foo<[f32; 1]> = Foo([0.0]);
52     (arr.0)[0] = 1.0;
53     d = d + (arr.0)[0];
54     let mut arr: Bar<[f32; 1]> = Bar { x: [0.0] };
55     arr.x[0] = 1.0;
56     d = d + arr.x[0];
57     // +11
58
59     d
60 }
61
62 const fn add_assign(W(a): W) -> f32 {
63     // Mutables:
64     let mut d: f32 = a + 1.0;
65     d += 1.0;
66     // +2 so far.
67
68     // Array projection
69     let mut arr: [f32; 1] = [0.0];
70     arr[0] += 1.0;
71     d += arr[0];
72     // +3
73
74     // Field projection:
75     let mut foo: Foo<f32> = Foo(0.0);
76     let mut bar: Bar<f32> = Bar { x: 0.0 };
77     foo.0 += 1.0;
78     bar.x += 1.0;
79     d += foo.0 + bar.x;
80     // +5
81
82     // Array + Field projection:
83     let mut arr: [Foo<f32>; 1] = [Foo(0.0)];
84     arr[0].0 += 1.0;
85     d += arr[0].0;
86     let mut arr: [Bar<f32>; 1] = [Bar { x: 0.0 }];
87     arr[0].x += 1.0;
88     d += arr[0].x;
89     // +7
90
91     // Field + Array projection:
92     let mut arr: Foo<[f32; 1]> = Foo([0.0]);
93     (arr.0)[0] += 1.0;
94     d += (arr.0)[0];
95     let mut arr: Bar<[f32; 1]> = Bar { x: [0.0] };
96     arr.x[0] += 1.0;
97     d += arr.x[0];
98     // +9
99
100     d
101 }
102
103 const fn mul_assign(A { a }: A) -> f32 {
104     // Mutables:
105     let mut d: f32 = a + 1.0;
106     d *= 2.0;
107     // 2^1 * (a + 1)
108
109     // Array projection
110     let mut arr: [f32; 1] = [1.0];
111     arr[0] *= 2.0;
112     d *= arr[0];
113     // 2^2 * (a + 1)
114
115     // Field projection:
116     let mut foo: Foo<f32> = Foo(1.0);
117     let mut bar: Bar<f32> = Bar { x: 1.0 };
118     foo.0 *= 2.0;
119     bar.x *= 2.0;
120     d *= foo.0 + bar.x;
121     // 2^4 * (a + 1)
122
123     // Array + Field projection:
124     let mut arr: [Foo<f32>; 1] = [Foo(1.0)];
125     arr[0].0 *= 2.0;
126     d *= arr[0].0;
127     let mut arr: [Bar<f32>; 1] = [Bar { x: 1.0 }];
128     arr[0].x *= 2.0;
129     d *= arr[0].x;
130     // 2^6 * (a + 1)
131
132     // Field + Array projection:
133     let mut arr: Foo<[f32; 1]> = Foo([1.0]);
134     (arr.0)[0] *= 2.0;
135     d *= (arr.0)[0];
136     let mut arr: Bar<[f32; 1]> = Bar { x: [1.0] };
137     arr.x[0] *= 2.0;
138     d *= arr.x[0];
139     // 2^8 * (a + 1)
140
141     d
142 }
143
144 const fn div_assign(a: [f32; 1]) -> f32 {
145     let a = a[0];
146     // Mutables:
147     let mut d: f32 = 1024.0 * a;
148     d /= 2.0;
149     // 512
150
151     // Array projection
152     let mut arr: [f32; 1] = [4.0];
153     arr[0] /= 2.0;
154     d /= arr[0];
155     // 256
156
157     // Field projection:
158     let mut foo: Foo<f32> = Foo(4.0);
159     let mut bar: Bar<f32> = Bar { x: 4.0 };
160     foo.0 /= 2.0;
161     bar.x /= 2.0;
162     d /= foo.0;
163     d /= bar.x;
164     // 64
165
166     // Array + Field projection:
167     let mut arr: [Foo<f32>; 1] = [Foo(4.0)];
168     arr[0].0 /= 2.0;
169     d /= arr[0].0;
170     let mut arr: [Bar<f32>; 1] = [Bar { x: 4.0 }];
171     arr[0].x /= 2.0;
172     d /= arr[0].x;
173     // 16
174
175     // Field + Array projection:
176     let mut arr: Foo<[f32; 1]> = Foo([4.0]);
177     (arr.0)[0] /= 2.0;
178     d /= (arr.0)[0];
179     let mut arr: Bar<[f32; 1]> = Bar { x: [4.0] };
180     arr.x[0] /= 2.0;
181     d /= arr.x[0];
182     // 4
183
184     d
185 }
186
187 const fn rem_assign(W(a): W) -> f32 {
188     // Mutables:
189     let mut d: f32 = a;
190     d %= 10.0;
191     d += 10.0;
192
193     // Array projection
194     let mut arr: [f32; 1] = [3.0];
195     arr[0] %= 2.0;
196     d %= 9.0 + arr[0];
197     d += 10.0;
198
199     // Field projection:
200     let mut foo: Foo<f32> = Foo(5.0);
201     let mut bar: Bar<f32> = Bar { x: 7.0 };
202     foo.0 %= 2.0;
203     bar.x %= 2.0;
204     d %= 8.0 + foo.0 + bar.x;
205     d += 10.0;
206
207     // Array + Field projection:
208     let mut arr: [Foo<f32>; 1] = [Foo(4.0)];
209     arr[0].0 %= 3.0;
210     d %= 9.0 + arr[0].0;
211     d += 10.0;
212     let mut arr: [Bar<f32>; 1] = [Bar { x: 7.0 }];
213     arr[0].x %= 3.0;
214     d %= 9.0 + arr[0].x;
215     d += 10.0;
216
217     // Field + Array projection:
218     let mut arr: Foo<[f32; 1]> = Foo([6.0]);
219     (arr.0)[0] %= 5.0;
220     d %= 9.0 + (arr.0)[0];
221     let mut arr: Bar<[f32; 1]> = Bar { x: [11.0] };
222     arr.x[0] %= 5.0;
223     d %= 9.0 + arr.x[0];
224
225     d
226 }
227
228 const fn sub_assign(W(a): W) -> f32 {
229     // Mutables:
230     let mut d: f32 = a;
231     d -= 1.0;
232
233     // Array projection
234     let mut arr: [f32; 1] = [2.0];
235     arr[0] -= 1.0;
236     d -= arr[0];
237
238     // Field projection:
239     let mut foo: Foo<f32> = Foo(2.0);
240     let mut bar: Bar<f32> = Bar { x: 2.0 };
241     foo.0 -= 1.0;
242     bar.x -= 1.0;
243     d -= foo.0 + bar.x;
244
245     // Array + Field projection:
246     let mut arr: [Foo<f32>; 1] = [Foo(2.0)];
247     arr[0].0 -= 1.0;
248     d -= arr[0].0;
249     let mut arr: [Bar<f32>; 1] = [Bar { x: 2.0 }];
250     arr[0].x -= 1.0;
251     d -= arr[0].x;
252
253     // Field + Array projection:
254     let mut arr: Foo<[f32; 1]> = Foo([2.0]);
255     (arr.0)[0] -= 1.0;
256     d -= (arr.0)[0];
257     let mut arr: Bar<[f32; 1]> = Bar { x: [2.0] };
258     arr.x[0] -= 1.0;
259     d -= arr.x[0];
260
261     d
262 }
263
264 macro_rules! test {
265     ($c:ident, $e:expr, $r:expr) => {
266         const $c: f32 = $e;
267         assert_eq!($c, $r);
268         assert_eq!($e, $r);
269     }
270 }
271
272 fn main() {
273     test!(BASICS, basics((2.0,)), 13.0);
274     test!(ADD, add_assign(W(1.0)), 10.0);
275     test!(MUL, mul_assign(A { a: 0.0 }), 256.0);
276     test!(DIV, div_assign([1.0]), 4.0);
277     test!(REM, rem_assign(W(5.0)), 5.0);
278     test!(SUB, sub_assign(W(8.0)), 0.0);
279 }