Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3555 from 9rnsr/fix12749
Issue 12749 - Constructor-local function allows multiple mutation of immutable member
- Loading branch information
Showing
8 changed files
with
162 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/fail12749.d(19): Error: immutable field 'inum' initialization is not allowed in foreach loop | ||
fail_compilation/fail12749.d(20): Error: const field 'cnum' initialization is not allowed in foreach loop | ||
fail_compilation/fail12749.d(25): Error: immutable field 'inum' initialization is not allowed in nested function 'set' | ||
fail_compilation/fail12749.d(26): Error: const field 'cnum' initialization is not allowed in nested function 'set' | ||
--- | ||
*/ | ||
struct S | ||
{ | ||
immutable int inum; | ||
const int cnum; | ||
|
||
this(int i) | ||
{ | ||
foreach (n; Aggr()) | ||
{ | ||
inum = i; | ||
cnum = i; | ||
} | ||
|
||
void set(int i) | ||
{ | ||
inum = i; | ||
cnum = i; | ||
} | ||
} | ||
} | ||
|
||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/fail12749.d(48): Error: immutable variable 'inum' initialization is not allowed in foreach loop | ||
fail_compilation/fail12749.d(49): Error: const variable 'cnum' initialization is not allowed in foreach loop | ||
fail_compilation/fail12749.d(54): Error: immutable variable 'inum' initialization is not allowed in nested function 'set' | ||
fail_compilation/fail12749.d(55): Error: const variable 'cnum' initialization is not allowed in nested function 'set' | ||
--- | ||
*/ | ||
immutable int inum; | ||
const int cnum; | ||
static this() | ||
{ | ||
int i = 10; | ||
|
||
foreach (n; Aggr()) | ||
{ | ||
inum = i; | ||
cnum = i; | ||
} | ||
|
||
void set(int i) | ||
{ | ||
inum = i; | ||
cnum = i; | ||
} | ||
} | ||
|
||
struct Aggr | ||
{ | ||
int opApply(int delegate(int) dg) { return dg(1); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters