1 // REQUIRED_SRGS: -o-
2 
3 
4 // references
5 alias P = int*;             P p;
6 alias FP = int function();  FP fp;
7 alias DG = int delegate();  DG dg;
8 alias DA = int[];           DA da;
9 alias AA = int[int];        AA aa;
10 class C {}                  C c;
11 alias N = typeof(null);     N n;
12 
13 // values
14 alias SA = int[1];          SA sa;
15 struct S {}                 S s;
16                             int i;
17                             double f;
18 
19 
20 /*
21 TEST_OUTPUT:
22 ---
23 fail_compilation/fail_casting1.d(39): Error: cannot cast expression `p` of type `int*` to `int[1]`
24 fail_compilation/fail_casting1.d(40): Error: cannot cast expression `fp` of type `int function()` to `int[1]`
25 fail_compilation/fail_casting1.d(41): Error: cannot cast expression `dg` of type `int delegate()` to `int[1]`
26 fail_compilation/fail_casting1.d(42): Error: cannot cast expression `da` of type `int[]` to `int[1]`
27 fail_compilation/fail_casting1.d(43): Error: cannot cast expression `aa` of type `int[int]` to `int[1]`
28 fail_compilation/fail_casting1.d(44): Error: cannot cast expression `c` of type `fail_casting1.C` to `int[1]`
29 fail_compilation/fail_casting1.d(45): Error: cannot cast expression `n` of type `typeof(null)` to `int[1]`
30 fail_compilation/fail_casting1.d(49): Error: cannot cast expression `sa` of type `int[1]` to `int delegate()`
31 fail_compilation/fail_casting1.d(51): Error: cannot cast expression `sa` of type `int[1]` to `double[]` since sizes don't line up
32 fail_compilation/fail_casting1.d(52): Error: cannot cast expression `sa` of type `int[1]` to `int[int]`
33 fail_compilation/fail_casting1.d(53): Error: cannot cast expression `sa` of type `int[1]` to `fail_casting1.C`
34 fail_compilation/fail_casting1.d(54): Error: cannot cast expression `sa` of type `int[1]` to `typeof(null)`
35 ---
36 */
test1()37 void test1()
38 {
39     { auto x = cast(SA) p; }        // Reject (Bugzilla 14596)
40     { auto x = cast(SA)fp; }        // Reject (Bugzilla 14596) (FP is Tpointer)
41     { auto x = cast(SA)dg; }        // Reject (from e2ir)
42     { auto x = cast(SA)da; }        // Reject (from e2ir)
43     { auto x = cast(SA)aa; }        // Reject (from e2ir)
44     { auto x = cast(SA) c; }        // Reject (Bugzilla 10646)
45     { auto x = cast(SA) n; }        // Reject (Bugzilla 8179)
46     { auto x = cast( P)sa; }        // Accept (equivalent with: cast(int*)sa.ptr;)
47     { auto x = cast(double*)sa; }   // Accept (equivalent with: cast(double*)sa.ptr;)
48     { auto x = cast(FP)sa; }        // Accept (equivalent with: cast(FP)sa.ptr;)
49     { auto x = cast(DG)sa; }        // Reject (from e2ir)
50     { auto x = cast(DA)sa; }        // Accept (equivalent with: cast(int[])sa[];)
51     { auto x = cast(double[])sa; }  // Reject (from e2ir)
52     { auto x = cast(AA)sa; }        // Reject (from e2ir)
53     { auto x = cast( C)sa; }        // Reject (Bugzilla 10646)
54     { auto x = cast( N)sa; }        // Reject (Bugzilla 8179)
55 }
56 
57 /*
58 TEST_OUTPUT:
59 ---
60 fail_compilation/fail_casting1.d(78): Error: cannot cast expression `p` of type `int*` to `S`
61 fail_compilation/fail_casting1.d(79): Error: cannot cast expression `fp` of type `int function()` to `S`
62 fail_compilation/fail_casting1.d(80): Error: cannot cast expression `dg` of type `int delegate()` to `S`
63 fail_compilation/fail_casting1.d(81): Error: cannot cast expression `da` of type `int[]` to `S`
64 fail_compilation/fail_casting1.d(82): Error: cannot cast expression `aa` of type `int[int]` to `S`
65 fail_compilation/fail_casting1.d(83): Error: cannot cast expression `c` of type `fail_casting1.C` to `S`
66 fail_compilation/fail_casting1.d(84): Error: cannot cast expression `n` of type `typeof(null)` to `S`
67 fail_compilation/fail_casting1.d(85): Error: cannot cast expression `s` of type `S` to `int*`
68 fail_compilation/fail_casting1.d(86): Error: cannot cast expression `s` of type `S` to `int function()`
69 fail_compilation/fail_casting1.d(87): Error: cannot cast expression `s` of type `S` to `int delegate()`
70 fail_compilation/fail_casting1.d(88): Error: cannot cast expression `s` of type `S` to `int[]`
71 fail_compilation/fail_casting1.d(89): Error: cannot cast expression `s` of type `S` to `int[int]`
72 fail_compilation/fail_casting1.d(90): Error: cannot cast expression `s` of type `S` to `fail_casting1.C`
73 fail_compilation/fail_casting1.d(91): Error: cannot cast expression `s` of type `S` to `typeof(null)`
74 ---
75 */
test2()76 void test2()
77 {
78     { auto x = cast( S) p; }        // Reject (Bugzilla 13959)
79     { auto x = cast( S)fp; }        // Reject (Bugzilla 13959) (FP is Tpointer)
80     { auto x = cast( S)dg; }        // Reject (from e2ir)
81     { auto x = cast( S)da; }        // Reject (from e2ir)
82     { auto x = cast( S)aa; }        // Reject (from e2ir)
83     { auto x = cast( S) c; }        // Reject (from e2ir)
84     { auto x = cast( S) n; }        // Reject (Bugzilla 9904)
85     { auto x = cast( P) s; }        // Reject (Bugzilla 13959)
86     { auto x = cast(FP) s; }        // Reject (Bugzilla 13959) (FP is Tpointer)
87     { auto x = cast(DG) s; }        // Reject (from e2ir)
88     { auto x = cast(DA) s; }        // Reject (from e2ir)
89     { auto x = cast(AA) s; }        // Reject (from e2ir)
90     { auto x = cast( C) s; }        // Reject (from e2ir)
91     { auto x = cast( N) s; }        // Reject (Bugzilla 9904)
92 }
93 
94 /*
95 TEST_OUTPUT:
96 ---
97 fail_compilation/fail_casting1.d(125): Error: cannot cast expression `p` of type `int*` to `int delegate()`
98 fail_compilation/fail_casting1.d(126): Error: cannot cast expression `p` of type `int*` to `int[]`
99 fail_compilation/fail_casting1.d(129): Error: cannot cast expression `p` of type `int*` to `typeof(null)`
100 fail_compilation/fail_casting1.d(133): Error: cannot cast expression `fp` of type `int function()` to `int delegate()`
101 fail_compilation/fail_casting1.d(134): Error: cannot cast expression `fp` of type `int function()` to `int[]`
102 fail_compilation/fail_casting1.d(137): Error: cannot cast expression `fp` of type `int function()` to `typeof(null)`
103 fail_compilation/fail_casting1.d(139): Deprecation: casting from int delegate() to int* is deprecated
104 fail_compilation/fail_casting1.d(140): Deprecation: casting from int delegate() to int function() is deprecated
105 fail_compilation/fail_casting1.d(142): Error: cannot cast expression `dg` of type `int delegate()` to `int[]`
106 fail_compilation/fail_casting1.d(143): Error: cannot cast expression `dg` of type `int delegate()` to `int[int]`
107 fail_compilation/fail_casting1.d(144): Error: cannot cast expression `dg` of type `int delegate()` to `fail_casting1.C`
108 fail_compilation/fail_casting1.d(145): Error: cannot cast expression `dg` of type `int delegate()` to `typeof(null)`
109 fail_compilation/fail_casting1.d(157): Error: cannot cast expression `da` of type `int[]` to `int delegate()`
110 fail_compilation/fail_casting1.d(159): Error: cannot cast expression `da` of type `int[]` to `int[int]`
111 fail_compilation/fail_casting1.d(160): Error: cannot cast expression `da` of type `int[]` to `fail_casting1.C`
112 fail_compilation/fail_casting1.d(161): Error: cannot cast expression `da` of type `int[]` to `typeof(null)`
113 fail_compilation/fail_casting1.d(165): Error: cannot cast expression `aa` of type `int[int]` to `int delegate()`
114 fail_compilation/fail_casting1.d(166): Error: cannot cast expression `aa` of type `int[int]` to `int[]`
115 fail_compilation/fail_casting1.d(169): Error: cannot cast expression `aa` of type `int[int]` to `typeof(null)`
116 fail_compilation/fail_casting1.d(173): Error: cannot cast expression `c` of type `fail_casting1.C` to `int delegate()`
117 fail_compilation/fail_casting1.d(174): Error: cannot cast expression `c` of type `fail_casting1.C` to `int[]`
118 fail_compilation/fail_casting1.d(177): Error: cannot cast expression `c` of type `fail_casting1.C` to `typeof(null)`
119 ---
120 */
test3()121 void test3()    // between reference types
122 {
123     { auto x = cast( P) p; }    // Accept
124     { auto x = cast(FP) p; }    // Accept (FP is Tpointer)
125     { auto x = cast(DG) p; }    // Reject (from e2ir)
126     { auto x = cast(DA) p; }    // Reject (Bugzilla 14596)
127     { auto x = cast(AA) p; }    // Accept (because of size match)
128     { auto x = cast( C) p; }    // Accept (because of size match)
129     { auto x = cast( N) p; }    // Reject (Bugzilla 14629)
130 
131     { auto x = cast( P)fp; }    // Accept (FP is Tpointer)
132     { auto x = cast(FP)fp; }    // Accept
133     { auto x = cast(DG)fp; }    // Reject (from e2ir)
134     { auto x = cast(DA)fp; }    // Reject (Bugzilla 14596)
135     { auto x = cast(AA)fp; }    // Accept (because of size match)
136     { auto x = cast( C)fp; }    // Accept (because of size match)
137     { auto x = cast( N)fp; }    // Reject (Bugzilla 14629)
138 
139     { auto x = cast( P)dg; }    // Deprecated (equivalent with: cast( P)dg.ptr;)
140     { auto x = cast(FP)dg; }    // Deprecated (equivalent with: cast(FP)dg.ptr;)
141     { auto x = cast(DG)dg; }    // Accept
142     { auto x = cast(DA)dg; }    // Reject (from e2ir)
143     { auto x = cast(AA)dg; }    // Reject (from e2ir)
144     { auto x = cast( C)dg; }    // Reject (from e2ir)
145     { auto x = cast( N)dg; }    // Reject (Bugzilla 14629)
146 
147     { auto x = cast( P) n; }    // Accept
148     { auto x = cast(FP) n; }    // Accept
149     { auto x = cast(DG) n; }    // Accept
150     { auto x = cast(DA) n; }    // Accept
151     { auto x = cast(AA) n; }    // Accept
152     { auto x = cast( C) n; }    // Accept
153     { auto x = cast( N) n; }    // Accept
154 
155     { auto x = cast( P)da; }    // Accept (equivalent with: cast(P)da.ptr;)
156     { auto x = cast(FP)da; }    // Accept (FP is Tpointer)
157     { auto x = cast(DG)da; }    // Reject (from e2ir)
158     { auto x = cast(DA)da; }    // Accept
159     { auto x = cast(AA)da; }    // Reject (from e2ir)
160     { auto x = cast( C)da; }    // Reject (Bugzilla 10646)
161     { auto x = cast( N)da; }    // Reject (Bugzilla 14629)
162 
163     { auto x = cast( P)aa; }    // Accept (because of size match)
164     { auto x = cast(FP)aa; }    // Accept (FP is Tpointer)
165     { auto x = cast(DG)aa; }    // Reject (from e2ir)
166     { auto x = cast(DA)aa; }    // Reject (from e2ir)
167     { auto x = cast(AA)aa; }    // Accept
168     { auto x = cast( C)aa; }    // Accept (because of size match)
169     { auto x = cast( N)aa; }    // Reject (Bugzilla 14629)
170 
171     { auto x = cast( P) c; }    // Accept
172     { auto x = cast(FP) c; }    // Accept (FP is Tpointer)
173     { auto x = cast(DG) c; }    // Reject (from e2ir)
174     { auto x = cast(DA) c; }    // Reject (Bugzilla 10646)
175     { auto x = cast(AA) c; }    // Accept (because of size match)
176     { auto x = cast( C) c; }    // Accept
177     { auto x = cast( N) c; }    // Reject (Bugzilla 14629)
178 }
179 
180 /*
181 TEST_OUTPUT:
182 ---
183 fail_compilation/fail_casting1.d(206): Error: cannot cast expression `0` of type `int` to `int delegate()`
184 fail_compilation/fail_casting1.d(207): Error: cannot cast expression `0` of type `int` to `int[]`
185 fail_compilation/fail_casting1.d(208): Error: cannot cast expression `0` of type `int` to `int[1]`
186 fail_compilation/fail_casting1.d(209): Error: cannot cast expression `0` of type `int` to `int[int]`
187 fail_compilation/fail_casting1.d(210): Error: cannot cast expression `0` of type `int` to `fail_casting1.C`
188 fail_compilation/fail_casting1.d(211): Error: cannot cast expression `0` of type `int` to `typeof(null)`
189 fail_compilation/fail_casting1.d(215): Error: cannot cast expression `i` of type `int` to `int delegate()`
190 fail_compilation/fail_casting1.d(216): Error: cannot cast expression `i` of type `int` to `int[]`
191 fail_compilation/fail_casting1.d(217): Error: cannot cast expression `i` of type `int` to `int[1]`
192 fail_compilation/fail_casting1.d(218): Error: cannot cast expression `i` of type `int` to `int[int]`
193 fail_compilation/fail_casting1.d(219): Error: cannot cast expression `i` of type `int` to `fail_casting1.C`
194 fail_compilation/fail_casting1.d(220): Error: cannot cast expression `i` of type `int` to `typeof(null)`
195 fail_compilation/fail_casting1.d(224): Error: cannot cast expression `dg` of type `int delegate()` to `int`
196 fail_compilation/fail_casting1.d(225): Error: cannot cast expression `da` of type `int[]` to `int`
197 fail_compilation/fail_casting1.d(226): Error: cannot cast expression `sa` of type `int[1]` to `int`
198 fail_compilation/fail_casting1.d(227): Error: cannot cast expression `aa` of type `int[int]` to `int`
199 fail_compilation/fail_casting1.d(228): Error: cannot cast expression `c` of type `fail_casting1.C` to `int`
200 ---
201 */
test4()202 void test4()
203 {
204     { auto x = cast( P) 0; }    // Accept
205     { auto x = cast(FP) 0; }    // Accept
206     { auto x = cast(DG) 0; }    // Reject (from constfold)
207     { auto x = cast(DA) 0; }    // Reject (Bugzilla 11484)
208     { auto x = cast(SA) 0; }    // Reject (Bugzilla 11484)
209     { auto x = cast(AA) 0; }    // Reject (from constfold)
210     { auto x = cast( C) 0; }    // Reject (Bugzilla 11485)
211     { auto x = cast( N) 0; }    // Reject (from constfold)
212 
213     { auto x = cast( P) i; }    // Accept
214     { auto x = cast(FP) i; }    // Accept
215     { auto x = cast(DG) i; }    // Reject (from e2ir)
216     { auto x = cast(DA) i; }    // Reject (Bugzilla 11484)
217     { auto x = cast(SA) i; }    // Reject (Bugzilla 11484)
218     { auto x = cast(AA) i; }    // Reject (from e2ir)
219     { auto x = cast( C) i; }    // Reject (Bugzilla 11485)
220     { auto x = cast( N) i; }    // Reject (from e2ir)
221 
222     { auto x = cast(int) p; }   // Accept
223     { auto x = cast(int)fp; }   // Accept
224     { auto x = cast(int)dg; }   // Reject (from e2ir)
225     { auto x = cast(int)da; }   // Reject (Bugzilla 11484)
226     { auto x = cast(int)sa; }   // Reject (Bugzilla 11484)
227     { auto x = cast(int)aa; }   // Reject (from e2ir)
228     { auto x = cast(int) c; }   // Reject (Bugzilla 7472)
229     { auto x = cast(int) n; }   // Accept
230 }
231 
232 /*
233 TEST_OUTPUT:
234 ---
235 fail_compilation/fail_casting1.d(249): Error: cannot cast expression `0` of type `int` to `int[1]`
236 fail_compilation/fail_casting1.d(250): Error: cannot cast expression `0` of type `int` to `S`
237 fail_compilation/fail_casting1.d(251): Error: cannot cast expression `i` of type `int` to `int[1]`
238 fail_compilation/fail_casting1.d(252): Error: cannot cast expression `i` of type `int` to `S`
239 fail_compilation/fail_casting1.d(253): Error: cannot cast expression `f` of type `double` to `int[1]`
240 fail_compilation/fail_casting1.d(254): Error: cannot cast expression `f` of type `double` to `S`
241 fail_compilation/fail_casting1.d(255): Error: cannot cast expression `sa` of type `int[1]` to `int`
242 fail_compilation/fail_casting1.d(256): Error: cannot cast expression `s` of type `S` to `int`
243 fail_compilation/fail_casting1.d(257): Error: cannot cast expression `sa` of type `int[1]` to `double`
244 fail_compilation/fail_casting1.d(258): Error: cannot cast expression `s` of type `S` to `double`
245 ---
246 */
test5()247 void test5()
248 {
249     { auto x = cast(SA) 0; }        // Reject (Bugzilla 14154)
250     { auto x = cast( S) 0; }        // Reject (Bugzilla 14154)
251     { auto x = cast(SA) i; }        // Reject (Bugzilla 14154)
252     { auto x = cast( S) i; }        // Reject (Bugzilla 14154)
253     { auto x = cast(SA) f; }        // Reject (Bugzilla 14154)
254     { auto x = cast( S) f; }        // Reject (Bugzilla 14154)
255     { auto x = cast(int)sa; }       // Reject (Bugzilla 14154)
256     { auto x = cast(int) s; }       // Reject (Bugzilla 14154)
257     { auto x = cast(double)sa; }    // Reject (Bugzilla 14154)
258     { auto x = cast(double) s; }    // Reject (Bugzilla 14154)
259 }
260