| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
Success |
| 5 |
--- |
| 6 |
*/ |
| 7 |
|
| 8 |
extern(C) int printf(const char*, ...); |
| 9 |
|
| 10 |
/***********************/ |
| 11 |
|
| 12 |
@nogc int test1() |
| 13 |
{ |
| 14 |
return 3; |
| 15 |
} |
| 16 |
|
| 17 |
/***********************/ |
| 18 |
// https://issues.dlang.org/show_bug.cgi?id=3032 |
| 19 |
|
| 20 |
void test3032() @nogc |
| 21 |
{ |
| 22 |
scope o1 = new Object(); // on stack |
| 23 |
scope o2 = new class Object {}; // on stack |
| 24 |
|
| 25 |
int n = 1; |
| 26 |
scope fp = (){ n = 10; }; // no closure |
| 27 |
fp(); |
| 28 |
assert(n == 10); |
| 29 |
} |
| 30 |
|
| 31 |
/***********************/ |
| 32 |
// https://issues.dlang.org/show_bug.cgi?id=12642 |
| 33 |
|
| 34 |
__gshared int[1] data12642; |
| 35 |
|
| 36 |
int[1] foo12642() @nogc |
| 37 |
{ |
| 38 |
int x; |
| 39 |
return [x]; |
| 40 |
} |
| 41 |
|
| 42 |
void test12642() @nogc |
| 43 |
{ |
| 44 |
int x; |
| 45 |
data12642 = [x]; |
| 46 |
int[1] data2; |
| 47 |
data2 = [x]; |
| 48 |
|
| 49 |
data2 = foo12642(); |
| 50 |
} |
| 51 |
|
| 52 |
/***********************/ |
| 53 |
// https://issues.dlang.org/show_bug.cgi?id=12936 |
| 54 |
|
| 55 |
void test12936() @nogc |
| 56 |
{ |
| 57 |
foreach (int[1] a; [[1]]) |
| 58 |
{ |
| 59 |
assert(a == [1]); |
| 60 |
} |
| 61 |
foreach (i, int[1] a; [[1], [2]]) |
| 62 |
{ |
| 63 |
if (i == 0) assert(a == [1]); |
| 64 |
else if (i == 1) assert(a == [2]); |
| 65 |
else assert(0); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/***********************/ |
| 70 |
|
| 71 |
version(none) // Pending future enhancements: |
| 72 |
void testIndexedArrayLiteral() @nogc |
| 73 |
{ |
| 74 |
int i = 2; |
| 75 |
int x = [10, 20, 30, 40][i]; |
| 76 |
assert(x == 30); |
| 77 |
|
| 78 |
enum arr = [100, 200, 300, 400][1 .. $]; |
| 79 |
assert(arr[i] == 400); |
| 80 |
} |
| 81 |
|
| 82 |
void testArrayLiteralLvalue() |
| 83 |
{ |
| 84 |
// https://github.com/dlang/dmd/pull/16784 |
| 85 |
// Test that this array literal is *not* put on the stack because |
| 86 |
// it gets its address taken |
| 87 |
static int* getPtr(int i) => &[1, 2, 3][i]; |
| 88 |
int* x = getPtr(1); |
| 89 |
int* y = getPtr(1); |
| 90 |
assert(x != y); |
| 91 |
} |
| 92 |
|
| 93 |
/***********************/ |
| 94 |
|
| 95 |
int main() |
| 96 |
{ |
| 97 |
test1(); |
| 98 |
test3032(); |
| 99 |
test12642(); |
| 100 |
test12936(); |
| 101 |
testArrayLiteralLvalue(); |
| 102 |
|
| 103 |
printf("Success\n"); |
| 104 |
return 0; |
| 105 |
} |