1 // REQUIRED_ARGS: -o-
2 
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 */
test3133()9 void test3133()
10 {
11     short[2] x = [1, 2];
12     auto y = cast(int[2])x;     // error
13 }
14 
15 /*
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 ---
23 */
test9904()24 void test9904()
25 {
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; }
32 }
33 
34 /*
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 ---
42 */
test10646()43 void test10646()
44 {
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; }
51 }
52 
53 /*
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 ---
65 */
test11484()66 void test11484()
67 {
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; }
73 
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; }
79 }
80 
81 /*
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 ---
89 */
90 
test11485()91 void test11485()
92 {
93     class C {}
94     interface I {}
95 
96     // 11485 TypeBasic --> Tclass
97     { int x; auto y = cast(C)x; }
98     { int x; auto y = cast(I)x; }
99 
100     //  7472 TypeBasic <-- Tclass
101     { C x; auto y = cast(int)x; }
102     { I x; auto y = cast(int)x; }
103 }
104 
105 /*
106 TEST_OUTPUT:
107 ---
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)`
110 ---
111 */
test8179()112 void test8179()
113 {
114     { typeof(null) x; auto y = cast(int[2])x; }
115     { int[2] x;       auto y = cast(typeof(null))x; }
116 }
117 
118 /*
119 TEST_OUTPUT:
120 ---
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`
123 ---
124 */
test13959()125 void test13959()
126 {
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; }
131 }
132 
133 /*
134 TEST_OUTPUT:
135 ---
136 fail_compilation/fail_casting.d(144): Error: cannot cast expression `mi.x` of type `int` to `MyUbyte14154`
137 ---
138 */
139 struct MyUbyte14154 { ubyte x; alias x this; }
140 struct MyInt14154   {   int x; alias x this; }
141 void test14154()
142 {
143     MyInt14154 mi;
144     ubyte t = cast(MyUbyte14154)mi;
145 }
146 
147 /*
148 TEST_OUTPUT:
149 ---
150 fail_compilation/fail_casting.d(179): Error: cannot cast expression `__tup$n$.__expand_field_0` of type `int` to `object.Object`
151 fail_compilation/fail_casting.d(179): Error: cannot cast expression `__tup$n$.__expand_field_1` of type `int` to `object.Object`
152 ---
153 */
154 alias TypeTuple14093(T...) = T;
155 struct Tuple14093(T...)
156 {
157     static if (T.length == 4)
158     {
159         alias Types = TypeTuple14093!(T[0], T[2]);
160 
161         Types expand;
162 
163         @property ref inout(Tuple14093!Types) _Tuple_super() inout @trusted
164         {
165             return *cast(typeof(return)*) &(expand[0]);
166         }
167         alias _Tuple_super this;
168     }
169     else
170     {
171         alias Types = T;
172         Types expand;
173         alias expand this;
174     }
175 }
176 void test14093()
177 {
178     Tuple14093!(int, "x", int, "y") point;
179     auto newPoint = cast(Object)(point);
180 }
181 
182 /*
183 TEST_OUTPUT:
184 ---
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]`
187 ---
188 */
189 void test14596()
190 {
191     void* p = null;
192     auto arr = cast(char[])p;
193     char[2] sarr = cast(char[2])p;
194 }
195 
196 /*
197 TEST_OUTPUT:
198 ---
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)`
205 ---
206 */
207 void test14629()
208 {
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);
216 
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; }
223 }
224