| 1 |
// https://issues.dlang.org/show_bug.cgi?id=23337 |
| 2 |
|
| 3 |
int copies, destroyed; |
| 4 |
|
| 5 |
void reset() { copies = destroyed = 0; } |
| 6 |
|
| 7 |
struct S |
| 8 |
{ |
| 9 |
this(inout ref S) inout { ++copies; } |
| 10 |
~this() { ++destroyed; } |
| 11 |
} |
| 12 |
|
| 13 |
S[3] globals; |
| 14 |
|
| 15 |
S[3] makeStaticArray() { return (S[3]).init; } |
| 16 |
|
| 17 |
S[] makeSlice(ref S[3] sa) { return sa[]; } |
| 18 |
|
| 19 |
void main() |
| 20 |
{ |
| 21 |
{ |
| 22 |
S[3] fromLvalStaticArray = globals; |
| 23 |
assert(copies == 3); |
| 24 |
} |
| 25 |
assert(destroyed == 3); |
| 26 |
reset(); |
| 27 |
|
| 28 |
{ |
| 29 |
S[3] fromRvalStaticArray = makeStaticArray(); |
| 30 |
assert(copies == 0); // moved or emplaced |
| 31 |
} |
| 32 |
assert(destroyed == 3); |
| 33 |
reset(); |
| 34 |
|
| 35 |
{ |
| 36 |
S[3] fromArrayLiteral = [S(), S(), S()]; |
| 37 |
assert(copies == 0); // moved or emplaced |
| 38 |
} |
| 39 |
assert(destroyed == 3); |
| 40 |
reset(); |
| 41 |
|
| 42 |
{ |
| 43 |
S[3] fromSliceExp = globals[]; |
| 44 |
assert(copies == 3); |
| 45 |
} |
| 46 |
assert(destroyed == 3); |
| 47 |
reset(); |
| 48 |
|
| 49 |
{ |
| 50 |
S[] slice = globals[]; |
| 51 |
S[3] fromLvalSlice = slice; |
| 52 |
assert(copies == 3); |
| 53 |
} |
| 54 |
assert(destroyed == 3); |
| 55 |
reset(); |
| 56 |
|
| 57 |
{ |
| 58 |
S[3] fromRvalSlice = makeSlice(globals); |
| 59 |
assert(copies == 3); |
| 60 |
} |
| 61 |
assert(destroyed == 3); |
| 62 |
reset(); |
| 63 |
|
| 64 |
{ |
| 65 |
S *p = &globals[0]; |
| 66 |
S[3] fromSingleLval = *p; |
| 67 |
assert(copies == 3); |
| 68 |
} |
| 69 |
assert(destroyed == 3); |
| 70 |
reset(); |
| 71 |
|
| 72 |
{ |
| 73 |
S[3] fromSingleRval = S(); |
| 74 |
assert(destroyed == 1); // temporary |
| 75 |
assert(copies == 3); |
| 76 |
} |
| 77 |
assert(destroyed == 4); |
| 78 |
reset(); |
| 79 |
|
| 80 |
// slice-exp left-hand-sides (*construction* only in ctors): |
| 81 |
|
| 82 |
{ |
| 83 |
static struct T |
| 84 |
{ |
| 85 |
S[3] ss; |
| 86 |
this(int) { ss[] = globals; } |
| 87 |
} |
| 88 |
T fromLvalStaticArray = T(0); |
| 89 |
assert(copies == 3); |
| 90 |
} |
| 91 |
assert(destroyed == 3); |
| 92 |
reset(); |
| 93 |
|
| 94 |
{ |
| 95 |
static struct T |
| 96 |
{ |
| 97 |
S[3] ss; |
| 98 |
this(int) { ss[] = makeStaticArray(); } |
| 99 |
} |
| 100 |
T fromRvalStaticArray = T(0); |
| 101 |
assert(copies == 0); // moved or emplaced |
| 102 |
} |
| 103 |
assert(destroyed == 3); |
| 104 |
reset(); |
| 105 |
|
| 106 |
{ |
| 107 |
static struct T |
| 108 |
{ |
| 109 |
S[3] ss; |
| 110 |
this(int) { ss[] = [S(), S(), S()]; } |
| 111 |
} |
| 112 |
T fromArrayLiteral = T(0); |
| 113 |
assert(copies == 0); // moved or emplaced |
| 114 |
} |
| 115 |
assert(destroyed == 3); |
| 116 |
reset(); |
| 117 |
|
| 118 |
{ |
| 119 |
static struct T |
| 120 |
{ |
| 121 |
S[3] ss; |
| 122 |
this(int) { ss[] = globals[]; } |
| 123 |
} |
| 124 |
T fromSliceExp = T(0); |
| 125 |
assert(copies == 3); |
| 126 |
} |
| 127 |
assert(destroyed == 3); |
| 128 |
reset(); |
| 129 |
|
| 130 |
{ |
| 131 |
static struct T |
| 132 |
{ |
| 133 |
S[3] ss; |
| 134 |
this(int) |
| 135 |
{ |
| 136 |
S[] slice = globals[]; |
| 137 |
ss[] = slice; |
| 138 |
} |
| 139 |
} |
| 140 |
T fromLvalSlice = T(0); |
| 141 |
assert(copies == 3); |
| 142 |
} |
| 143 |
assert(destroyed == 3); |
| 144 |
reset(); |
| 145 |
|
| 146 |
{ |
| 147 |
static struct T |
| 148 |
{ |
| 149 |
S[3] ss; |
| 150 |
this(int) { ss[] = makeSlice(globals); } |
| 151 |
} |
| 152 |
T fromRvalSlice = T(0); |
| 153 |
assert(copies == 3); |
| 154 |
} |
| 155 |
assert(destroyed == 3); |
| 156 |
reset(); |
| 157 |
|
| 158 |
{ |
| 159 |
static struct T |
| 160 |
{ |
| 161 |
S[3] ss; |
| 162 |
this(int) { ss[] = globals[0]; } |
| 163 |
} |
| 164 |
T fromSingleLval = T(0); |
| 165 |
assert(copies == 3); |
| 166 |
} |
| 167 |
assert(destroyed == 3); |
| 168 |
reset(); |
| 169 |
|
| 170 |
{ |
| 171 |
static struct T |
| 172 |
{ |
| 173 |
S[3] ss; |
| 174 |
this(int) { ss[] = S(); } |
| 175 |
} |
| 176 |
T fromSingleRval = T(0); |
| 177 |
assert(destroyed == 1); // temporary |
| 178 |
assert(copies == 3); |
| 179 |
} |
| 180 |
assert(destroyed == 4); |
| 181 |
reset(); |
| 182 |
} |