]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/fail_compilation/fail_scope.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / fail_compilation / fail_scope.d
1 /*
2 REQUIRED_ARGS:
3 TEST_OUTPUT:
4 ---
5 fail_compilation/fail_scope.d(44): Error: returning `cast(char[])string` escapes a reference to local variable `string`
6 fail_compilation/fail_scope.d(62): Error: returning `s.bar()` escapes a reference to local variable `s`
7 fail_compilation/fail_scope.d(73): Error: `fail_scope.foo8` called with argument types `(int)` matches both:
8 fail_compilation/fail_scope.d(67): `fail_scope.foo8(ref int x)`
9 and:
10 fail_compilation/fail_scope.d(68): `fail_scope.foo8(return ref int x)`
11 fail_compilation/fail_scope.d(81): Error: returning `& string` escapes a reference to local variable `string`
12 fail_compilation/fail_scope.d(91): Error: returning `cast(int[])a` escapes a reference to local variable `a`
13 fail_compilation/fail_scope.d(99): Error: returning `cast(int[])a` escapes a reference to local variable `a`
14 fail_compilation/fail_scope.d(107): Deprecation: escaping reference to outer local variable `x`
15 fail_compilation/fail_scope.d(126): Error: returning `s.bar()` escapes a reference to local variable `s`
16 fail_compilation/fail_scope.d(136): Error: returning `foo16226(i)` escapes a reference to local variable `i`
17 ---
18 //fail_compilation/fail_scope.d(30): Error: scope variable `da` may not be returned
19 //fail_compilation/fail_scope.d(32): Error: scope variable `o` may not be returned
20 //fail_compilation/fail_scope.d(33): Error: scope variable `dg` may not be returned
21 //fail_compilation/fail_scope.d(35): Error: scope variable `da` may not be returned
22 //fail_compilation/fail_scope.d(37): Error: scope variable `o` may not be returned
23 //fail_compilation/fail_scope.d(38): Error: scope variable `dg` may not be returned
24 //fail_compilation/fail_scope.d(40): Error: scope variable `p` may not be returned
25 */
26
27 alias int delegate() dg_t;
28
29 int[] checkEscapeScope1(scope int[] da) { return da; }
30 int[3] checkEscapeScope2(scope int[3] sa) { return sa; }
31 Object checkEscapeScope3(scope Object o) { return o; }
32 dg_t checkEscapeScope4(scope dg_t dg) { return dg; }
33
34 int[] checkEscapeScope1() { scope int[] da = []; return da; }
35 int[3] checkEscapeScope2() { scope int[3] sa = [1,2,3]; return sa; }
36 Object checkEscapeScope3() { scope Object o = new Object; return o; } // same with fail7294.d
37 dg_t checkEscapeScope4() { scope dg_t dg = () => 1; return dg; }
38
39 int* test(scope int* p) @safe { return p; }
40
41 char[] foo140()
42 {
43 char[4] string = "abcd";
44 return string;
45 }
46
47 /************/
48
49 struct S
50 {
51 int x;
52
53 ref int bar() return
54 {
55 return x;
56 }
57 }
58
59 ref int test()
60 {
61 S s;
62 return s.bar();
63 }
64
65 /************/
66
67 ref int foo8(ref int x);
68 ref int foo8(return ref int x);
69
70 void testover()
71 {
72 int x;
73 foo8(x);
74 }
75
76 /************/
77
78 char* fail141()
79 {
80 char[4] string = "abcd";
81 return string.ptr;
82 }
83
84 /************/
85
86 int[] test1313b()
87 out{}
88 do
89 {
90 int[2] a;
91 return a;
92 }
93
94 int[] test1313a()
95 //out{}
96 do
97 {
98 int[2] a;
99 return a;
100 }
101
102 /******************/
103 // https://issues.dlang.org/show_bug.cgi?id=15192
104
105 ref int fun15192(ref int x) @safe
106 {
107 ref int bar(){ return x; }
108 return bar();
109 }
110
111 ref int fun15192_2(return ref int x) @safe
112 {
113 ref int bar(){ return x; }
114 return bar();
115 }
116
117 /**************************/
118 // https://issues.dlang.org/show_bug.cgi?id=15193
119
120 ref int foo15193()@safe{
121 struct S{
122 int x;
123 ref int bar() { return x; }
124 }
125 S s;
126 return s.bar();
127 }
128
129
130 /*****************************/
131 // https://issues.dlang.org/show_bug.cgi?id=16226
132
133 ref int test16226() @safe
134 {
135 int i;
136 return foo16226(i);
137 }
138
139
140 ref foo16226(ref int bar) @safe
141 {
142 return bar;
143 }