| 1 |
/* |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
pure nothrow @safe Object(bool b) |
| 5 |
pure nothrow @safe int*(bool b) |
| 6 |
pure nothrow @safe int[](bool b) |
| 7 |
--- |
| 8 |
|
| 9 |
RUN_OUTPUT: |
| 10 |
--- |
| 11 |
Success |
| 12 |
--- |
| 13 |
*/ |
| 14 |
extern (C) int printf(const(char*) fmt, ...); |
| 15 |
|
| 16 |
alias typeof(null) null_t; |
| 17 |
|
| 18 |
/**********************************************/ |
| 19 |
|
| 20 |
void test1() |
| 21 |
{ |
| 22 |
null_t null1; |
| 23 |
typeof(null) null2; |
| 24 |
|
| 25 |
static assert(is(typeof(null1) == typeof(null))); |
| 26 |
static assert(is(typeof(null2) == typeof(null))); |
| 27 |
|
| 28 |
static assert(is(typeof(null1) == null_t)); |
| 29 |
static assert(is(typeof(null2) == null_t)); |
| 30 |
} |
| 31 |
|
| 32 |
/**********************************************/ |
| 33 |
|
| 34 |
interface I{} |
| 35 |
class C{} |
| 36 |
|
| 37 |
int f(null_t) { return 1; } |
| 38 |
int f(int[]) { return 2; } |
| 39 |
int f(C) { return 3; } |
| 40 |
|
| 41 |
void test2() |
| 42 |
{ |
| 43 |
static assert(is(null_t : C)); |
| 44 |
static assert(is(null_t : I)); |
| 45 |
static assert(is(null_t : int[])); |
| 46 |
static assert(is(null_t : void*)); |
| 47 |
static assert(is(null_t : int**)); |
| 48 |
|
| 49 |
static assert(!is(null_t == C)); |
| 50 |
static assert(!is(null_t == I)); |
| 51 |
static assert(!is(null_t == int[])); |
| 52 |
static assert(!is(null_t == void*)); |
| 53 |
static assert(!is(null_t == int**)); |
| 54 |
|
| 55 |
static assert(is(null_t == null_t)); |
| 56 |
|
| 57 |
assert(f(null) == 1); |
| 58 |
} |
| 59 |
|
| 60 |
/**********************************************/ |
| 61 |
// https://issues.dlang.org/show_bug.cgi?id=5899 |
| 62 |
|
| 63 |
auto f5899(bool b) |
| 64 |
{ |
| 65 |
if (b) |
| 66 |
return new Object; |
| 67 |
else |
| 68 |
return null; |
| 69 |
} |
| 70 |
static assert(is(typeof(f5899) R == return) && is(R == Object)); |
| 71 |
pragma(msg, typeof(f5899)); |
| 72 |
|
| 73 |
auto g5899(bool b) |
| 74 |
{ |
| 75 |
if (b) |
| 76 |
return new int; |
| 77 |
else |
| 78 |
return null; |
| 79 |
} |
| 80 |
static assert(is(typeof(g5899) R == return) && is(R == int*)); |
| 81 |
pragma(msg, typeof(g5899)); |
| 82 |
|
| 83 |
auto h5899(bool b) |
| 84 |
{ |
| 85 |
if (b) |
| 86 |
return [1]; |
| 87 |
else |
| 88 |
return null; |
| 89 |
} |
| 90 |
static assert(is(typeof(h5899) R == return) && is(R == int[])); |
| 91 |
pragma(msg, typeof(h5899)); |
| 92 |
|
| 93 |
/**********************************************/ |
| 94 |
// https://issues.dlang.org/show_bug.cgi?id=7278 |
| 95 |
|
| 96 |
struct Foo7278(string s) |
| 97 |
{ |
| 98 |
string var; |
| 99 |
void func() |
| 100 |
{ |
| 101 |
string local = var; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void test7278() |
| 106 |
{ |
| 107 |
Foo7278!null a; |
| 108 |
Foo7278!null b; |
| 109 |
} |
| 110 |
|
| 111 |
/**********************************************/ |
| 112 |
// https://issues.dlang.org/show_bug.cgi?id=8221 |
| 113 |
|
| 114 |
class A8221 |
| 115 |
{ |
| 116 |
A8221 foo() { return this; } |
| 117 |
} |
| 118 |
class B8221: A8221 |
| 119 |
{ |
| 120 |
override typeof(null) foo() { return null; } // error |
| 121 |
} |
| 122 |
void test8221() |
| 123 |
{ |
| 124 |
auto a = new A8221(); |
| 125 |
assert(a.foo() is a); |
| 126 |
auto b = new B8221(); |
| 127 |
assert(b.foo() is null); |
| 128 |
a = b; |
| 129 |
assert(a.foo() is null); |
| 130 |
} |
| 131 |
|
| 132 |
/***************************************************/ |
| 133 |
// https://issues.dlang.org/show_bug.cgi?id=8589 |
| 134 |
|
| 135 |
void test8589() |
| 136 |
{ |
| 137 |
static typeof(null) retnull() { return null; } |
| 138 |
|
| 139 |
void test(bool result, T)() |
| 140 |
{ |
| 141 |
void f(T function() dg) { assert(!dg()); } |
| 142 |
|
| 143 |
static assert((is(typeof(null) function() : T function())) == result); |
| 144 |
static assert(is(typeof( f(&retnull) )) == result); |
| 145 |
static assert(is(typeof( f(()=>null) )) == result); |
| 146 |
static if (result) |
| 147 |
{ |
| 148 |
f(&retnull); |
| 149 |
f(()=>null); |
| 150 |
} |
| 151 |
} |
| 152 |
test!(true, int*)(); |
| 153 |
test!(true, Object)(); |
| 154 |
test!(false, int[int])(); |
| 155 |
test!(false, int[])(); |
| 156 |
test!(false, void delegate())(); |
| 157 |
} |
| 158 |
|
| 159 |
/**********************************************/ |
| 160 |
// https://issues.dlang.org/show_bug.cgi?id=9385 |
| 161 |
|
| 162 |
void test9385() |
| 163 |
{ |
| 164 |
assert((null ? true : false) == false); |
| 165 |
if (null) assert(0); |
| 166 |
assert(!null); |
| 167 |
} |
| 168 |
|
| 169 |
/**********************************************/ |
| 170 |
// https://issues.dlang.org/show_bug.cgi?id=12203 |
| 171 |
|
| 172 |
void test12203() |
| 173 |
{ |
| 174 |
typeof(null) v; |
| 175 |
void foo(float) {} |
| 176 |
void delegate(float) dg = &foo; |
| 177 |
assert(dg !is null); |
| 178 |
|
| 179 |
dg = v; // Error: e2ir: cannot cast v of type typeof(null) to type void delegate(float) |
| 180 |
|
| 181 |
assert(dg is null); |
| 182 |
} |
| 183 |
|
| 184 |
/**********************************************/ |
| 185 |
|
| 186 |
int main() |
| 187 |
{ |
| 188 |
test1(); |
| 189 |
test2(); |
| 190 |
test7278(); |
| 191 |
test8221(); |
| 192 |
test8589(); |
| 193 |
test9385(); |
| 194 |
test12203(); |
| 195 |
|
| 196 |
printf("Success\n"); |
| 197 |
return 0; |
| 198 |
} |