| 1 |
string result; |
| 2 |
|
| 3 |
struct A |
| 4 |
{ |
| 5 |
this(ref A rhs) |
| 6 |
{ |
| 7 |
result ~= "A"; |
| 8 |
} |
| 9 |
this(ref immutable A rhs) |
| 10 |
{ |
| 11 |
result ~= "B"; |
| 12 |
} |
| 13 |
this(ref const A rhs) |
| 14 |
{ |
| 15 |
result ~= "C"; |
| 16 |
} |
| 17 |
this(ref A rhs) immutable |
| 18 |
{ |
| 19 |
result ~= "D"; |
| 20 |
} |
| 21 |
this(ref const A rhs) shared |
| 22 |
{ |
| 23 |
result ~= "E"; |
| 24 |
} |
| 25 |
this(ref A rhs) shared |
| 26 |
{ |
| 27 |
result ~= "F"; |
| 28 |
} |
| 29 |
this(ref shared A rhs) immutable |
| 30 |
{ |
| 31 |
result ~= "G"; |
| 32 |
} |
| 33 |
this(ref shared A rhs) shared |
| 34 |
{ |
| 35 |
result ~= "H"; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// copy constructor correctly uses function declaration overload resolution |
| 40 |
void test1() |
| 41 |
{ |
| 42 |
result = ""; |
| 43 |
A a; |
| 44 |
A a1 = a; |
| 45 |
immutable A a2 = a; |
| 46 |
const A a3 = a2; |
| 47 |
shared A a4 = a3; |
| 48 |
A a5 = a3; |
| 49 |
assert(result == "ADBEC"); |
| 50 |
} |
| 51 |
|
| 52 |
// copy constructor has priority over alias this |
| 53 |
struct B |
| 54 |
{ |
| 55 |
B fun(immutable B) |
| 56 |
{ |
| 57 |
return B(); |
| 58 |
} |
| 59 |
|
| 60 |
this(ref immutable B rhs) |
| 61 |
{ |
| 62 |
result ~= "A"; |
| 63 |
} |
| 64 |
alias fun this; |
| 65 |
} |
| 66 |
|
| 67 |
void test2() |
| 68 |
{ |
| 69 |
result = ""; |
| 70 |
immutable B a; |
| 71 |
B a1 = a; |
| 72 |
assert(result == "A"); |
| 73 |
} |
| 74 |
|
| 75 |
// arguments and return values correctly call the copy constructor |
| 76 |
shared(A) fun(A x) |
| 77 |
{ |
| 78 |
return x; |
| 79 |
} |
| 80 |
|
| 81 |
immutable(A) fun2(shared A x) |
| 82 |
{ |
| 83 |
return x; |
| 84 |
} |
| 85 |
|
| 86 |
void test3() |
| 87 |
{ |
| 88 |
result = ""; |
| 89 |
A a1; |
| 90 |
shared A a2 = fun(a1); |
| 91 |
immutable A a3 = fun2(a2); |
| 92 |
assert(result == "AFHG"); |
| 93 |
} |
| 94 |
|
| 95 |
// nested structs |
| 96 |
int fun() |
| 97 |
{ |
| 98 |
int x = 1; |
| 99 |
struct A |
| 100 |
{ |
| 101 |
struct B |
| 102 |
{ |
| 103 |
int x2 = 2; |
| 104 |
} |
| 105 |
|
| 106 |
B b; |
| 107 |
int x1; |
| 108 |
|
| 109 |
this(int x) |
| 110 |
{ |
| 111 |
this.x1 = x; |
| 112 |
b = B(3); |
| 113 |
} |
| 114 |
this(ref A rhs) |
| 115 |
{ |
| 116 |
this.x1 = rhs.x1 + rhs.b.x2 + x; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
A a = A(2); |
| 121 |
A b = a; |
| 122 |
return b.x1; |
| 123 |
} |
| 124 |
|
| 125 |
void test4() |
| 126 |
{ |
| 127 |
assert(fun() == 6); |
| 128 |
} |
| 129 |
|
| 130 |
// generated copy constructor |
| 131 |
struct X |
| 132 |
{ |
| 133 |
this(ref inout(X) rhs) inout |
| 134 |
{ |
| 135 |
result ~= "A"; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
struct Y |
| 140 |
{ |
| 141 |
X a; |
| 142 |
} |
| 143 |
|
| 144 |
void test5() |
| 145 |
{ |
| 146 |
result = ""; |
| 147 |
Y b1; |
| 148 |
Y b2 = b1; |
| 149 |
assert(result == "A"); |
| 150 |
} |
| 151 |
|
| 152 |
void main() |
| 153 |
{ |
| 154 |
test1(); |
| 155 |
test2(); |
| 156 |
test3(); |
| 157 |
test4(); |
| 158 |
test5(); |
| 159 |
} |