Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/fail_arrayop2.d

raw
1 // REQUIRED_ARGS: -o-
2
3
4 /*
5 TEST_OUTPUT:
6 ---
7 fail_compilation/fail_arrayop2.d(13): Error: array operation `[1, 2, 3] - [1, 2, 3]` without destination memory not allowed
8 fail_compilation/fail_arrayop2.d(16): Error: invalid array operation `"a" - "b"` (possible missing [])
9 ---
10 */
11 void test2603() // https://issues.dlang.org/show_bug.cgi?id=2603 - ICE(cgcs.c) on subtracting string literals
12 {
13 auto c1 = [1,2,3] - [1,2,3];
14
15 // this variation is wrong code on D2, ICE ..\ztc\cgcs.c 358 on D1.
16 string c2 = "a" - "b";
17 }
18
19 /*
20 TEST_OUTPUT:
21 ---
22 fail_compilation/fail_arrayop2.d(38): Error: array operation `-a[]` without destination memory not allowed (possible missing [])
23 fail_compilation/fail_arrayop2.d(39): Error: array operation `~a[]` without destination memory not allowed (possible missing [])
24 fail_compilation/fail_arrayop2.d(41): Error: array operation `a[] + a[]` without destination memory not allowed (possible missing [])
25 fail_compilation/fail_arrayop2.d(42): Error: array operation `a[] - a[]` without destination memory not allowed (possible missing [])
26 fail_compilation/fail_arrayop2.d(43): Error: array operation `a[] * a[]` without destination memory not allowed (possible missing [])
27 fail_compilation/fail_arrayop2.d(44): Error: array operation `a[] / a[]` without destination memory not allowed (possible missing [])
28 fail_compilation/fail_arrayop2.d(45): Error: array operation `a[] % a[]` without destination memory not allowed (possible missing [])
29 fail_compilation/fail_arrayop2.d(46): Error: array operation `a[] ^ a[]` without destination memory not allowed (possible missing [])
30 fail_compilation/fail_arrayop2.d(47): Error: array operation `a[] & a[]` without destination memory not allowed (possible missing [])
31 fail_compilation/fail_arrayop2.d(48): Error: array operation `a[] | a[]` without destination memory not allowed (possible missing [])
32 fail_compilation/fail_arrayop2.d(49): Error: array operation `a[] ^^ a[]` without destination memory not allowed (possible missing [])
33 ---
34 */
35 void test9459()
36 {
37 int[] a = [1, 2, 3];
38 a = -a[];
39 a = ~a[];
40
41 a = a[] + a[];
42 a = a[] - a[];
43 a = a[] * a[];
44 a = a[] / a[];
45 a = a[] % a[];
46 a = a[] ^ a[];
47 a = a[] & a[];
48 a = a[] | a[];
49 a = a[] ^^ a[];
50 }
51
52 /*
53 TEST_OUTPUT:
54 ---
55 fail_compilation/fail_arrayop2.d(75): Error: array operation `a[] + a[]` without destination memory not allowed
56 fail_compilation/fail_arrayop2.d(76): Error: array operation `a[] - a[]` without destination memory not allowed
57 fail_compilation/fail_arrayop2.d(77): Error: array operation `a[] * a[]` without destination memory not allowed
58 fail_compilation/fail_arrayop2.d(78): Error: array operation `a[] / a[]` without destination memory not allowed
59 fail_compilation/fail_arrayop2.d(79): Error: array operation `a[] % a[]` without destination memory not allowed
60 fail_compilation/fail_arrayop2.d(80): Error: array operation `a[] ^ a[]` without destination memory not allowed
61 fail_compilation/fail_arrayop2.d(81): Error: array operation `a[] & a[]` without destination memory not allowed
62 fail_compilation/fail_arrayop2.d(82): Error: array operation `a[] | a[]` without destination memory not allowed
63 fail_compilation/fail_arrayop2.d(83): Error: array operation `a[] ^^ 10` without destination memory not allowed
64 fail_compilation/fail_arrayop2.d(84): Error: array operation `-a[]` without destination memory not allowed
65 fail_compilation/fail_arrayop2.d(85): Error: array operation `~a[]` without destination memory not allowed
66 fail_compilation/fail_arrayop2.d(90): Error: array operation `[1] + a[]` without destination memory not allowed
67 fail_compilation/fail_arrayop2.d(91): Error: array operation `[1] + a[]` without destination memory not allowed
68 ---
69 */
70 void test12179()
71 {
72 void foo(int[]) {}
73 int[1] a;
74
75 foo(a[] + a[]);
76 foo(a[] - a[]);
77 foo(a[] * a[]);
78 foo(a[] / a[]);
79 foo(a[] % a[]);
80 foo(a[] ^ a[]);
81 foo(a[] & a[]);
82 foo(a[] | a[]);
83 foo(a[] ^^ 10);
84 foo(-a[]);
85 foo(~a[]);
86
87 // from https://issues.dlang.org/show_bug.cgi?id=11992
88 int[] arr1;
89 int[][] arr2;
90 arr1 ~= [1] + a[]; // NG
91 arr2 ~= [1] + a[]; // NG
92 }
93
94 /*
95 TEST_OUTPUT:
96 ---
97 fail_compilation/fail_arrayop2.d(105): Error: array operation `h * y[]` without destination memory not allowed
98 ---
99 */
100 void test12381()
101 {
102 double[2] y;
103 double h;
104
105 double[2] temp1 = cast(double[2])(h * y[]);
106 }
107
108 /*
109 TEST_OUTPUT:
110 ---
111 fail_compilation/fail_arrayop2.d(118): Error: array operation `-a[]` without destination memory not allowed
112 fail_compilation/fail_arrayop2.d(120): Error: array operation `(-a[])[0..4]` without destination memory not allowed
113 ---
114 */
115 float[] test12769(float[] a)
116 {
117 if (a.length < 4)
118 return -a[];
119 else
120 return (-a[])[0..4];
121 }
122
123 /*
124 TEST_OUTPUT:
125 ---
126 fail_compilation/fail_arrayop2.d(137): Error: array operation `a[] - a[]` without destination memory not allowed
127 fail_compilation/fail_arrayop2.d(139): Error: array operation `a[] - a[]` without destination memory not allowed
128 fail_compilation/fail_arrayop2.d(140): Error: array operation `a[] - a[]` without destination memory not allowed
129 fail_compilation/fail_arrayop2.d(143): Error: array operation `a[] - a[]` without destination memory not allowed
130 fail_compilation/fail_arrayop2.d(145): Error: array operation `a[] - a[]` without destination memory not allowed
131 ---
132 */
133 void test13208()
134 {
135 int[] a;
136
137 auto arr = [a[] - a[]][0];
138
139 auto aa1 = [1 : a[] - a[]];
140 auto aa2 = [a[] - a[] : 1];
141
142 struct S { int[] a; }
143 auto s = S(a[] - a[]);
144
145 auto n = int(a[] - a[]);
146 }
147
148 /*
149 TEST_OUTPUT:
150 ---
151 fail_compilation/fail_arrayop2.d(160): Error: array operation `a[] * a[]` without destination memory not allowed
152 fail_compilation/fail_arrayop2.d(161): Error: array operation `(a[] * a[])[0..1]` without destination memory not allowed
153 fail_compilation/fail_arrayop2.d(164): Error: array operation `a[] * a[]` without destination memory not allowed (possible missing [])
154 fail_compilation/fail_arrayop2.d(165): Error: array operation `(a[] * a[])[0..1]` without destination memory not allowed (possible missing [])
155 ---
156 */
157 void test13497()
158 {
159 int[1] a;
160 auto b1 = (a[] * a[])[];
161 auto b2 = (a[] * a[])[0..1];
162
163 int[] c;
164 c = (a[] * a[])[];
165 c = (a[] * a[])[0..1];
166 }
167
168 /*
169 TEST_OUTPUT:
170 ---
171 fail_compilation/fail_arrayop2.d(181): Error: array operation `data[segmentId][28..29] & cast(ubyte)(1 << 0)` without destination memory not allowed
172 ---
173 */
174 void test13910()
175 {
176 ubyte[][] data;
177 size_t segmentId;
178
179 bool isGroup()
180 {
181 return !!((data[segmentId][28..29]) & (1 << 0));
182 }
183 }
184
185 /*
186 TEST_OUTPUT:
187 ---
188 fail_compilation/fail_arrayop2.d(195): Error: array operation `a[] + 1` without destination memory not allowed
189 fail_compilation/fail_arrayop2.d(195): Error: array operation `a[] * 2` without destination memory not allowed
190 ---
191 */
192 void test14895()
193 {
194 int[] a;
195 int[] b = (a[] + 1) ~ a[] * 2;
196 }
197
198 /*
199 TEST_OUTPUT:
200 ---
201 fail_compilation/fail_arrayop2.d(247): Error: array operation `[1] * 6` without destination memory not allowed
202 fail_compilation/fail_arrayop2.d(248): Error: array operation `[1] * 6` without destination memory not allowed
203 fail_compilation/fail_arrayop2.d(249): Error: array operation `[1] * 6` without destination memory not allowed
204 fail_compilation/fail_arrayop2.d(253): Error: array operation `[1] * 6` without destination memory not allowed
205 fail_compilation/fail_arrayop2.d(256): Error: array operation `[1] * 6` without destination memory not allowed
206 fail_compilation/fail_arrayop2.d(265): Error: array operation `[1] * 6` without destination memory not allowed
207 fail_compilation/fail_arrayop2.d(268): Error: array operation `[1] * 6` without destination memory not allowed
208 fail_compilation/fail_arrayop2.d(269): Error: array operation `"abc"[] + '\x01'` without destination memory not allowed
209 fail_compilation/fail_arrayop2.d(272): Error: array operation `[1] * 6` without destination memory not allowed
210 fail_compilation/fail_arrayop2.d(275): Error: cannot take address of expression `([1] * 6)[0..2]` because it is not an lvalue
211 fail_compilation/fail_arrayop2.d(278): Error: can only `*` a pointer, not a `int[]`
212 fail_compilation/fail_arrayop2.d(284): Error: array operation `da[] * 6` without destination memory not allowed
213 fail_compilation/fail_arrayop2.d(287): Error: array operation `da[] * 6` without destination memory not allowed
214 fail_compilation/fail_arrayop2.d(290): Error: cannot modify expression `[1] * 6` because it is not an lvalue
215 fail_compilation/fail_arrayop2.d(291): Error: array operation `[1] * 6` without destination memory not allowed
216 fail_compilation/fail_arrayop2.d(294): Error: cannot modify expression `[1] * 6` because it is not an lvalue
217 fail_compilation/fail_arrayop2.d(295): Error: cannot modify expression `([1] * 6)[]` because it is not an lvalue
218 fail_compilation/fail_arrayop2.d(298): Error: array operation `[1] * 6` without destination memory not allowed
219 fail_compilation/fail_arrayop2.d(299): Error: array operation `[1] * 6` without destination memory not allowed
220 fail_compilation/fail_arrayop2.d(300): Error: array operation `[1] * 6` without destination memory not allowed
221 fail_compilation/fail_arrayop2.d(303): Error: cannot modify expression `[1] * 6` because it is not an lvalue
222 fail_compilation/fail_arrayop2.d(304): Error: cannot modify expression `[1] * 6` because it is not an lvalue
223 fail_compilation/fail_arrayop2.d(307): Error: `[1] * 6` is not of integral type, it is a `int[]`
224 fail_compilation/fail_arrayop2.d(308): Error: `[1] * 6` is not of integral type, it is a `int[]`
225 fail_compilation/fail_arrayop2.d(309): Error: `[1] * 6` is not of integral type, it is a `int[]`
226 fail_compilation/fail_arrayop2.d(312): Error: array operation `[1] * 6` without destination memory not allowed
227 fail_compilation/fail_arrayop2.d(313): Error: array operation `[1] * 6` without destination memory not allowed
228 fail_compilation/fail_arrayop2.d(316): Error: array operation `[1] * 6` without destination memory not allowed
229 fail_compilation/fail_arrayop2.d(317): Error: array operation `[1] * 6` without destination memory not allowed
230 fail_compilation/fail_arrayop2.d(318): Error: array operation `[1] * 6` without destination memory not allowed
231 fail_compilation/fail_arrayop2.d(321): Error: array operation `[1] * 6` without destination memory not allowed
232 fail_compilation/fail_arrayop2.d(321): Error: array operation `[1] * 6` without destination memory not allowed
233 fail_compilation/fail_arrayop2.d(321): Error: array operation `[1] * 6` without destination memory not allowed
234 ---
235 */
236
237
238 // Test all expressions, which can take arrays as their operands but cannot be a part of array operation.
239 void test15407exp()
240 {
241 struct S { int[] a; }
242 void f(int[] a) {}
243
244 int[] da;
245 int[6] sa;
246
247 { auto r = [[1] * 6]; } // ArrayLiteralExp
248 { auto r = [[1] * 6 :
249 [1] * 6]; } // AssocArrayLiteralExp
250
251 //TupleExp
252 // StructLiteralExp.elements <- preFunctionParameters in CallExp
253 { auto r = S([1] * 6); }
254
255 // NewExp.arguments <- preFunctionParameters
256 { auto r = new S([1] * 6); }
257
258 // TODO: TypeidExp
259 //auto ti = typeid([1] * 6);
260 //auto foo(T)(T t) {}
261 //foo(typeid([1] * 6));
262 //auto a = [typeid([1] * 6)];
263
264 // CommaExp.e1
265 { auto r = ([1] * 6, 1); }
266
267 // AssertExp
268 assert([1] * 6,
269 cast(char)1 + "abc"[]);
270
271 // CallExp.arguments <- preFunctionParameters
272 f([1] * 6);
273
274 // AddrExp, if a CT-known length slice can become an TypeSarray lvalue in the future.
275 { auto r = &(([1] * 6)[0..2]); }
276
277 // PtrExp, *([1] * 6).ptr is also invalid -> show better diagnostic
278 { auto r = *([1] * 6); }
279
280
281
282
283 // TypeDArray.dotExp, cannot check in ArrayLengthExp.semantic()
284 { auto r = (6 * da[]).length; }
285
286 // IndexExp - e1
287 { auto x1 = (da[] * 6)[1]; }
288
289 // Pre, PostExp - e1
290 ([1] * 6)++;
291 --([1] * 6);
292
293 // AssignExp e1
294 ([1] * 6) = 10;
295 ([1] * 6)[] = 10;
296
297 // BinAssignExp e1
298 ([1] * 6) += 1;
299 ([1] * 6)[] *= 2;
300 ([1] * 6)[] ^^= 3;
301
302 // CatExp e1
303 ([1] * 6) ~= 1;
304 ([1] * 6)[] ~= 2;
305
306 // Shl, Shr, UshrExp - e1, e2 --> checkIntegralBin
307 { auto r = ([1] * 6) << 1; }
308 { auto r = ([1] * 6) >> 1; }
309 { auto r = ([1] * 6) >>> 1; }
310
311 // AndAnd, OrOrExp - e1, e2
312 { auto r = sa[0..5] && [1] * 6; }
313 { auto r = sa[0..5] || [1] * 6; }
314
315 // Cmp, Equal, IdentityExp - e1, e2
316 { auto r = sa[0..5] <= [1] * 6; }
317 { auto r = sa[0..5] == [1] * 6; }
318 { auto r = sa[0..5] is [1] * 6; }
319
320 // CondExp - econd, e1, e2
321 { auto r = [1] * 6 ? [1] * 6 : [1] * 6; }
322 }
323
324 /* TEST_OUTPUT:
325 ---
326 fail_compilation/fail_arrayop2.d(342): Error: array operation `[1] * 6` without destination memory not allowed
327 fail_compilation/fail_arrayop2.d(345): Error: array operation `[1] * 6` without destination memory not allowed
328 fail_compilation/fail_arrayop2.d(348): Error: array operation `[1] * 6` without destination memory not allowed
329 fail_compilation/fail_arrayop2.d(349): Error: array operation `[1] * 6` without destination memory not allowed
330 fail_compilation/fail_arrayop2.d(350): Deprecation: `[1] * 6` has no effect
331 fail_compilation/fail_arrayop2.d(350): Error: array operation `[1] * 6` without destination memory not allowed
332 fail_compilation/fail_arrayop2.d(353): Error: array operation `[1] * 6` without destination memory not allowed
333 fail_compilation/fail_arrayop2.d(356): Error: array operation `[1] * 6` without destination memory not allowed
334 fail_compilation/fail_arrayop2.d(359): Error: array operation `"str"[] + cast(immutable(char))1` without destination memory not allowed
335 fail_compilation/fail_arrayop2.d(367): Error: CTFE internal error: non-constant value `"uvt"`
336 fail_compilation/fail_arrayop2.d(367): Error: `"uvt"[] - '\x01'` cannot be interpreted at compile time
337 ---
338 */
339 // Test all statements, which can take arrays as their operands.
340 void test15407stmt() {
341 // ExpStatement - exp
342 [1] * 6;
343
344 // DoStatement - condition
345 do {} while ([1] * 6);
346
347 // ForStatement - condition, increment
348 for ([1] * 6; // init == ExpStatement
349 [1] * 6;
350 [1] * 6) {}
351
352 // ForeachStatement - aggr -> lowered to ForStatement
353 foreach (e; [1] * 6) {}
354
355 // IfStatement condition
356 if ([1] * 6) {}
357
358 // SwitchStatement - condition
359 switch ("str"[] + 1)
360 {
361 case "tus": break;
362 default: break;
363 }
364 // CaseStatement - exp
365 switch ("tus")
366 {
367 case "uvt"[] - 1: break;
368 default: break;
369 }
370 }