MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / testsuite / gdc.test / fail_compilation / fail_arrayop2.d
blobc3c735ed552f190e253c5d431874be7c66f814d5
1 // REQUIRED_ARGS: -o-
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 ---
11 void test2603() // https://issues.dlang.org/show_bug.cgi?id=2603 - ICE(cgcs.c) on subtracting string literals
13 auto c1 = [1,2,3] - [1,2,3];
15 // this variation is wrong code on D2, ICE ..\ztc\cgcs.c 358 on D1.
16 string c2 = "a" - "b";
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 ---
35 void test9459()
37 int[] a = [1, 2, 3];
38 a = -a[];
39 a = ~a[];
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[];
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 ---
70 void test12179()
72 void foo(int[]) {}
73 int[1] a;
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[]);
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
95 TEST_OUTPUT:
96 ---
97 fail_compilation/fail_arrayop2.d(105): Error: array operation `h * y[]` without destination memory not allowed
98 ---
100 void test12381()
102 double[2] y;
103 double h;
105 double[2] temp1 = cast(double[2])(h * y[]);
109 TEST_OUTPUT:
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
115 float[] test12769(float[] a)
117 if (a.length < 4)
118 return -a[];
119 else
120 return (-a[])[0..4];
124 TEST_OUTPUT:
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
133 void test13208()
135 int[] a;
137 auto arr = [a[] - a[]][0];
139 auto aa1 = [1 : a[] - a[]];
140 auto aa2 = [a[] - a[] : 1];
142 struct S { int[] a; }
143 auto s = S(a[] - a[]);
145 auto n = int(a[] - a[]);
149 TEST_OUTPUT:
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 [])
157 void test13497()
159 int[1] a;
160 auto b1 = (a[] * a[])[];
161 auto b2 = (a[] * a[])[0..1];
163 int[] c;
164 c = (a[] * a[])[];
165 c = (a[] * a[])[0..1];
169 TEST_OUTPUT:
171 fail_compilation/fail_arrayop2.d(181): Error: array operation `data[segmentId][28..29] & cast(ubyte)(1 << 0)` without destination memory not allowed
174 void test13910()
176 ubyte[][] data;
177 size_t segmentId;
179 bool isGroup()
181 return !!((data[segmentId][28..29]) & (1 << 0));
186 TEST_OUTPUT:
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
192 void test14895()
194 int[] a;
195 int[] b = (a[] + 1) ~ a[] * 2;
199 TEST_OUTPUT:
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: `([1] * 6)[0..2]` is not an lvalue and cannot be modified
211 fail_compilation/fail_arrayop2.d(278): Error: can only `*` a pointer, not a `int[]`
212 fail_compilation/fail_arrayop2.d(281): Error: the `delete` keyword is obsolete
213 fail_compilation/fail_arrayop2.d(281): use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead
214 fail_compilation/fail_arrayop2.d(284): Error: array operation `da[] * 6` without destination memory not allowed
215 fail_compilation/fail_arrayop2.d(287): Error: array operation `da[] * 6` without destination memory not allowed
216 fail_compilation/fail_arrayop2.d(290): Error: `[1] * 6` is not an lvalue and cannot be modified
217 fail_compilation/fail_arrayop2.d(291): Error: array operation `[1] * 6` without destination memory not allowed
218 fail_compilation/fail_arrayop2.d(294): Error: `[1] * 6` is not an lvalue and cannot be modified
219 fail_compilation/fail_arrayop2.d(295): Error: `([1] * 6)[]` is not an lvalue and cannot be modified
220 fail_compilation/fail_arrayop2.d(298): Error: array operation `[1] * 6` without destination memory not allowed
221 fail_compilation/fail_arrayop2.d(299): Error: array operation `[1] * 6` without destination memory not allowed
222 fail_compilation/fail_arrayop2.d(300): Error: array operation `[1] * 6` without destination memory not allowed
223 fail_compilation/fail_arrayop2.d(303): Error: `[1] * 6` is not an lvalue and cannot be modified
224 fail_compilation/fail_arrayop2.d(304): Error: `[1] * 6` is not an lvalue and cannot be modified
225 fail_compilation/fail_arrayop2.d(307): Error: `[1] * 6` is not of integral type, it is a `int[]`
226 fail_compilation/fail_arrayop2.d(308): Error: `[1] * 6` is not of integral type, it is a `int[]`
227 fail_compilation/fail_arrayop2.d(309): Error: `[1] * 6` is not of integral type, it is a `int[]`
228 fail_compilation/fail_arrayop2.d(312): Error: array operation `[1] * 6` without destination memory not allowed
229 fail_compilation/fail_arrayop2.d(313): Error: array operation `[1] * 6` without destination memory not allowed
230 fail_compilation/fail_arrayop2.d(316): Error: array operation `[1] * 6` without destination memory not allowed
231 fail_compilation/fail_arrayop2.d(317): Error: array operation `[1] * 6` without destination memory not allowed
232 fail_compilation/fail_arrayop2.d(318): 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 fail_compilation/fail_arrayop2.d(321): Error: array operation `[1] * 6` without destination memory not allowed
235 fail_compilation/fail_arrayop2.d(321): Error: array operation `[1] * 6` without destination memory not allowed
238 // Test all expressions, which can take arrays as their operands but cannot be a part of array operation.
239 void test15407exp()
241 struct S { int[] a; }
242 void f(int[] a) {}
244 int[] da;
245 int[6] sa;
247 { auto r = [[1] * 6]; } // ArrayLiteralExp
248 { auto r = [[1] * 6 :
249 [1] * 6]; } // AssocArrayLiteralExp
251 //TupleExp
252 // StructLiteralExp.elements <- preFunctionParameters in CallExp
253 { auto r = S([1] * 6); }
255 // NewExp.arguments <- preFunctionParameters
256 { auto r = new S([1] * 6); }
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)];
264 // CommaExp.e1
265 { auto r = ([1] * 6, 1); }
267 // AssertExp
268 assert([1] * 6,
269 cast(char)1 + "abc"[]);
271 // CallExp.arguments <- preFunctionParameters
272 f([1] * 6);
274 // AddrExp, if a CT-known length slice can become an TypeSarray lvalue in the future.
275 { auto r = &(([1] * 6)[0..2]); }
277 // PtrExp, *([1] * 6).ptr is also invalid -> show better diagnostic
278 { auto r = *([1] * 6); }
280 // DeleteExp - e1
281 delete ([1] * 6);
283 // TypeDArray.dotExp, cannot check in ArrayLengthExp.semantic()
284 { auto r = (6 * da[]).length; }
286 // IndexExp - e1
287 { auto x1 = (da[] * 6)[1]; }
289 // Pre, PostExp - e1
290 ([1] * 6)++;
291 --([1] * 6);
293 // AssignExp e1
294 ([1] * 6) = 10;
295 ([1] * 6)[] = 10;
297 // BinAssignExp e1
298 ([1] * 6) += 1;
299 ([1] * 6)[] *= 2;
300 ([1] * 6)[] ^^= 3;
302 // CatExp e1
303 ([1] * 6) ~= 1;
304 ([1] * 6)[] ~= 2;
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; }
311 // AndAnd, OrOrExp - e1, e2
312 { auto r = sa[0..5] && [1] * 6; }
313 { auto r = sa[0..5] || [1] * 6; }
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; }
320 // CondExp - econd, e1, e2
321 { auto r = [1] * 6 ? [1] * 6 : [1] * 6; }
324 /* TEST_OUTPUT:
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
339 // Test all statements, which can take arrays as their operands.
340 void test15407stmt() {
341 // ExpStatement - exp
342 [1] * 6;
344 // DoStatement - condition
345 do {} while ([1] * 6);
347 // ForStatement - condition, increment
348 for ([1] * 6; // init == ExpStatement
349 [1] * 6;
350 [1] * 6) {}
352 // ForeachStatement - aggr -> lowered to ForStatement
353 foreach (e; [1] * 6) {}
355 // IfStatement condition
356 if ([1] * 6) {}
358 // SwitchStatement - condition
359 switch ("str"[] + 1)
361 case "tus": break;
362 default: break;
364 // CaseStatement - exp
365 switch ("tus")
367 case "uvt"[] - 1: break;
368 default: break;