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_casting.d
blob88d579b4dffe5ff0737d8d7847dc6792ec22f9cb
1 // REQUIRED_ARGS: -o-
3 /*
4 TEST_OUTPUT:
5 ---
6 fail_compilation/fail_casting.d(12): Error: cannot cast expression `x` of type `short[2]` to `int[2]` because of different sizes
7 ---
8 */
9 void test3133()
11 short[2] x = [1, 2];
12 auto y = cast(int[2])x; // error
16 TEST_OUTPUT:
17 ---
18 fail_compilation/fail_casting.d(28): Error: cannot cast expression `null` of type `typeof(null)` to `S1`
19 fail_compilation/fail_casting.d(29): Error: cannot cast expression `null` of type `typeof(null)` to `S2`
20 fail_compilation/fail_casting.d(30): Error: cannot cast expression `s1` of type `S1` to `typeof(null)`
21 fail_compilation/fail_casting.d(31): Error: cannot cast expression `s2` of type `S2` to `typeof(null)`
22 ---
24 void test9904()
26 static struct S1 { size_t m; }
27 static struct S2 { size_t m; byte b; }
28 { auto x = cast(S1)null; }
29 { auto x = cast(S2)null; }
30 { S1 s1; auto x = cast(typeof(null))s1; }
31 { S2 s2; auto x = cast(typeof(null))s2; }
35 TEST_OUTPUT:
36 ---
37 fail_compilation/fail_casting.d(46): Error: cannot cast expression `x` of type `Object[]` to `object.Object`
38 fail_compilation/fail_casting.d(47): Error: cannot cast expression `x` of type `Object[2]` to `object.Object`
39 fail_compilation/fail_casting.d(49): Error: cannot cast expression `x` of type `object.Object` to `Object[]`
40 fail_compilation/fail_casting.d(50): Error: cannot cast expression `x` of type `object.Object` to `Object[2]`
41 ---
43 void test10646()
45 // T[] or T[n] --> Tclass
46 { Object[] x; auto y = cast(Object)x; }
47 { Object[2] x; auto y = cast(Object)x; }
48 // T[] or T[n] <-- Tclass
49 { Object x; auto y = cast(Object[] )x; }
50 { Object x; auto y = cast(Object[2])x; }
54 TEST_OUTPUT:
55 ---
56 fail_compilation/fail_casting.d(69): Error: cannot cast expression `x` of type `int[1]` to `int`
57 fail_compilation/fail_casting.d(70): Error: cannot cast expression `x` of type `int` to `int[1]`
58 fail_compilation/fail_casting.d(71): Error: cannot cast expression `x` of type `float[1]` to `int`
59 fail_compilation/fail_casting.d(72): Error: cannot cast expression `x` of type `int` to `float[1]`
60 fail_compilation/fail_casting.d(75): Error: cannot cast expression `x` of type `int[]` to `int`
61 fail_compilation/fail_casting.d(76): Error: cannot cast expression `x` of type `int` to `int[]`
62 fail_compilation/fail_casting.d(77): Error: cannot cast expression `x` of type `float[]` to `int`
63 fail_compilation/fail_casting.d(78): Error: cannot cast expression `x` of type `int` to `float[]`
64 ---
66 void test11484()
68 // Tsarray <--> integer
69 { int[1] x; auto y = cast(int ) x; }
70 { int x; auto y = cast(int[1] ) x; }
71 { float[1] x; auto y = cast(int ) x; }
72 { int x; auto y = cast(float[1]) x; }
74 // Tarray <--> integer
75 { int[] x; auto y = cast(int ) x; }
76 { int x; auto y = cast(int[] ) x; }
77 { float[] x; auto y = cast(int ) x; }
78 { int x; auto y = cast(float[]) x; }
82 TEST_OUTPUT:
83 ---
84 fail_compilation/fail_casting.d(97): Error: cannot cast expression `x` of type `int` to `fail_casting.test11485.C`
85 fail_compilation/fail_casting.d(98): Error: cannot cast expression `x` of type `int` to `fail_casting.test11485.I`
86 fail_compilation/fail_casting.d(101): Error: cannot cast expression `x` of type `fail_casting.test11485.C` to `int`
87 fail_compilation/fail_casting.d(102): Error: cannot cast expression `x` of type `fail_casting.test11485.I` to `int`
88 ---
91 void test11485()
93 class C {}
94 interface I {}
96 // https://issues.dlang.org/show_bug.cgi?id=11485 TypeBasic --> Tclass
97 { int x; auto y = cast(C)x; }
98 { int x; auto y = cast(I)x; }
100 // https://issues.dlang.org/show_bug.cgi?id=7472 TypeBasic <-- Tclass
101 { C x; auto y = cast(int)x; }
102 { I x; auto y = cast(int)x; }
106 TEST_OUTPUT:
108 fail_compilation/fail_casting.d(114): Error: cannot cast expression `x` of type `typeof(null)` to `int[2]`
109 fail_compilation/fail_casting.d(115): Error: cannot cast expression `x` of type `int[2]` to `typeof(null)`
112 void test8179()
114 { typeof(null) x; auto y = cast(int[2])x; }
115 { int[2] x; auto y = cast(typeof(null))x; }
119 TEST_OUTPUT:
121 fail_compilation/fail_casting.d(128): Error: cannot cast expression `x` of type `S` to `int*`
122 fail_compilation/fail_casting.d(130): Error: cannot cast expression `x` of type `void*` to `S`
125 void test13959()
127 struct S { int* p; }
128 { S x; auto y = cast(int*)x; }
129 { int* x; auto y = cast(S)x; } // no error so it's rewritten as: S(x)
130 { void* x; auto y = cast(S)x; }
134 TEST_OUTPUT:
136 fail_compilation/fail_casting.d(144): Error: cannot cast expression `mi` of type `MyInt14154` to `MyUbyte14154` because of different sizes
139 struct MyUbyte14154 { ubyte x; alias x this; }
140 struct MyInt14154 { int x; alias x this; }
141 void test14154()
143 MyInt14154 mi;
144 ubyte t = cast(MyUbyte14154)mi;
148 TEST_OUTPUT:
150 fail_compilation/fail_casting.d(179): Error: cannot cast expression `point` of type `Tuple14093!(int, "x", int, "y")` to `object.Object`
154 alias TypeTuple14093(T...) = T;
155 struct Tuple14093(T...)
157 static if (T.length == 4)
159 alias Types = TypeTuple14093!(T[0], T[2]);
161 Types expand;
163 @property ref inout(Tuple14093!Types) _Tuple_super() inout @trusted
165 return *cast(typeof(return)*) &(expand[0]);
167 alias _Tuple_super this;
169 else
171 alias Types = T;
172 Types expand;
173 alias expand this;
176 void test14093()
178 Tuple14093!(int, "x", int, "y") point;
179 auto newPoint = cast(Object)(point);
183 TEST_OUTPUT:
185 fail_compilation/fail_casting.d(192): Error: cannot cast expression `p` of type `void*` to `char[]`
186 fail_compilation/fail_casting.d(193): Error: cannot cast expression `p` of type `void*` to `char[2]`
189 void test14596()
191 void* p = null;
192 auto arr = cast(char[])p;
193 char[2] sarr = cast(char[2])p;
197 TEST_OUTPUT:
199 fail_compilation/fail_casting.d(217): Error: cannot cast expression `c` of type `fail_casting.test14629.C` to `typeof(null)`
200 fail_compilation/fail_casting.d(218): Error: cannot cast expression `p` of type `int*` to `typeof(null)`
201 fail_compilation/fail_casting.d(219): Error: cannot cast expression `da` of type `int[]` to `typeof(null)`
202 fail_compilation/fail_casting.d(220): Error: cannot cast expression `aa` of type `int[int]` to `typeof(null)`
203 fail_compilation/fail_casting.d(221): Error: cannot cast expression `fp` of type `int function()` to `typeof(null)`
204 fail_compilation/fail_casting.d(222): Error: cannot cast expression `dg` of type `int delegate()` to `typeof(null)`
207 void test14629()
209 alias P = int*; P p;
210 alias DA = int[]; DA da;
211 alias AA = int[int]; AA aa;
212 alias FP = int function(); FP fp;
213 alias DG = int delegate(); DG dg;
214 class C {} C c;
215 alias N = typeof(null);
217 { auto x = cast(N)c; }
218 { auto x = cast(N)p; }
219 { auto x = cast(N)da; }
220 { auto x = cast(N)aa; }
221 { auto x = cast(N)fp; }
222 { auto x = cast(N)dg; }