| 1 |
/* PERMUTE_ARGS: -preview=rvaluerefparam |
| 2 |
/* testing __rvalue */ |
| 3 |
|
| 4 |
import core.stdc.stdio; |
| 5 |
|
| 6 |
/********************************/ |
| 7 |
|
| 8 |
int foo(int) { printf("foo(int)\n"); return 1; } |
| 9 |
int foo(ref int) { printf("foo(ref int)\n"); return 2; } |
| 10 |
|
| 11 |
void test1() |
| 12 |
{ |
| 13 |
int s; |
| 14 |
assert(foo(s) == 2); |
| 15 |
assert(foo(__rvalue(s)) == 1); |
| 16 |
} |
| 17 |
|
| 18 |
/********************************/ |
| 19 |
|
| 20 |
struct S |
| 21 |
{ |
| 22 |
nothrow: |
| 23 |
~this() { printf("~this() %p\n", &this); } |
| 24 |
this(ref S) { printf("this(ref S)\n"); } |
| 25 |
void opAssign(S) { printf("opAssign(S)\n"); } |
| 26 |
} |
| 27 |
|
| 28 |
void test2() |
| 29 |
{ |
| 30 |
S s; |
| 31 |
S t; |
| 32 |
|
| 33 |
t = __rvalue(s); |
| 34 |
} |
| 35 |
|
| 36 |
/********************************/ |
| 37 |
|
| 38 |
struct S3 |
| 39 |
{ |
| 40 |
int a, b, c; |
| 41 |
|
| 42 |
this(S3) {} |
| 43 |
this(ref S3) {} |
| 44 |
} |
| 45 |
|
| 46 |
void test3() |
| 47 |
{ |
| 48 |
S3 s; |
| 49 |
S3 x = s; // this line causes the compiler to crash |
| 50 |
} |
| 51 |
|
| 52 |
/********************************/ |
| 53 |
|
| 54 |
struct S4 |
| 55 |
{ |
| 56 |
void* p; |
| 57 |
|
| 58 |
this(ref S4) { } |
| 59 |
|
| 60 |
this(S4 s) |
| 61 |
{ |
| 62 |
assert(&s is &x); // confirm the rvalue reference |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
__gshared S4 x; |
| 67 |
|
| 68 |
void test4() |
| 69 |
{ |
| 70 |
S4 t = __rvalue(x); |
| 71 |
} |
| 72 |
|
| 73 |
/********************************/ |
| 74 |
|
| 75 |
struct S5 |
| 76 |
{ |
| 77 |
this(S5 s) { printf("this(S5 s)\n"); } |
| 78 |
this(ref inout S5 s) inout { printf("this(ref inout S5 s) inout\n"); } |
| 79 |
} |
| 80 |
|
| 81 |
void test5() |
| 82 |
{ |
| 83 |
S5 t; |
| 84 |
S5 t1 = t; |
| 85 |
S5 t2 = __rvalue(t); |
| 86 |
} |
| 87 |
|
| 88 |
/********************************/ |
| 89 |
|
| 90 |
int moveCtor, copyCtor, moveAss, copyAss; |
| 91 |
|
| 92 |
struct S6 |
| 93 |
{ |
| 94 |
this(S6 s) { ++moveCtor; } |
| 95 |
this(ref S6 s) { ++copyCtor; } |
| 96 |
void opAssign(S6 s) { ++moveAss; } |
| 97 |
void opAssign(ref S6 s) { ++copyAss; } |
| 98 |
} |
| 99 |
|
| 100 |
void test6() |
| 101 |
{ |
| 102 |
S6 x; |
| 103 |
S6 s = x; |
| 104 |
// printf("S6 s = x; moveCtor %d copyCtor %d moveAss %d copyAss %d\n", moveCtor, copyCtor, moveAss, copyAss); |
| 105 |
assert(copyCtor == 1); |
| 106 |
S6 s2 = __rvalue(x); |
| 107 |
// printf("S6 s2 = __rvalue(x); moveCtor %d copyCtor %d moveAss %d copyAss %d\n", moveCtor, copyCtor, moveAss, copyAss); |
| 108 |
assert(moveCtor == 1); |
| 109 |
s2 = s; |
| 110 |
// printf("s2 = s; moveCtor %d copyCtor %d moveAss %d copyAss %d\n", moveCtor, copyCtor, moveAss, copyAss); |
| 111 |
assert(copyAss == 1); |
| 112 |
s2 = __rvalue(s); |
| 113 |
// printf("s2 = __rvalue(s); moveCtor %d copyCtor %d moveAss %d copyAss %d\n", moveCtor, copyCtor, moveAss, copyAss); |
| 114 |
assert(moveAss == 1); |
| 115 |
assert(copyCtor == 1 && moveCtor == 1 && copyAss == 1 && moveAss == 1); |
| 116 |
} |
| 117 |
|
| 118 |
/********************************/ |
| 119 |
// https://github.com/dlang/dmd/pull/17050#issuecomment-2543370370 |
| 120 |
|
| 121 |
struct MutableString(size_t E) {} |
| 122 |
|
| 123 |
struct StringTest |
| 124 |
{ |
| 125 |
alias toString this; |
| 126 |
const(char)[] toString() const pure |
| 127 |
=> null; |
| 128 |
|
| 129 |
this(StringTest rhs) {} |
| 130 |
this(ref inout StringTest rhs) inout pure {} |
| 131 |
|
| 132 |
this(typeof(null)) inout pure {} |
| 133 |
this(size_t Embed)(MutableString!Embed str) inout {} |
| 134 |
|
| 135 |
~this() {} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void test7() |
| 140 |
{ |
| 141 |
StringTest s = StringTest(null); |
| 142 |
} |
| 143 |
|
| 144 |
/********************************/ |
| 145 |
// https://github.com/dlang/dmd/issues/20562 |
| 146 |
|
| 147 |
struct S8 |
| 148 |
{ |
| 149 |
int a,b; |
| 150 |
this(S8) { printf("this(S)\n"); b = 4; } |
| 151 |
this(ref S8) { printf("this(ref S)\n"); assert(0); } |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
S8 returnRval(ref S8 arg) |
| 156 |
{ |
| 157 |
static if (0) |
| 158 |
{ |
| 159 |
/* __copytmp2 = 0 ; |
| 160 |
_D7rvalue51S6__ctorMFNcKSQxQrZQg call (arg param #__copytmp2); |
| 161 |
* __HID1 streq 1 __copytmp2; |
| 162 |
__HID1; |
| 163 |
*/ |
| 164 |
return arg; |
| 165 |
} |
| 166 |
else static if (1) |
| 167 |
{ |
| 168 |
/* * __HID1 streq 1 * arg; |
| 169 |
__HID1; |
| 170 |
*/ |
| 171 |
return __rvalue(arg); // should move-construct the NRVO value |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
/* * t = 0 ; |
| 176 |
t; |
| 177 |
_TMP0 = t; |
| 178 |
_D7rvalue51S6__ctorMFNcSQwQqZQg call (arg param _TMP0); |
| 179 |
t; |
| 180 |
*/ |
| 181 |
S8 t = __rvalue(arg); |
| 182 |
return t; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
void test8() |
| 188 |
{ |
| 189 |
S8 s; |
| 190 |
S8 t = returnRval(s); |
| 191 |
printf("t.b: %d\n", t.b); |
| 192 |
assert(t.b == 4); |
| 193 |
} |
| 194 |
|
| 195 |
/********************************/ |
| 196 |
|
| 197 |
struct T9 |
| 198 |
{ |
| 199 |
int i; |
| 200 |
inout this(ref inout T9 t) { this.i = t.i - 1; printf("this(ref T9)\n"); } |
| 201 |
inout this(inout T9 t) { this.i = t.i + 1; printf("this(T9)\n"); } |
| 202 |
} |
| 203 |
|
| 204 |
struct S9 |
| 205 |
{ |
| 206 |
T9 t; |
| 207 |
//inout this(return ref scope inout S9 t);// { this.i = t.i - 1; printf("this(ref T9)\n"); } |
| 208 |
//@system inout this(return scope inout S9 t);// { this.i = t.i + 1; printf("this(T9)\n"); } |
| 209 |
} |
| 210 |
|
| 211 |
void test9() |
| 212 |
{ |
| 213 |
S9 s; |
| 214 |
s.t.i = 3; |
| 215 |
S9 u = s; |
| 216 |
printf("u.t.i = %d\n", u.t.i); |
| 217 |
assert(u.t.i == 2); |
| 218 |
|
| 219 |
S9 v = __rvalue(u); |
| 220 |
printf("v.t.i = %d\n", v.t.i); |
| 221 |
assert(v.t.i == 3); |
| 222 |
} |
| 223 |
|
| 224 |
/********************************/ |
| 225 |
// https://github.com/s-ludwig/taggedalgebraic/issues/75 |
| 226 |
|
| 227 |
struct T10 |
| 228 |
{ |
| 229 |
string s; |
| 230 |
this(T10) {} |
| 231 |
this(string v) { s = v; } |
| 232 |
} |
| 233 |
|
| 234 |
struct S10 |
| 235 |
{ |
| 236 |
T10 p; |
| 237 |
} |
| 238 |
|
| 239 |
void test10() |
| 240 |
{ |
| 241 |
S10 s = S10(T10("hello")); |
| 242 |
assert(s.p.s == "hello"); |
| 243 |
} |
| 244 |
|
| 245 |
/********************************/ |
| 246 |
|
| 247 |
int main() |
| 248 |
{ |
| 249 |
test1(); |
| 250 |
test2(); |
| 251 |
test3(); |
| 252 |
test4(); |
| 253 |
test5(); |
| 254 |
test6(); |
| 255 |
test7(); |
| 256 |
test8(); |
| 257 |
test9(); |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |