Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/test15306.d

raw
1 /*
2 TEST_OUTPUT:
3 ---
4 fail_compilation/test15306.d(15): Error: `immutable` delegate `test15306.main.__dgliteral_L15_C16` cannot access mutable data `i`
5 fail_compilation/test15306.d(19): Error: `shared` delegate `test15306.main.__dgliteral_L19_C16` cannot access non-shared data `p`
6 ---
7 */
8
9 // https://issues.dlang.org/show_bug.cgi?id=15306
10
11 void main()
12 {
13 // immutable cannot access mutable
14 int i = 42;
15 auto dg1 = delegate void() immutable { auto inner = i; };
16
17 // shared cannot access unshared
18 int* p = &i;
19 auto dg2 = delegate int() shared { return *p; };
20 assert(dg2() == i);
21
22 // unshared can access shared
23 shared j = 43;
24 shared int* q = &j;
25 auto dg3 = delegate int() { return *q; };
26 assert(dg2() == j);
27 }