| 1 |
/* |
| 2 |
REQUIRED_ARGS: -w -o- |
| 3 |
|
| 4 |
TEST_OUTPUT: |
| 5 |
--- |
| 6 |
fail_compilation/noreturn2.d(18): Error: expected return type of `noreturn`, not `void` |
| 7 |
--- |
| 8 |
|
| 9 |
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md |
| 10 |
*/ |
| 11 |
|
| 12 |
alias noreturn = typeof(*null); |
| 13 |
|
| 14 |
void doStuff(); |
| 15 |
|
| 16 |
noreturn returnVoid() |
| 17 |
{ |
| 18 |
return doStuff(); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
/+ |
| 23 |
TEST_OUTPUT: |
| 24 |
--- |
| 25 |
fail_compilation/noreturn2.d(37): Error: expected return type of `int`, not `string`: |
| 26 |
fail_compilation/noreturn2.d(35): Return type of `int` inferred here. |
| 27 |
--- |
| 28 |
+/ |
| 29 |
|
| 30 |
auto missmatch(int i) |
| 31 |
{ |
| 32 |
if (i < 0) |
| 33 |
return assert(false); |
| 34 |
if (i == 0) |
| 35 |
return i; |
| 36 |
if (i > 0) |
| 37 |
return ""; |
| 38 |
} |
| 39 |
|
| 40 |
/+ |
| 41 |
TEST_OUTPUT: |
| 42 |
--- |
| 43 |
fail_compilation/noreturn2.d(50): Error: function `noreturn2.returns` is typed as `NR` but does return |
| 44 |
fail_compilation/noreturn2.d(50): `noreturn` functions must either throw, abort or loop indefinitely |
| 45 |
--- |
| 46 |
+/ |
| 47 |
|
| 48 |
enum NR : noreturn; |
| 49 |
|
| 50 |
NR returns() |
| 51 |
{ |
| 52 |
// Fallthrough despite noreturn |
| 53 |
} |
| 54 |
|
| 55 |
/+ |
| 56 |
TEST_OUTPUT: |
| 57 |
--- |
| 58 |
fail_compilation/noreturn2.d(64): Error: return value `1` of type `int` does not match return type `noreturn`, and cannot be implicitly converted |
| 59 |
--- |
| 60 |
+/ |
| 61 |
|
| 62 |
noreturn returnsValue() |
| 63 |
{ |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
/+ |
| 68 |
TEST_OUTPUT: |
| 69 |
--- |
| 70 |
fail_compilation/noreturn2.d(75): Error: expected return type of `int`, not `void` |
| 71 |
--- |
| 72 |
+/ |
| 73 |
int returnVoid2() |
| 74 |
{ |
| 75 |
return doStuff(); |
| 76 |
} |
| 77 |
|
| 78 |
/+ |
| 79 |
TEST_OUTPUT: |
| 80 |
--- |
| 81 |
fail_compilation/noreturn2.d(89): Error: mismatched function return type inference of `void` and `int` |
| 82 |
--- |
| 83 |
+/ |
| 84 |
auto returnVoid3(int i) |
| 85 |
{ |
| 86 |
if (i > 0) |
| 87 |
return i; |
| 88 |
else |
| 89 |
return doStuff(); |
| 90 |
} |
| 91 |
|
| 92 |
/+ |
| 93 |
TEST_OUTPUT: |
| 94 |
--- |
| 95 |
fail_compilation/noreturn2.d(104): Error: `object.Exception` is thrown but not caught |
| 96 |
fail_compilation/noreturn2.d(100): Error: function `noreturn2.doesNestedThrow` may throw but is marked as `nothrow` |
| 97 |
--- |
| 98 |
+/ |
| 99 |
|
| 100 |
int doesNestedThrow(int i) nothrow |
| 101 |
{ |
| 102 |
// Weird formatting is intended to check the loc |
| 103 |
return i ? i++ : |
| 104 |
throw |
| 105 |
new |
| 106 |
Exception("") |
| 107 |
; |
| 108 |
} |
| 109 |
|
| 110 |
int doesNestedThrowThrowable(int i) nothrow |
| 111 |
{ |
| 112 |
return i ? i++ : throw new Error(""); |
| 113 |
} |
| 114 |
|
| 115 |
/+ |
| 116 |
TEST_OUTPUT: |
| 117 |
--- |
| 118 |
fail_compilation/noreturn2.d(130): Error: cannot create instance of interface `I` |
| 119 |
fail_compilation/noreturn2.d(133): Error: can only throw class objects derived from `Throwable`, not type `int[]` |
| 120 |
fail_compilation/noreturn2.d(138): Error: undefined identifier `UnkownException` |
| 121 |
--- |
| 122 |
+/ |
| 123 |
|
| 124 |
int throwInvalid(int i) nothrow |
| 125 |
{ |
| 126 |
static interface I {} |
| 127 |
// Weird formatting is intended to check the loc |
| 128 |
return |
| 129 |
throw |
| 130 |
new |
| 131 |
I() |
| 132 |
? |
| 133 |
throw |
| 134 |
new |
| 135 |
int[4] |
| 136 |
: |
| 137 |
throw |
| 138 |
new |
| 139 |
UnkownException("") |
| 140 |
; |
| 141 |
} |
| 142 |
|
| 143 |
/+ |
| 144 |
https://issues.dlang.org/show_bug.cgi?id=24054 |
| 145 |
TEST_OUTPUT: |
| 146 |
--- |
| 147 |
fail_compilation/noreturn2.d(153): Error: cannot return from `noreturn` function |
| 148 |
fail_compilation/noreturn2.d(153): Consider adding an endless loop, `assert(0)`, or another `noreturn` expression |
| 149 |
--- |
| 150 |
+/ |
| 151 |
const(noreturn) f() |
| 152 |
{ |
| 153 |
return; |
| 154 |
} |