d: Merge upstream dmd, druntime 26f049fb26, phobos 330d6a4fd.
[official-gcc.git] / gcc / testsuite / gdc.test / compilable / noreturn3.d
blob2538a0d0231b30e6572d5e6d676c9d0117bc216d
1 /*
2 REQUIRED_ARGS: -w -o- -d
4 More complex examples from the DIP
5 https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md
6 */
8 alias noreturn = typeof(*null);
9 static assert (!is(noreturn == void));
11 void initialize()
13 noreturn a;
14 noreturn b = noreturn.init;
17 void foo(const noreturn);
18 void foo(const int);
20 noreturn bar();
22 void overloads()
24 noreturn n;
25 foo(n);
27 foo(bar());
30 // /*****************************************************************************/
32 auto inferNoreturn(int i)
34 if (i < 0)
35 return assert(false);
36 else if (i == 0)
37 return assert(false);
38 else
39 return assert(false);
42 auto inferReturn(int i)
44 if (i < 0)
45 return assert(false);
46 else if (i == 0)
47 return i;
48 else
49 return assert(false);
52 // /*****************************************************************************/
53 // // https://issues.dlang.org/show_bug.cgi?id=22004
55 alias fun22004 = _ => {}();
56 alias gun22004 = _ => assert(0);
57 auto bun22004(bool b)
59 if (b)
60 return gun22004(0);
61 else
62 return fun22004(0);
65 static assert(is(typeof(bun22004(true)) == void));
67 // // Reversed order
68 auto bun22004_reversed(bool b)
70 if (b)
71 return fun22004(0);
72 else
73 return gun22004(0);
76 static assert(is(typeof(bun22004_reversed(true)) == void));
78 // /*****************************************************************************/
80 // // Also works fine with non-void types and ref inference
82 int global;
84 auto ref forwardOrExit(ref int num)
86 if (num)
87 return num;
88 else
89 return assert(false);
92 static assert( is(typeof(forwardOrExit(global)) == int));
94 // Noreturn types do not affect `auto ref` deduction
95 static assert(is(typeof(&forwardOrExit(global))));
97 auto ref forwardOrExit2(ref int num)
99 if (num)
100 return assert(false);
101 else
102 return num;
105 static assert( is(typeof(forwardOrExit2(global)) == int));
107 // Noreturn types do not affect `auto ref` deduction
108 static assert(is(typeof(&forwardOrExit2(global))));
110 /*****************************************************************************/
112 void inference()
114 auto inf = cast(noreturn) 1;
115 static assert(is(typeof(inf) == noreturn));
117 noreturn n;
118 auto c = cast(const shared noreturn) n;
119 static assert(is(typeof(c) == const shared noreturn));
120 static assert(is(typeof(n) == noreturn));
122 auto c2 = cast(immutable noreturn) n;
123 static assert(is(typeof(c) == const shared noreturn));
124 static assert(is(typeof(c2) == immutable noreturn));
125 static assert(is(typeof(n) == noreturn));
129 /******************************************************************************/
130 // https://issues.dlang.org/show_bug.cgi?id=21957
131 // Calculate proper alignment and size for noreturn members
133 enum longPad = long.alignof - int.sizeof;
135 struct BasicStruct
137 int firstInt;
138 noreturn noRet;
139 long lastLong;
142 static assert(BasicStruct.sizeof == (int.sizeof + longPad + long.sizeof));
144 static assert(BasicStruct.firstInt.offsetof == 0);
145 static assert(BasicStruct.noRet.offsetof == 4);
146 static assert(BasicStruct.lastLong.offsetof == (4 + longPad));
148 struct AlignedStruct
150 int firstInt;
151 align(16) noreturn noRet;
152 long lastLong;
155 static assert(AlignedStruct.sizeof == 32);
157 static assert(AlignedStruct.firstInt.offsetof == 0);
158 static assert(AlignedStruct.noRet.offsetof == 16);
159 static assert(AlignedStruct.lastLong.offsetof == 16);
161 union BasicUnion
163 int firstInt;
164 noreturn noRet;
165 long lastLong;
168 static assert(BasicUnion.sizeof == 8);
170 static assert(BasicUnion.firstInt.offsetof == 0);
171 static assert(BasicUnion.noRet.offsetof == 0);
172 static assert(BasicUnion.lastLong.offsetof == 0);
174 union AlignedUnion
176 int firstInt;
177 align(16) noreturn noRet;
178 long lastLong;
181 static assert(AlignedUnion.sizeof == 16);
183 static assert(AlignedUnion.firstInt.offsetof == 0);
184 static assert(AlignedUnion.noRet.offsetof == 0);
185 static assert(AlignedUnion.lastLong.offsetof == 0);
187 class BasicClass
189 int firstInt;
190 noreturn noRet;
191 long lastLong;
194 enum objectMemberSize = __traits(classInstanceSize, Object);
196 static assert(__traits(classInstanceSize, BasicClass) == objectMemberSize + (int.sizeof + longPad + long.sizeof));
198 static assert(BasicClass.firstInt.offsetof == objectMemberSize + 0);
199 static assert(BasicClass.noRet.offsetof == objectMemberSize + 4);
200 static assert(BasicClass.lastLong.offsetof == objectMemberSize + (4 + longPad));
202 class AlignedClass
204 int firstInt;
205 align(16) noreturn noRet;
206 long lastLong;
209 enum offset = (objectMemberSize + 4 + 16) & ~15;
211 static assert(__traits(classInstanceSize, AlignedClass) == offset + 8);
213 static assert(AlignedClass.firstInt.offsetof == objectMemberSize + 0);
214 static assert(AlignedClass.noRet.offsetof == offset);
215 static assert(AlignedClass.lastLong.offsetof == offset);
217 struct EmptyStruct
219 noreturn noRet;
222 static assert(EmptyStruct.sizeof == 1);
223 static assert(EmptyStruct.noRet.offsetof == 0);
225 struct EmptyStruct2
227 noreturn[4] noRet;
230 static assert(EmptyStruct2.sizeof == 1);
231 static assert(EmptyStruct2.noRet.offsetof == 0);
233 // https://issues.dlang.org/show_bug.cgi?id=22858
234 // Shouldn't mess with the alignment of other zero-sized types.
236 struct S22858
238 int a;
239 void*[0] arr;
240 char c;
241 noreturn[0] arr2;
242 char c2;
245 static assert (S22858.arr.offsetof % size_t.sizeof == 0);
246 static assert (S22858.arr2.offsetof == S22858.c.offsetof + 1);
247 static assert (S22858.arr2.offsetof == S22858.c2.offsetof);