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