| 1 |
/* TEST_OUTPUT: |
| 2 |
--- |
| 3 |
fail_compilation/fail19038.d(21): Error: cannot implicitly convert expression `a` of type `string[][]` to `const(string)[][]` |
| 4 |
fail_compilation/fail19038.d(23): Error: cannot modify `const` expression `c[0]` |
| 5 |
--- |
| 6 |
* Credit: yshui |
| 7 |
* https://github.com/dlang/dmd/pull/8413#issuecomment-401104961 |
| 8 |
* https://issues.dlang.org/show_bug.cgi?id=19038 |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
void test() |
| 13 |
{ |
| 14 |
/* string[][] is not implicitly converible to const(string)[][], |
| 15 |
* and there is good reason why: |
| 16 |
* |
| 17 |
* https://stackoverflow.com/questions/5055655/double-pointer-const-correctness-warnings-in-c |
| 18 |
*/ |
| 19 |
|
| 20 |
string[][] a = [["Lord"]]; |
| 21 |
const(string)[][] b = a; // assume this works (and it should not) |
| 22 |
const(string)[] c = ["Sauron"]; |
| 23 |
c[0] = "Mordor"; // invalid, because c[0] is const(string) |
| 24 |
|
| 25 |
b[0] = c; // valid, b[0] is const(string)[] |
| 26 |
// But now, a[0] has become c |
| 27 |
a[0][0] = "Nazgul"; // valid, because a[0][0] is string |
| 28 |
// But this also changes c[0], which shouldn't be possible |
| 29 |
} |