]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gdc.test/fail_compilation/fail_casting.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / fail_compilation / fail_casting.d
CommitLineData
b4c522fa
IB
1// REQUIRED_ARGS: -o-
2
3/*
4TEST_OUTPUT:
5---
6fail_compilation/fail_casting.d(12): Error: cannot cast expression `x` of type `short[2]` to `int[2]` because of different sizes
7---
8*/
9void test3133()
10{
11 short[2] x = [1, 2];
12 auto y = cast(int[2])x; // error
13}
14
15/*
16TEST_OUTPUT:
17---
18fail_compilation/fail_casting.d(28): Error: cannot cast expression `null` of type `typeof(null)` to `S1`
19fail_compilation/fail_casting.d(29): Error: cannot cast expression `null` of type `typeof(null)` to `S2`
20fail_compilation/fail_casting.d(30): Error: cannot cast expression `s1` of type `S1` to `typeof(null)`
21fail_compilation/fail_casting.d(31): Error: cannot cast expression `s2` of type `S2` to `typeof(null)`
22---
23*/
24void 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/*
35TEST_OUTPUT:
36---
37fail_compilation/fail_casting.d(46): Error: cannot cast expression `x` of type `Object[]` to `object.Object`
38fail_compilation/fail_casting.d(47): Error: cannot cast expression `x` of type `Object[2]` to `object.Object`
39fail_compilation/fail_casting.d(49): Error: cannot cast expression `x` of type `object.Object` to `Object[]`
40fail_compilation/fail_casting.d(50): Error: cannot cast expression `x` of type `object.Object` to `Object[2]`
41---
42*/
43void 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/*
54TEST_OUTPUT:
55---
56fail_compilation/fail_casting.d(69): Error: cannot cast expression `x` of type `int[1]` to `int`
57fail_compilation/fail_casting.d(70): Error: cannot cast expression `x` of type `int` to `int[1]`
58fail_compilation/fail_casting.d(71): Error: cannot cast expression `x` of type `float[1]` to `int`
59fail_compilation/fail_casting.d(72): Error: cannot cast expression `x` of type `int` to `float[1]`
60fail_compilation/fail_casting.d(75): Error: cannot cast expression `x` of type `int[]` to `int`
61fail_compilation/fail_casting.d(76): Error: cannot cast expression `x` of type `int` to `int[]`
62fail_compilation/fail_casting.d(77): Error: cannot cast expression `x` of type `float[]` to `int`
63fail_compilation/fail_casting.d(78): Error: cannot cast expression `x` of type `int` to `float[]`
64---
65*/
66void 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/*
82TEST_OUTPUT:
83---
84fail_compilation/fail_casting.d(97): Error: cannot cast expression `x` of type `int` to `fail_casting.test11485.C`
85fail_compilation/fail_casting.d(98): Error: cannot cast expression `x` of type `int` to `fail_casting.test11485.I`
86fail_compilation/fail_casting.d(101): Error: cannot cast expression `x` of type `fail_casting.test11485.C` to `int`
87fail_compilation/fail_casting.d(102): Error: cannot cast expression `x` of type `fail_casting.test11485.I` to `int`
88---
89*/
90
91void test11485()
92{
93 class C {}
94 interface I {}
95
5fee5ec3 96 // https://issues.dlang.org/show_bug.cgi?id=11485 TypeBasic --> Tclass
b4c522fa
IB
97 { int x; auto y = cast(C)x; }
98 { int x; auto y = cast(I)x; }
99
5fee5ec3 100 // https://issues.dlang.org/show_bug.cgi?id=7472 TypeBasic <-- Tclass
b4c522fa
IB
101 { C x; auto y = cast(int)x; }
102 { I x; auto y = cast(int)x; }
103}
104
105/*
106TEST_OUTPUT:
107---
108fail_compilation/fail_casting.d(114): Error: cannot cast expression `x` of type `typeof(null)` to `int[2]`
109fail_compilation/fail_casting.d(115): Error: cannot cast expression `x` of type `int[2]` to `typeof(null)`
110---
111*/
112void 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/*
119TEST_OUTPUT:
120---
121fail_compilation/fail_casting.d(128): Error: cannot cast expression `x` of type `S` to `int*`
122fail_compilation/fail_casting.d(130): Error: cannot cast expression `x` of type `void*` to `S`
123---
124*/
125void 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/*
134TEST_OUTPUT:
135---
5fee5ec3 136fail_compilation/fail_casting.d(144): Error: cannot cast expression `mi` of type `MyInt14154` to `MyUbyte14154` because of different sizes
b4c522fa
IB
137---
138*/
139struct MyUbyte14154 { ubyte x; alias x this; }
140struct MyInt14154 { int x; alias x this; }
141void test14154()
142{
143 MyInt14154 mi;
144 ubyte t = cast(MyUbyte14154)mi;
145}
146
147/*
148TEST_OUTPUT:
149---
5fee5ec3 150fail_compilation/fail_casting.d(179): Error: cannot cast expression `point` of type `Tuple14093!(int, "x", int, "y")` to `object.Object`
b4c522fa
IB
151---
152*/
5fee5ec3 153
b4c522fa
IB
154alias TypeTuple14093(T...) = T;
155struct 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}
176void test14093()
177{
178 Tuple14093!(int, "x", int, "y") point;
179 auto newPoint = cast(Object)(point);
180}
181
182/*
183TEST_OUTPUT:
184---
185fail_compilation/fail_casting.d(192): Error: cannot cast expression `p` of type `void*` to `char[]`
186fail_compilation/fail_casting.d(193): Error: cannot cast expression `p` of type `void*` to `char[2]`
187---
188*/
189void test14596()
190{
191 void* p = null;
192 auto arr = cast(char[])p;
193 char[2] sarr = cast(char[2])p;
194}
195
196/*
197TEST_OUTPUT:
198---
199fail_compilation/fail_casting.d(217): Error: cannot cast expression `c` of type `fail_casting.test14629.C` to `typeof(null)`
200fail_compilation/fail_casting.d(218): Error: cannot cast expression `p` of type `int*` to `typeof(null)`
201fail_compilation/fail_casting.d(219): Error: cannot cast expression `da` of type `int[]` to `typeof(null)`
202fail_compilation/fail_casting.d(220): Error: cannot cast expression `aa` of type `int[int]` to `typeof(null)`
203fail_compilation/fail_casting.d(221): Error: cannot cast expression `fp` of type `int function()` to `typeof(null)`
204fail_compilation/fail_casting.d(222): Error: cannot cast expression `dg` of type `int delegate()` to `typeof(null)`
205---
206*/
207void 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}