| 1 |
/* |
| 2 |
REQUIRED_ARGS: -checkaction=context -preview=dip1000 |
| 3 |
PERMUTE_ARGS: -O -g -inline |
| 4 |
*/ |
| 5 |
|
| 6 |
void test8765() |
| 7 |
{ |
| 8 |
string msg; |
| 9 |
try |
| 10 |
{ |
| 11 |
int a = 0; |
| 12 |
assert(a); |
| 13 |
} |
| 14 |
catch (Throwable e) |
| 15 |
{ |
| 16 |
// no-message -> assert expression |
| 17 |
msg = e.msg; |
| 18 |
} |
| 19 |
assert(msg == "0 != true"); |
| 20 |
} |
| 21 |
|
| 22 |
void test9255() |
| 23 |
{ |
| 24 |
string file; |
| 25 |
try |
| 26 |
{ |
| 27 |
int x = 0; |
| 28 |
assert(x); |
| 29 |
} |
| 30 |
catch (Throwable e) |
| 31 |
{ |
| 32 |
file = e.file; |
| 33 |
} |
| 34 |
|
| 35 |
version(Windows) |
| 36 |
assert(file && file == r"runnable\testassert.d"); |
| 37 |
else |
| 38 |
assert(file && file == "runnable/testassert.d"); |
| 39 |
} |
| 40 |
|
| 41 |
// https://issues.dlang.org/show_bug.cgi?id=20114 |
| 42 |
void test20114() |
| 43 |
{ |
| 44 |
// Function call returning simple type |
| 45 |
static int fun() { |
| 46 |
static int i = 0; |
| 47 |
assert(i++ == 0); |
| 48 |
return 3; |
| 49 |
} |
| 50 |
|
| 51 |
const a = getMessage(assert(fun() == 4)); |
| 52 |
assert(a == "3 != 4"); |
| 53 |
|
| 54 |
// Function call returning complex type with opEquals |
| 55 |
static struct S |
| 56 |
{ |
| 57 |
bool opEquals(const int x) const |
| 58 |
{ |
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static S bar() |
| 64 |
{ |
| 65 |
static int i = 0; |
| 66 |
assert(i++ == 0); |
| 67 |
return S.init; |
| 68 |
} |
| 69 |
|
| 70 |
const b = getMessage(assert(bar() == 4)); |
| 71 |
assert(b == "S() != 4"); |
| 72 |
|
| 73 |
// Non-call expression with side effects |
| 74 |
int i = 0; |
| 75 |
const c = getMessage(assert(++i == 0)); |
| 76 |
assert(c == "1 != 0"); |
| 77 |
} |
| 78 |
|
| 79 |
version (DigitalMars) version (Win64) version = DMD_Win64; |
| 80 |
|
| 81 |
void test20375() @safe |
| 82 |
{ |
| 83 |
static struct RefCounted |
| 84 |
{ |
| 85 |
// Force temporary through "impure" generator function |
| 86 |
static RefCounted create() @trusted |
| 87 |
{ |
| 88 |
__gshared int counter = 0; |
| 89 |
return RefCounted(++counter > 0); |
| 90 |
} |
| 91 |
|
| 92 |
static int instances; |
| 93 |
static int postblits; |
| 94 |
|
| 95 |
this(bool) @safe |
| 96 |
{ |
| 97 |
instances++; |
| 98 |
} |
| 99 |
|
| 100 |
this(this) @safe |
| 101 |
{ |
| 102 |
instances++; |
| 103 |
postblits++; |
| 104 |
} |
| 105 |
|
| 106 |
~this() @safe |
| 107 |
{ |
| 108 |
// make the dtor non-nothrow (we are tracking clean-ups during AssertError unwinding) |
| 109 |
if (postblits > 100) |
| 110 |
throw new Exception(""); |
| 111 |
assert(instances > 0); |
| 112 |
instances--; |
| 113 |
} |
| 114 |
|
| 115 |
bool opEquals(RefCounted) @safe |
| 116 |
{ |
| 117 |
return true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
{ |
| 122 |
auto a = RefCounted.create(); |
| 123 |
RefCounted.instances++; // we're about to construct an instance below, increment manually |
| 124 |
assert(a == RefCounted()); // both operands are pure expressions => no temporaries |
| 125 |
} |
| 126 |
|
| 127 |
assert(RefCounted.instances == 0); |
| 128 |
assert(RefCounted.postblits == 0); |
| 129 |
|
| 130 |
{ |
| 131 |
auto a = RefCounted.create(); |
| 132 |
assert(a == RefCounted.create()); // impure rhs is promoted to a temporary lvalue => copy for a.opEquals(rhs) |
| 133 |
} |
| 134 |
|
| 135 |
assert(RefCounted.instances == 0); |
| 136 |
assert(RefCounted.postblits == 1); |
| 137 |
RefCounted.postblits = 0; |
| 138 |
|
| 139 |
{ |
| 140 |
const msg = getMessage(assert(RefCounted.create() != RefCounted.create())); // both operands promoted to temporary lvalues |
| 141 |
assert(msg == "RefCounted() == RefCounted()"); |
| 142 |
} |
| 143 |
|
| 144 |
version (DMD_Win64) // FIXME: temporaries apparently not destructed when unwinding via AssertError |
| 145 |
{ |
| 146 |
assert(RefCounted.instances >= 0 && RefCounted.instances <= 2); |
| 147 |
RefCounted.instances = 0; |
| 148 |
} |
| 149 |
else |
| 150 |
assert(RefCounted.instances == 0); |
| 151 |
assert(RefCounted.postblits == 1); |
| 152 |
RefCounted.postblits = 0; |
| 153 |
|
| 154 |
static int numGetLvalImpureCalls = 0; |
| 155 |
ref RefCounted getLvalImpure() @trusted |
| 156 |
{ |
| 157 |
numGetLvalImpureCalls++; |
| 158 |
__gshared lval = RefCounted(); // not incrementing RefCounted.instances |
| 159 |
return lval; |
| 160 |
} |
| 161 |
|
| 162 |
{ |
| 163 |
const msg = getMessage(assert(getLvalImpure() != getLvalImpure())); // both operands promoted to ref temporaries |
| 164 |
assert(msg == "RefCounted() == RefCounted()"); |
| 165 |
} |
| 166 |
|
| 167 |
assert(numGetLvalImpureCalls == 2); |
| 168 |
assert(RefCounted.instances == 0); |
| 169 |
assert(RefCounted.postblits == 1); |
| 170 |
RefCounted.postblits = 0; |
| 171 |
} |
| 172 |
|
| 173 |
// https://issues.dlang.org/show_bug.cgi?id=21471 |
| 174 |
void test21471() |
| 175 |
{ |
| 176 |
{ |
| 177 |
static struct S |
| 178 |
{ |
| 179 |
S get()() const { return this; } |
| 180 |
} |
| 181 |
|
| 182 |
static auto f(S s) { return s; } |
| 183 |
|
| 184 |
auto s = S(); |
| 185 |
assert(f(s.get) == s); |
| 186 |
} |
| 187 |
{ |
| 188 |
pragma(inline, true) |
| 189 |
real exp(real x) pure nothrow |
| 190 |
{ |
| 191 |
return x; |
| 192 |
} |
| 193 |
|
| 194 |
bool isClose(int lhs, real rhs) pure nothrow |
| 195 |
{ |
| 196 |
return false; |
| 197 |
} |
| 198 |
auto c = 0; |
| 199 |
assert(!isClose(c, exp(1))); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// https://issues.dlang.org/show_bug.cgi?id=20581 |
| 204 |
void test20581() @safe |
| 205 |
{ |
| 206 |
static auto retro(return scope int[] s) @safe |
| 207 |
{ |
| 208 |
static struct R |
| 209 |
{ |
| 210 |
int[] source; |
| 211 |
} |
| 212 |
return R(s); |
| 213 |
} |
| 214 |
|
| 215 |
int[5] a = [ 1, 2, 3, 4, 5 ]; |
| 216 |
// Creates ref temporary __assertTmpXXXX for source |
| 217 |
// Error: address of variable a assigned to __assertOp27 with longer lifetime |
| 218 |
assert(retro(a[]).source is a[]); |
| 219 |
} |
| 220 |
|
| 221 |
string getMessage(T)(lazy T expr) @trusted |
| 222 |
{ |
| 223 |
try |
| 224 |
{ |
| 225 |
expr(); |
| 226 |
return null; |
| 227 |
} |
| 228 |
catch (Throwable t) |
| 229 |
{ |
| 230 |
return t.msg; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void testMixinExpression() @safe |
| 235 |
{ |
| 236 |
static struct S |
| 237 |
{ |
| 238 |
bool opEquals(S) @safe { return true; } |
| 239 |
} |
| 240 |
|
| 241 |
const msg = getMessage(assert(mixin("S() != S()"))); |
| 242 |
assert(msg == "S() == S()"); |
| 243 |
} |
| 244 |
|
| 245 |
void testUnaryFormat() |
| 246 |
{ |
| 247 |
int zero = 0, one = 1; |
| 248 |
assert(getMessage(assert(zero)) == "0 != true"); |
| 249 |
assert(getMessage(assert(!one)) == "1 == true"); |
| 250 |
|
| 251 |
assert(getMessage(assert(!cast(int) 1.5)) == "1 == true"); |
| 252 |
|
| 253 |
assert(getMessage(assert(!!__ctfe)) == "assert(__ctfe) failed!"); |
| 254 |
|
| 255 |
static struct S |
| 256 |
{ |
| 257 |
int i; |
| 258 |
bool opCast() const |
| 259 |
{ |
| 260 |
return i < 2; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
assert(getMessage(assert(S(4))) == "S(4) != true"); |
| 265 |
|
| 266 |
S s = S(4); |
| 267 |
assert(getMessage(assert(*&s)) == "S(4) != true"); |
| 268 |
|
| 269 |
assert(getMessage(assert(--(++zero))) == "0 != true"); |
| 270 |
} |
| 271 |
|
| 272 |
void testAssignments() |
| 273 |
{ |
| 274 |
int a = 1; |
| 275 |
int b = 2; |
| 276 |
assert(getMessage(assert(a -= --b)) == "0 != true"); |
| 277 |
|
| 278 |
static ref int c() |
| 279 |
{ |
| 280 |
static int counter; |
| 281 |
counter++; |
| 282 |
return counter; |
| 283 |
} |
| 284 |
|
| 285 |
assert(getMessage(assert(--c())) == "0 != true"); |
| 286 |
} |
| 287 |
|
| 288 |
/// https://issues.dlang.org/show_bug.cgi?id=21472 |
| 289 |
void testTupleFormat() |
| 290 |
{ |
| 291 |
alias AliasSeq(T...) = T; |
| 292 |
|
| 293 |
// Default usage |
| 294 |
{ |
| 295 |
alias a = AliasSeq!(1, "ab"); |
| 296 |
alias b = AliasSeq!(false, "xyz"); |
| 297 |
assert(getMessage(assert(a == b)) == `(1, "ab") != (false, "xyz")`); |
| 298 |
} |
| 299 |
|
| 300 |
// Single elements work but are not formatted as tuples |
| 301 |
// Is this acceptable? (a workaround would probably require a |
| 302 |
// different name for the tuple formatting hook) |
| 303 |
{ |
| 304 |
alias a = AliasSeq!(1, "ab", []); |
| 305 |
alias b = AliasSeq!(false, "xyz", [1]); |
| 306 |
assert(getMessage(assert(a == b)) == `(1, "ab", []) != (false, "xyz", [1])`); |
| 307 |
} |
| 308 |
|
| 309 |
// Also works with tupleof (as taken from the bug report) |
| 310 |
{ |
| 311 |
static struct Var { int a, b; } |
| 312 |
const a = Var(1, 2); |
| 313 |
const b = Var(3, 4); |
| 314 |
const msg = getMessage(assert(a.tupleof == b.tupleof)); |
| 315 |
assert(msg == `(1, 2) != (3, 4)`); |
| 316 |
} |
| 317 |
|
| 318 |
// Also works when creating temporaries for the TupleExp |
| 319 |
{ |
| 320 |
static struct S |
| 321 |
{ |
| 322 |
int a, b; |
| 323 |
} |
| 324 |
|
| 325 |
static S get() |
| 326 |
{ |
| 327 |
static int i; |
| 328 |
return S(i++, i++); |
| 329 |
} |
| 330 |
|
| 331 |
auto a = get().tupleof; |
| 332 |
auto b = get().tupleof; |
| 333 |
|
| 334 |
string msg = getMessage(assert(a == b)); |
| 335 |
assert(msg == `(0, 1) != (2, 3)`); |
| 336 |
|
| 337 |
msg = getMessage(assert(get().tupleof == AliasSeq!(2, 3))); |
| 338 |
assert(msg == `(4, 5) != (2, 3)`); |
| 339 |
|
| 340 |
msg = getMessage(assert(get().tupleof == get().tupleof)); |
| 341 |
assert(msg == `(6, 7) != (8, 9)`); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// https://issues.dlang.org/show_bug.cgi?id=21682 |
| 346 |
void testStaticOperators() |
| 347 |
{ |
| 348 |
static class environment { |
| 349 |
static bool opCmp(scope const(char)[] name) |
| 350 |
{ |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
static bool opBinaryRight(string op : "in")(scope const(char)[] name) |
| 355 |
{ |
| 356 |
return false; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
string msg = getMessage(assert(environment < "Hello")); |
| 361 |
assert(msg == `"environment" >= "Hello"`); |
| 362 |
|
| 363 |
msg = getMessage(assert("Hello" in environment)); |
| 364 |
assert(msg == `"Hello" !in "environment"`); |
| 365 |
} |
| 366 |
|
| 367 |
void main() |
| 368 |
{ |
| 369 |
test8765(); |
| 370 |
test9255(); |
| 371 |
test20114(); |
| 372 |
test20375(); |
| 373 |
test21471(); |
| 374 |
test20581(); |
| 375 |
testMixinExpression(); |
| 376 |
testUnaryFormat(); |
| 377 |
testAssignments(); |
| 378 |
testTupleFormat(); |
| 379 |
testStaticOperators(); |
| 380 |
} |