| 1 |
/* |
| 2 |
REQUIRED_ARGS: -w |
| 3 |
TEST_OUTPUT: |
| 4 |
--- |
| 5 |
noreturn |
| 6 |
--- |
| 7 |
|
| 8 |
Basic properties and usage mentioned in the DIP: |
| 9 |
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md |
| 10 |
*/ |
| 11 |
|
| 12 |
alias noreturn = typeof(*null); |
| 13 |
pragma(msg, noreturn); |
| 14 |
|
| 15 |
static assert(!is(noreturn == void)); |
| 16 |
|
| 17 |
// Fails |
| 18 |
// static assert(is( typeof([]) == noreturn[] )); |
| 19 |
// static assert(is( typeof([][0]) == noreturn )); |
| 20 |
|
| 21 |
static assert(is( typeof(assert(0)) == noreturn )); |
| 22 |
|
| 23 |
static assert(is( typeof(throw new Exception("")) == noreturn )); |
| 24 |
|
| 25 |
static assert(is(noreturn == noreturn)); |
| 26 |
static assert(!is(noreturn == const noreturn)); |
| 27 |
static assert(is(noreturn : const noreturn)); |
| 28 |
|
| 29 |
static assert(!is(noreturn == int)); |
| 30 |
static assert(is(noreturn : int)); |
| 31 |
|
| 32 |
// Covariance |
| 33 |
static assert(is(noreturn[] : int[])); |
| 34 |
static assert(is(noreturn* : int*)); |
| 35 |
static assert(is(noreturn function() : int function())); |
| 36 |
static assert(is(noreturn delegate() : int delegate())); |
| 37 |
|
| 38 |
// Reject inverse conversions |
| 39 |
static assert(!is(int[] : noreturn[])); |
| 40 |
static assert(!is(int* : noreturn*)); |
| 41 |
static assert(!is(int function() : noreturn function())); |
| 42 |
static assert(!is(int delegate() : noreturn delegate())); |
| 43 |
|
| 44 |
static assert(noreturn.mangleof == "Nn"); // Changed from b due to conflicts with bool |
| 45 |
static assert(noreturn.sizeof == 0); |
| 46 |
static assert(noreturn.alignof == 0); |
| 47 |
|
| 48 |
static assert((noreturn*).sizeof == (int*).sizeof); |
| 49 |
static assert((noreturn[]).sizeof == (int[]).sizeof); |
| 50 |
|
| 51 |
static assert(is(typeof(noreturn.init) == noreturn)); |
| 52 |
static assert(is(typeof((const noreturn).init) == const noreturn)); |
| 53 |
static assert(is(typeof((immutable noreturn).init) == immutable noreturn)); |
| 54 |
static assert(is(typeof((shared noreturn).init) == shared noreturn)); |
| 55 |
|
| 56 |
version (DigitalMars) |
| 57 |
noreturn exits(int* p) |
| 58 |
{ |
| 59 |
*p = 3; |
| 60 |
assert(false); // *p could be valid |
| 61 |
} |
| 62 |
|
| 63 |
noreturn exit(); |
| 64 |
|
| 65 |
noreturn pureexits() @nogc nothrow pure @safe { assert(0); } |
| 66 |
|
| 67 |
noreturn callpureexits() { pureexits(); } |
| 68 |
|
| 69 |
noreturn returnExits() |
| 70 |
{ |
| 71 |
return pureexits(); |
| 72 |
} |
| 73 |
|
| 74 |
void alsoExits() |
| 75 |
{ |
| 76 |
return assert(0); |
| 77 |
} |
| 78 |
|
| 79 |
int thisAlsoExits() |
| 80 |
{ |
| 81 |
return assert(0); |
| 82 |
} |
| 83 |
|
| 84 |
void cast_() |
| 85 |
{ |
| 86 |
noreturn n; |
| 87 |
int i = n; |
| 88 |
} |
| 89 |
|
| 90 |
int test1(int i) |
| 91 |
{ |
| 92 |
if (exit()) |
| 93 |
return i + 1; |
| 94 |
return i - 1; |
| 95 |
} |
| 96 |
|
| 97 |
noreturn tlsNoreturn; |
| 98 |
__gshared noreturn globalNoreturn; |
| 99 |
|
| 100 |
template CreateTLS(A) |
| 101 |
{ |
| 102 |
A a; |
| 103 |
} |
| 104 |
|
| 105 |
void* useTls() |
| 106 |
{ |
| 107 |
alias Tnr = CreateTLS!noreturn; |
| 108 |
void* a1 = &Tnr.a; |
| 109 |
void* a2 = &tlsNoreturn; |
| 110 |
void* a3 = &globalNoreturn; |
| 111 |
return a1 < a2 ? a2 : a3; |
| 112 |
} |
| 113 |
|
| 114 |
/***************************************************/ |
| 115 |
|
| 116 |
noreturn testfn(noreturn function() fn) |
| 117 |
{ |
| 118 |
fn(); |
| 119 |
} |
| 120 |
|
| 121 |
noreturn testdg(noreturn delegate() dg) |
| 122 |
{ |
| 123 |
dg(); |
| 124 |
} |
| 125 |
|
| 126 |
noreturn func() |
| 127 |
{ |
| 128 |
while(1) |
| 129 |
{ |
| 130 |
} |
| 131 |
} |
| 132 |
alias AliasSeq(T...) = T; |
| 133 |
alias Types = AliasSeq!(bool, byte, ubyte, short, ushort, int, uint, |
| 134 |
long, ulong, char, wchar, dchar, float, double, |
| 135 |
real); |
| 136 |
void noreturnImplicit() |
| 137 |
{ |
| 138 |
/* |
| 139 |
Testing both ways because, although the underlying table |
| 140 |
is symmetrical the code that calls into it may be buggy. |
| 141 |
*/ |
| 142 |
{ |
| 143 |
int x = 2 + func(); |
| 144 |
int y = func() + 2; |
| 145 |
} |
| 146 |
foreach(T; Types) |
| 147 |
{ |
| 148 |
T value; |
| 149 |
auto x = value + throw new Exception("Hello"); |
| 150 |
auto y = (throw new Exception("wow")) + value; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// https://issues.dlang.org/show_bug.cgi?id=23549 |
| 155 |
int foo(int g = assert(0)) { |
| 156 |
return g; |
| 157 |
} |
| 158 |
|
| 159 |
// https://issues.dlang.org/show_bug.cgi?id=22587 |
| 160 |
int front(int param) |
| 161 |
{ |
| 162 |
return param ? 1 : assert(0); |
| 163 |
} |