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