Debian Codegraph @

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

raw
1 /*
2 REQUIRED_ARGS: -preview=dip1000
3 TEST_OUTPUT:
4 ---
5 fail_compilation/test22977.d(16): Error: escaping local variable through nested function `scfunc` is not allowed in a `@safe` function
6 fail_compilation/test22977.d(22): Error: escaping local variable through nested function `scfunc2` is not allowed in a `@safe` function
7 ---
8 */
9
10 // Issue 22977 - [dip1000] can escape scope pointer returned by nested function
11 // https://issues.dlang.org/show_bug.cgi?id=22977
12
13 auto p0(scope string s) @safe
14 {
15 string scfunc() { return s; }
16 return scfunc();
17 }
18
19 auto p1(scope string s) @safe
20 {
21 ref string scfunc2() { return s; }
22 return scfunc2();
23 }
24
25 // Reduced from Mir
26 struct Tuple(T...)
27 {
28 T expand;
29 }
30
31 auto autoExpandAndForward(alias value)()
32 {
33 return value.expand[0];
34 }
35
36 struct MapIterator
37 {
38 int* p;
39 int* foo() scope
40 {
41 auto t = Tuple!(int*)(p);
42 return autoExpandAndForward!t;
43 }
44 }
45
46 // Reduced from Phobos
47 float partial(alias fun)()
48 {
49 return fun();
50 }
51
52 auto partialFunction() @safe
53 {
54 int function() f = () => 0;
55 return &partial!(f);
56 }