/
fail_casting.d
194 lines (178 loc) · 5.62 KB
/
fail_casting.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// REQUIRED_ARGS: -o-
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(12): Error: cannot cast expression x of type short[2] to int[2]
---
*/
void test3133()
{
short[2] x = [1, 2];
auto y = cast(int[2])x; // error
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(28): Error: cannot cast expression null of type typeof(null) to S1
fail_compilation/fail_casting.d(29): Error: cannot cast expression null of type typeof(null) to S2
fail_compilation/fail_casting.d(30): Error: cannot cast expression s1 of type S1 to typeof(null)
fail_compilation/fail_casting.d(31): Error: cannot cast expression s2 of type S2 to typeof(null)
---
*/
void test9904()
{
static struct S1 { size_t m; }
static struct S2 { size_t m; byte b; }
{ auto x = cast(S1)null; }
{ auto x = cast(S2)null; }
{ S1 s1; auto x = cast(typeof(null))s1; }
{ S2 s2; auto x = cast(typeof(null))s2; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(46): Error: cannot cast expression x of type Object[] to object.Object
fail_compilation/fail_casting.d(47): Error: cannot cast expression x of type Object[2] to object.Object
fail_compilation/fail_casting.d(49): Error: cannot cast expression x of type object.Object to Object[]
fail_compilation/fail_casting.d(50): Error: cannot cast expression x of type object.Object to Object[2]
---
*/
void test10646()
{
// T[] or T[n] --> Tclass
{ Object[] x; auto y = cast(Object)x; }
{ Object[2] x; auto y = cast(Object)x; }
// T[] or T[n] <-- Tclass
{ Object x; auto y = cast(Object[] )x; }
{ Object x; auto y = cast(Object[2])x; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(69): Error: cannot cast expression x of type int[1] to int
fail_compilation/fail_casting.d(70): Error: cannot cast expression x of type int to int[1]
fail_compilation/fail_casting.d(71): Error: cannot cast expression x of type float[1] to int
fail_compilation/fail_casting.d(72): Error: cannot cast expression x of type int to float[1]
fail_compilation/fail_casting.d(75): Error: cannot cast expression x of type int[] to int
fail_compilation/fail_casting.d(76): Error: cannot cast expression x of type int to int[]
fail_compilation/fail_casting.d(77): Error: cannot cast expression x of type float[] to int
fail_compilation/fail_casting.d(78): Error: cannot cast expression x of type int to float[]
---
*/
void tst11484()
{
// Tsarray <--> integer
{ int[1] x; auto y = cast(int ) x; }
{ int x; auto y = cast(int[1] ) x; }
{ float[1] x; auto y = cast(int ) x; }
{ int x; auto y = cast(float[1]) x; }
// Tarray <--> integer
{ int[] x; auto y = cast(int ) x; }
{ int x; auto y = cast(int[] ) x; }
{ float[] x; auto y = cast(int ) x; }
{ int x; auto y = cast(float[]) x; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(97): Error: cannot cast expression x of type int to fail_casting.test11485.C
fail_compilation/fail_casting.d(98): Error: cannot cast expression x of type int to fail_casting.test11485.I
fail_compilation/fail_casting.d(101): Error: cannot cast expression x of type fail_casting.test11485.C to int
fail_compilation/fail_casting.d(102): Error: cannot cast expression x of type fail_casting.test11485.I to int
---
*/
void test11485()
{
class C {}
interface I {}
// 11485 TypeBasic --> Tclass
{ int x; auto y = cast(C)x; }
{ int x; auto y = cast(I)x; }
// 7472 TypeBasic <-- Tclass
{ C x; auto y = cast(int)x; }
{ I x; auto y = cast(int)x; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(114): Error: cannot cast expression x of type typeof(null) to int[2]
fail_compilation/fail_casting.d(115): Error: cannot cast expression x of type int[2] to typeof(null)
---
*/
void test8179()
{
{ typeof(null) x; auto y = cast(int[2])x; }
{ int[2] x; auto y = cast(typeof(null))x; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(128): Error: cannot cast expression x of type S to int*
fail_compilation/fail_casting.d(130): Error: cannot cast expression x of type void* to S
---
*/
void test13959()
{
struct S { int* p; }
{ S x; auto y = cast(int*)x; }
{ int* x; auto y = cast(S)x; } // no error so it's rewritten as: S(x)
{ void* x; auto y = cast(S)x; }
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(144): Error: cannot cast expression mi.x of type int to MyUbyte14154
---
*/
struct MyUbyte14154 { ubyte x; alias x this; }
struct MyInt14154 { int x; alias x this; }
void test14154()
{
MyInt14154 mi;
ubyte t = cast(MyUbyte14154)mi;
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(179): Error: cannot cast expression __tup$n$.__expand_field_0 of type int to object.Object
fail_compilation/fail_casting.d(179): Error: cannot cast expression __tup$n$.__expand_field_1 of type int to object.Object
---
*/
alias TypeTuple14093(T...) = T;
struct Tuple14093(T...)
{
static if (T.length == 4)
{
alias Types = TypeTuple14093!(T[0], T[2]);
Types expand;
@property ref inout(Tuple14093!Types) _Tuple_super() inout @trusted
{
return *cast(typeof(return)*) &(expand[0]);
}
alias _Tuple_super this;
}
else
{
alias Types = T;
Types expand;
alias expand this;
}
}
void test14093()
{
Tuple14093!(int, "x", int, "y") point;
auto newPoint = cast(Object)(point);
}
/*
TEST_OUTPUT:
---
fail_compilation/fail_casting.d(192): Error: cannot cast expression p of type void* to char[]
fail_compilation/fail_casting.d(193): Error: cannot cast expression p of type void* to char[2]
---
*/
void test14596()
{
void* p = null;
auto arr = cast(char[])p;
char[2] sarr = cast(char[2])p;
}