Debian Codegraph @

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

raw
1 /* TEST_OUTPUT:
2 REQUIRED_ARGS: -preview=dip1000
3 ---
4 fail_compilation/test15191.d(34): Error: returning `&identity(x)` escapes a reference to local variable `x`
5 fail_compilation/test15191.d(40): Error: returning `&identityPtr(x)` escapes a reference to local variable `x`
6 fail_compilation/test15191.d(46): Error: returning `&identityPtr(x)` escapes a reference to local variable `x`
7 fail_compilation/test15191.d(67): Error: taking address of `scope` variable `x` with pointers is not allowed in a `@safe` function
8 fail_compilation/test15191.d(69): Error: taking address of `scope` variable `x` with pointers is not allowed in a `@safe` function
9 ---
10 */
11
12 // Test taking the address of a `ref return` using & and [] operators
13 // https://issues.dlang.org/show_bug.cgi?id=15191
14 // https://issues.dlang.org/show_bug.cgi?id=22519
15 // https://issues.dlang.org/show_bug.cgi?id=22539
16
17 @safe:
18 ref int foo(return ref int s)
19 {
20 return s;
21 }
22
23 int* bar(return ref int s)
24 {
25 return &foo(s);
26 }
27
28 ref int identity(ref return int x) {return x;}
29 ref int* identityPtr(ref return int* x) {return x;}
30
31 int* addrOfRefEscape()
32 {
33 int x;
34 return &identity(x);
35 }
36
37 int** addrOfRefSystem() @system
38 {
39 int* x;
40 return &identityPtr(x);
41 }
42
43 int** addrOfRefTransitive()
44 {
45 int* x;
46 return &identityPtr(x);
47 }
48
49 int gInt;
50 ref int getGlobalInt() {return gInt;}
51
52 int* addrOfRefGlobal()
53 {
54 return &getGlobalInt();
55 }
56
57 // Slice:
58 ref int*[1] identityArr(return ref scope int*[1] x)
59 {
60 return x;
61 }
62
63 int*[] sliceOfRefEscape()
64 {
65 int stackVar = 0xFF;
66 scope int*[1] x = [&stackVar];
67 auto y = identityArr(x)[]; // check transitive scope in assignment
68 cast(void) y;
69 return identityArr(x)[]; // check transitive scope in return statement
70 }
71
72 // https://issues.dlang.org/show_bug.cgi?id=23079
73 int** p;
74
75 ref int* get() @safe
76 {
77 return *p;
78 }
79
80 int** g1() @safe
81 {
82 return &get();
83 }