| 1 |
/* |
| 2 |
PERMUTE_ARGS: -O -inline |
| 3 |
RUN_OUTPUT: |
| 4 |
--- |
| 5 |
getAndPrintS |
| 6 |
--- |
| 7 |
*/ |
| 8 |
|
| 9 |
import core.stdc.stdio; |
| 10 |
import core.exception : AssertError; |
| 11 |
|
| 12 |
/*****************************************/ |
| 13 |
|
| 14 |
// noreturn is inferred for functions that always throw |
| 15 |
// The code must not strip the destructor when calling a noreturn function |
| 16 |
|
| 17 |
struct WithDtor |
| 18 |
{ |
| 19 |
__gshared int destroyed; |
| 20 |
|
| 21 |
int num; |
| 22 |
|
| 23 |
int acceptNoreturn(int a, int b, int c) |
| 24 |
{ |
| 25 |
puts("unreachable"); |
| 26 |
return num + a + b + c; |
| 27 |
} |
| 28 |
|
| 29 |
~this() |
| 30 |
{ |
| 31 |
destroyed += num; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
noreturn doesThrow() |
| 36 |
{ |
| 37 |
WithDtor wd = WithDtor(1); |
| 38 |
throw new Exception(""); |
| 39 |
} |
| 40 |
|
| 41 |
noreturn callDoesThrow() |
| 42 |
{ |
| 43 |
WithDtor wd = WithDtor(2); |
| 44 |
doesThrow(); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
void testDtors() |
| 49 |
{ |
| 50 |
try |
| 51 |
{ |
| 52 |
callDoesThrow(); |
| 53 |
assert(0); |
| 54 |
} catch (Exception e) {} |
| 55 |
|
| 56 |
assert(WithDtor.destroyed == 3); |
| 57 |
} |
| 58 |
|
| 59 |
/*****************************************************************************/ |
| 60 |
|
| 61 |
/// Verifies that `func` throws a `Throwable` with `message` at `line` |
| 62 |
void testAssertFailure(size_t expLine, string expMsg, void function() func, size_t callLine = __LINE__) |
| 63 |
{ |
| 64 |
void enforce(bool check, string error) |
| 65 |
{ |
| 66 |
if (!check) |
| 67 |
throw new AssertError(error, __FILE__, callLine); |
| 68 |
} |
| 69 |
|
| 70 |
bool caught; |
| 71 |
try |
| 72 |
{ |
| 73 |
func(); |
| 74 |
} |
| 75 |
catch (Throwable t) |
| 76 |
{ |
| 77 |
// Save members because t might be overwritten by an Assertion failure below |
| 78 |
string actFile = t.file; |
| 79 |
size_t actLine = t.line; |
| 80 |
string actMsg = t.msg; |
| 81 |
caught = true; |
| 82 |
|
| 83 |
scope (failure) |
| 84 |
{ |
| 85 |
printf("\nfile = \"%.*s\"\nline = %zu\nmsg = \"%.*s\"\n\n", |
| 86 |
cast(int) actFile.length, actFile.ptr, |
| 87 |
actLine, |
| 88 |
cast(int) actMsg.length, actMsg.ptr |
| 89 |
); |
| 90 |
fflush(stdout); |
| 91 |
} |
| 92 |
|
| 93 |
enforce(actFile == __FILE__, "Wrong file"); |
| 94 |
enforce(actLine == expLine, "Wrong line"); |
| 95 |
enforce(actMsg == expMsg, "Wrong message"); |
| 96 |
} |
| 97 |
|
| 98 |
enforce(caught, "No Throwable was thrown!"); |
| 99 |
} |
| 100 |
|
| 101 |
void testAccess() |
| 102 |
{ |
| 103 |
enum msg = "Accessed expression of type `noreturn`"; |
| 104 |
|
| 105 |
// FIXME: Another assertion failure in the backend trying to generate noreturn.sizeof = 0 byte assignment |
| 106 |
version (FIXME) |
| 107 |
testAssertFailure(__LINE__ + 3, msg, function noreturn() |
| 108 |
{ |
| 109 |
noreturn a; |
| 110 |
noreturn b = a; |
| 111 |
}); |
| 112 |
|
| 113 |
if (false) // read does not assert! |
| 114 |
testAssertFailure(__LINE__ + 3, msg, function noreturn() |
| 115 |
{ |
| 116 |
noreturn a; |
| 117 |
int b = a; |
| 118 |
assert(false, "Unreachable!"); // Statement above not detected as noreturn |
| 119 |
}); |
| 120 |
|
| 121 |
testAssertFailure(__LINE__ + 2, msg, function noreturn() |
| 122 |
{ |
| 123 |
cast(noreturn) 1; |
| 124 |
}); |
| 125 |
|
| 126 |
version (FIXME) |
| 127 |
testAssertFailure(__LINE__ + 3, msg, function noreturn() |
| 128 |
{ |
| 129 |
noreturn a; |
| 130 |
noreturn b = cast(noreturn) 1; |
| 131 |
}); |
| 132 |
|
| 133 |
if (false) // Read does not assert |
| 134 |
testAssertFailure(__LINE__ + 3, msg, function noreturn() |
| 135 |
{ |
| 136 |
noreturn a; |
| 137 |
return a; |
| 138 |
}); |
| 139 |
|
| 140 |
if (false) // Read does not assert |
| 141 |
testAssertFailure(__LINE__ + 4, msg, function noreturn() |
| 142 |
{ |
| 143 |
static void foo(noreturn) {} |
| 144 |
noreturn a; |
| 145 |
foo(a); |
| 146 |
assert(false, "Unreachable!"); // Ditto |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
/*****************************************/ |
| 151 |
|
| 152 |
void testFuncCall() |
| 153 |
{ |
| 154 |
enum msg = "Called abort()"; |
| 155 |
enum line = __LINE__ + 1; |
| 156 |
static noreturn abort() { assert(0, msg); } |
| 157 |
|
| 158 |
// Canaries to check for side effects |
| 159 |
__gshared int countLeft, countRight; |
| 160 |
|
| 161 |
scope (failure) printf("countLeft = %d\ncountRight = %d\n", countLeft, countRight); |
| 162 |
|
| 163 |
|
| 164 |
// D function arguments are evaluated left to right |
| 165 |
testAssertFailure(line, msg, function() |
| 166 |
{ |
| 167 |
static void acceptNoreturnD(int, int, int) { puts("unreachable"); } |
| 168 |
|
| 169 |
acceptNoreturnD(countLeft++, abort(), countRight++); |
| 170 |
}); |
| 171 |
|
| 172 |
assert(countLeft == 1); |
| 173 |
assert(countRight == 0); |
| 174 |
|
| 175 |
// // C function arguments are still evaluated left to right |
| 176 |
// // Despite them being push in reverse order |
| 177 |
testAssertFailure(line, msg, function() |
| 178 |
{ |
| 179 |
static extern(C) void acceptNoreturnC(int, int, int) { puts("unreachable"); } |
| 180 |
|
| 181 |
acceptNoreturnC(countLeft++, abort(), countRight++); |
| 182 |
|
| 183 |
assert(false); |
| 184 |
}); |
| 185 |
|
| 186 |
assert(countLeft == 2); |
| 187 |
assert(countRight == 0); |
| 188 |
|
| 189 |
WithDtor.destroyed = 0; |
| 190 |
|
| 191 |
testAssertFailure(__LINE__ + 2, "Error", function() |
| 192 |
{ |
| 193 |
static WithDtor getS() { assert(false, "Error"); } |
| 194 |
|
| 195 |
getS().acceptNoreturn(countLeft++, abort(), countRight++); |
| 196 |
}); |
| 197 |
|
| 198 |
assert(countLeft == 2); // No changes |
| 199 |
assert(countRight == 0); |
| 200 |
assert(WithDtor.destroyed == 0); // No temporary to destruct |
| 201 |
|
| 202 |
testAssertFailure(line, msg, function() |
| 203 |
{ |
| 204 |
static WithDtor getAndPrintS() { puts("getAndPrintS"); return WithDtor(1); } |
| 205 |
|
| 206 |
getAndPrintS().acceptNoreturn(countLeft++, abort(), countRight++); |
| 207 |
}); |
| 208 |
|
| 209 |
assert(countLeft == 3); |
| 210 |
assert(countRight == 0); |
| 211 |
assert(WithDtor.destroyed == 1); |
| 212 |
} |
| 213 |
|
| 214 |
// https://issues.dlang.org/show_bug.cgi?id=24701 |
| 215 |
void testCast() |
| 216 |
{ |
| 217 |
noreturn foo; |
| 218 |
try |
| 219 |
auto a = cast(int)foo; |
| 220 |
catch (Throwable e) |
| 221 |
{ |
| 222 |
assert(e.msg == "Accessed expression of type `noreturn`"); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
assert(0); |
| 227 |
} |
| 228 |
|
| 229 |
int main() |
| 230 |
{ |
| 231 |
testDtors(); |
| 232 |
testAccess(); |
| 233 |
testFuncCall(); |
| 234 |
testCast(); |
| 235 |
return 0; |
| 236 |
} |