| 1 |
/+ |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
fail_compilation/fail9665a.d(43): Error: immutable field `v` initialized multiple times |
| 5 |
fail_compilation/fail9665a.d(42): Previous initialization is here. |
| 6 |
fail_compilation/fail9665a.d(53): Error: immutable field `v` initialized multiple times |
| 7 |
fail_compilation/fail9665a.d(52): Previous initialization is here. |
| 8 |
fail_compilation/fail9665a.d(58): Error: immutable field `v` initialized multiple times |
| 9 |
fail_compilation/fail9665a.d(57): Previous initialization is here. |
| 10 |
fail_compilation/fail9665a.d(63): Error: immutable field `v` initialized multiple times |
| 11 |
fail_compilation/fail9665a.d(62): Previous initialization is here. |
| 12 |
fail_compilation/fail9665a.d(73): Error: immutable field `v` initialized multiple times |
| 13 |
fail_compilation/fail9665a.d(72): Previous initialization is here. |
| 14 |
fail_compilation/fail9665a.d(78): Error: immutable field `v` initialized multiple times |
| 15 |
fail_compilation/fail9665a.d(77): Previous initialization is here. |
| 16 |
fail_compilation/fail9665a.d(83): Error: immutable field `v` initialized multiple times |
| 17 |
fail_compilation/fail9665a.d(82): Previous initialization is here. |
| 18 |
fail_compilation/fail9665a.d(96): Error: immutable field `v` initialization is not allowed in loops or after labels |
| 19 |
fail_compilation/fail9665a.d(101): Error: immutable field `v` initialization is not allowed in loops or after labels |
| 20 |
fail_compilation/fail9665a.d(106): Error: immutable field `v` initialized multiple times |
| 21 |
fail_compilation/fail9665a.d(105): Previous initialization is here. |
| 22 |
fail_compilation/fail9665a.d(111): Error: immutable field `v` initialized multiple times |
| 23 |
fail_compilation/fail9665a.d(110): Previous initialization is here. |
| 24 |
fail_compilation/fail9665a.d(116): Error: immutable field `v` initialized multiple times |
| 25 |
fail_compilation/fail9665a.d(115): Previous initialization is here. |
| 26 |
fail_compilation/fail9665a.d(130): Error: immutable field `v` initialized multiple times |
| 27 |
fail_compilation/fail9665a.d(129): Previous initialization is here. |
| 28 |
fail_compilation/fail9665a.d(134): Error: immutable field `w` initialized multiple times |
| 29 |
fail_compilation/fail9665a.d(133): Previous initialization is here. |
| 30 |
fail_compilation/fail9665a.d(148): Error: static assert: `__traits(compiles, this.v = 1)` is false |
| 31 |
--- |
| 32 |
+/ |
| 33 |
|
| 34 |
/***************************************************/ |
| 35 |
// immutable field |
| 36 |
|
| 37 |
struct S1A |
| 38 |
{ |
| 39 |
immutable int v; |
| 40 |
this(int) |
| 41 |
{ |
| 42 |
v = 1; |
| 43 |
v = 2; // multiple initialization |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
struct S1B |
| 48 |
{ |
| 49 |
immutable int v; |
| 50 |
this(int) |
| 51 |
{ |
| 52 |
if (true) v = 1; else v = 2; |
| 53 |
v = 3; // multiple initialization |
| 54 |
} |
| 55 |
this(long) |
| 56 |
{ |
| 57 |
if (true) v = 1; |
| 58 |
v = 3; // multiple initialization |
| 59 |
} |
| 60 |
this(string) |
| 61 |
{ |
| 62 |
if (true) {} else v = 2; |
| 63 |
v = 3; // multiple initialization |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
struct S1C |
| 68 |
{ |
| 69 |
immutable int v; |
| 70 |
this(int) |
| 71 |
{ |
| 72 |
true ? (v = 1) : (v = 2); |
| 73 |
v = 3; // multiple initialization |
| 74 |
} |
| 75 |
this(long) |
| 76 |
{ |
| 77 |
auto x = true ? (v = 1) : 2; |
| 78 |
v = 3; // multiple initialization |
| 79 |
} |
| 80 |
this(string) |
| 81 |
{ |
| 82 |
auto x = true ? 1 : (v = 2); |
| 83 |
v = 3; // multiple initialization |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/***************************************************/ |
| 88 |
// with control flow |
| 89 |
|
| 90 |
struct S2 |
| 91 |
{ |
| 92 |
immutable int v; |
| 93 |
this(int) |
| 94 |
{ |
| 95 |
L: |
| 96 |
v = 1; // after labels |
| 97 |
} |
| 98 |
this(long) |
| 99 |
{ |
| 100 |
foreach (i; 0..1) |
| 101 |
v = 1; // in loops |
| 102 |
} |
| 103 |
this(string) |
| 104 |
{ |
| 105 |
v = 1; // initialization |
| 106 |
L: v = 2; // assignment after labels |
| 107 |
} |
| 108 |
this(wstring) |
| 109 |
{ |
| 110 |
v = 1; // initialization |
| 111 |
foreach (i; 0..1) v = 2; // assignment in loops |
| 112 |
} |
| 113 |
this(dstring) |
| 114 |
{ |
| 115 |
v = 1; return; |
| 116 |
v = 2; // multiple initialization |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/***************************************************/ |
| 121 |
// with immutable constructor |
| 122 |
|
| 123 |
struct S3 |
| 124 |
{ |
| 125 |
int v; |
| 126 |
int w; |
| 127 |
this(int) immutable |
| 128 |
{ |
| 129 |
v = 1; |
| 130 |
v = 2; // multiple initialization |
| 131 |
|
| 132 |
if (true) |
| 133 |
w = 1; |
| 134 |
w = 2; // multiple initialization |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/***************************************************/ |
| 139 |
// in __traits(compiles) |
| 140 |
|
| 141 |
struct S4 |
| 142 |
{ |
| 143 |
immutable int v; |
| 144 |
this(int) |
| 145 |
{ |
| 146 |
static assert(__traits(compiles, v = 1)); |
| 147 |
v = 1; |
| 148 |
static assert(__traits(compiles, v = 1)); // multiple initialization |
| 149 |
} |
| 150 |
} |