]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gdc.test/fail_compilation/test19097.d
Merge remote-tracking branch 'origin/master' into devel/c++-contracts
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / fail_compilation / test19097.d
CommitLineData
5fee5ec3
IB
1/* REQUIRED_ARGS: -preview=dip1000
2 * TEST_OUTPUT:
3---
31350635
IB
4fail_compilation/test19097.d(44): Error: scope variable `s` may not be returned
5fail_compilation/test19097.d(48): Error: scope variable `s1` may not be returned
7e7ebe3e 6fail_compilation/test19097.d(77): Error: scope variable `z` assigned to `ref` variable `refPtr` with longer lifetime
31350635
IB
7fail_compilation/test19097.d(108): Error: scope variable `s4` may not be returned
8fail_compilation/test19097.d(126): Error: scope variable `s5c` may not be returned
9fail_compilation/test19097.d(130): Error: scope variable `s5m` may not be returned
10fail_compilation/test19097.d(147): Error: scope variable `s6c` may not be returned
11fail_compilation/test19097.d(151): Error: scope variable `s6m` may not be returned
5fee5ec3
IB
12---
13 */
14
31350635
IB
15// Test extended return-scope / return-ref semantics, e.g. assigning to `this` or the first parameter
16
5fee5ec3
IB
17// https://issues.dlang.org/show_bug.cgi?id=19097
18
19@safe:
20
21void betty(ref scope int* r, return scope int* p)
22{
23 r = p;
24}
25
26void freddy(out scope int* r, return scope int* p)
27{
28 r = p;
29}
30
31struct S
32{
33 int* a;
34 this(return scope int* b) scope { a = b; }
35
36 int* c;
37 void mem(return scope int* d) scope { c = d; }
38}
39
40S thorin()
41{
42 int i;
43 S s = S(&i); // should infer scope for s
44 return s; // so this should error
31350635
IB
45
46 S s1;
47 s1.mem(&i);
48 return s1;
5fee5ec3
IB
49}
50
51/************************/
52
53struct S2(T)
54{
55 int* p;
56
57 void silent(lazy void dg);
58
59 void foo()
60 {
61 char[] name;
62 silent(name = parseType());
63 }
64
65 char[] parseType(char[] name = null);
66}
67
68S2!int s2;
69
7e287503
IB
70/************************/
71struct S3
72{
73 int* ptr;
74 void assign(ref int* refPtr, return scope int* z) scope @safe
75 {
76 this.ptr = z; // allowed, first ref
77 refPtr = z; // should not be allowed
78 }
79}
80
81int* escape() @safe
82{
83 int local;
84
85 S3 escapeThis;
86 int* escapeRef;
87
88 escapeThis.assign(escapeRef, &local);
89
90 return escapeRef;
91}
92
93/************************/
94// https://issues.dlang.org/show_bug.cgi?id=22837
95struct S4
96{
97 int* p;
98 this(int dummy, return scope int* p) @safe
99 {
100 this.p = p;
101 }
102}
103
104int* escape2()
105{
106 int x;
31350635
IB
107 auto s4 = S4(0, &x);
108 return s4.p;
109}
110
111/************************/
112// https://issues.dlang.org/show_bug.cgi?id=22801
113struct S5
114{
115 int* a;
116 this(return ref int b) { a = &b; }
117
118 int* c;
119 void mem(return ref int d) scope { c = &d; }
120}
121
122S5 frerin()
123{
124 int i;
125 S5 s5c = S5(i); // should infer scope for s
126 return s5c; // so this should error
127
128 S5 s5m;
129 s5m.mem(i);
130 return s5m;
131}
132
133
134struct S6
135{
136 int** a;
137 this(return ref int* b) { a = &b; }
138
139 int** c;
140 void mem(return ref int* d) scope { c = &d; }
141}
142
143S6 dis()
144{
145 int* i = null;
146 S6 s6c = S6(i); // should infer scope for s
147 return s6c; // so this should error
148
149 S6 s6m;
150 s6m.mem(i);
151 return s6m;
7e287503 152}