Debian Codegraph @

gcc/testsuite/gdc.test/runnable/test20811.d

raw
1 // https://issues.dlang.org/show_bug.cgi?id=20811
2
3 // OK: Mutable array literals are copied before CTFE takes ownership.
4 string issue20811a()
5 {
6 char[1] counter = ['0'];
7 counter[$-1]++;
8 return counter.dup;
9 }
10
11 static assert(issue20811a() == "1");
12 static assert(issue20811a() == "1");
13 static assert(issue20811a() == "1");
14 static assert(issue20811a() == "1");
15
16 // Issue 20811: String literals were assumed to be read-only, so weren't copied.
17 string issue20811b()
18 {
19 char[1] counter = "0";
20 counter[$-1]++;
21 return counter.dup;
22 }
23
24 static assert(issue20811b() == "1");
25 static assert(issue20811b() == "1");
26 static assert(issue20811b() == "1");
27 static assert(issue20811b() == "1");
28
29 void main()
30 {
31 // Ensure CTFE did not overwrite the original AST.
32 assert(issue20811a() == "1");
33 assert(issue20811b() == "1");
34 }