]> git.ipfire.org Git - people/ms/gcc.git/blob - gcc/testsuite/gdc.test/fail_compilation/shared.d
7d15b16bdc1f787b00068d653a8d140a2884b07b
[people/ms/gcc.git] / gcc / testsuite / gdc.test / fail_compilation / shared.d
1 /* REQUIRED_ARGS: -preview=nosharedaccess
2 * TEST_OUTPUT:
3 ---
4 fail_compilation/shared.d(1010): Error: direct access to shared `j` is not allowed, see `core.atomic`
5 fail_compilation/shared.d(1011): Error: direct access to shared `j` is not allowed, see `core.atomic`
6 fail_compilation/shared.d(1012): Error: direct access to shared `*p` is not allowed, see `core.atomic`
7 fail_compilation/shared.d(1013): Error: direct access to shared `a[0]` is not allowed, see `core.atomic`
8 fail_compilation/shared.d(1014): Error: direct access to shared `s.si` is not allowed, see `core.atomic`
9 fail_compilation/shared.d(1015): Error: direct access to shared `t.i` is not allowed, see `core.atomic`
10 ---
11 */
12
13 #line 1000
14
15 struct S
16 {
17 shared(int) si;
18 int i;
19 }
20
21 int test1(shared int j, shared(int)* p, shared(int)[] a, ref S s, ref shared S t)
22 {
23 int i;
24 j = i;
25 i = j;
26 i = *p;
27 i = a[0];
28 i = s.si;
29 return t.i;
30 }
31
32 /**************************************/
33
34 void byref(ref shared int);
35 void byptr(shared(int)*);
36
37 shared int x;
38
39 void test2()
40 {
41 byref(x); // ok
42 byptr(&x); // ok
43 }
44
45 /**************************************/
46
47 /*
48 * TEST_OUTPUT:
49 ---
50 fail_compilation/shared.d(2008): Error: direct access to shared `i` is not allowed, see `core.atomic`
51 fail_compilation/shared.d(2009): Error: direct access to shared `j` is not allowed, see `core.atomic`
52 fail_compilation/shared.d(2010): Error: direct access to shared `k` is not allowed, see `core.atomic`
53 ---
54 */
55
56 #line 2000
57
58 void func(int);
59
60 shared int i;
61
62 void test3(shared int k)
63 {
64 shared int j = void;
65 func(i);
66 func(j);
67 func(k);
68 }
69
70 /**************************************/
71
72 void test4() // no errors for initialization
73 {
74 shared int x;
75 shared int y = 3;
76 }
77
78 /*
79 * TEST_OUTPUT:
80 ---
81 fail_compilation/shared.d(2105): Error: direct access to shared `*pi` is not allowed, see `core.atomic`
82 fail_compilation/shared.d(2112): Error: direct access to shared `**pi` is not allowed, see `core.atomic`
83 fail_compilation/shared.d(2136): Error: direct access to shared `*c` is not allowed, see `core.atomic`
84 fail_compilation/shared.d(2142): Error: direct access to shared `*c` is not allowed, see `core.atomic`
85 fail_compilation/shared.d(2148): Error: direct access to shared `*c` is not allowed, see `core.atomic`
86 fail_compilation/shared.d(2154): Error: direct access to shared `*c.c1` is not allowed, see `core.atomic`
87 fail_compilation/shared.d(2160): Error: direct access to shared `*c.c1.c1` is not allowed, see `core.atomic`
88 fail_compilation/shared.d(2181): Error: direct access to shared `k` is not allowed, see `core.atomic`
89 fail_compilation/shared.d(2187): Error: direct access to shared `k.k2.k1` is not allowed, see `core.atomic`
90 fail_compilation/shared.d(2194): Error: direct access to shared `(new shared(K2)).k1` is not allowed, see `core.atomic`
91 fail_compilation/shared.d(2202): Error: direct access to shared `c` is not allowed, see `core.atomic`
92 fail_compilation/shared.d(2206): Error: function `shared.test_inference_2` function returns `shared` but cannot be inferred `ref`
93 fail_compilation/shared.d(2208): Error: returning `c` escapes a reference to parameter `c`
94 fail_compilation/shared.d(2214): Error: function `shared.test_inference_3` function returns `shared` but cannot be inferred `ref`
95 fail_compilation/shared.d(2216): return value `getSharedObject()` is not an lvalue
96 fail_compilation/shared.d(2222): Error: direct access to shared `a` is not allowed, see `core.atomic`
97 fail_compilation/shared.d(2220): Error: function `shared.test_inference_4` function returns `shared` but cannot be inferred `ref`
98 fail_compilation/shared.d(2222): cannot implicitly convert `a` of type `shared(const(Object))` to `object.Object`
99 fail_compilation/shared.d(2222): Error: cannot implicitly convert expression `a` of type `shared(const(Object))` to `object.Object`
100 ---
101 */
102
103 #line 2100
104 // Derived from https://issues.dlang.org/show_bug.cgi?id=20908
105 ref shared(int) test20908()
106 {
107 shared int* pi;
108 // Single indirection, but the pointer is `shared`
109 return *pi;
110 }
111
112 ref shared(int) test20908_2()
113 {
114 shared(int*)* pi;
115 // Double indirection, external pointer is not `shared`
116 return **pi;
117 }
118
119 // DotVarExp tests: See matching tests in `compilable/shared.d`
120
121 struct C1
122 {
123 int value;
124 }
125
126 struct C2
127 {
128 C1* c1;
129 }
130
131 struct C3
132 {
133 C2 c1;
134 C2* c2;
135 }
136
137 // Reading a shared pointer: not okay
138 ref shared(int) test_dotvarexp_1(return shared C1* c)
139 {
140 return c.value;
141 }
142
143 // Ditto, but explicitly dereferenced
144 ref shared(int) test_dotvarexp_2(return shared C1* c)
145 {
146 return (*c).value;
147 }
148
149 // Even taking the address (which offset the pointers) requires a load
150 shared(int)* test_dotvarexp_3(return shared C1* c)
151 {
152 return &c.value;
153 }
154
155 // First level DotVarExp dereferencing
156 ref shared(int) test_dotvarexp_4(return shared ref C2 c)
157 {
158 return c.c1.value;
159 }
160
161 // Second level DotVarExp dereferencing
162 ref shared(int) test_dotvarexp_5(return shared ref C3 c)
163 {
164 return c.c1.c1.value;
165 }
166
167 class K1
168 {
169 int value;
170 }
171
172 class K2
173 {
174 shared K1 k1;
175 }
176
177 class K3
178 {
179 K2 k2;
180 }
181
182 // A class is a pointer under the hood, and `shared` applies to the pointer
183 ref shared(int) test_dotvarexp_6(return shared K1 k)
184 {
185 return k.value;
186 }
187
188 // Using `k.ke.k1` would be okay, but not `value`
189 ref shared(int) test_dotvarexp_7(return ref K3 k)
190 {
191 return k.k2.k1.value;
192 }
193
194 // The returned value is `shared` so we shouldn't be able to access it
195 // The pointer could already be shared, e.g. by the ctor
196 ref shared(K1) test_newexp_1()
197 {
198 return new shared(K2)().k1;
199 }
200
201 // Inference tests
202
203 // Fails because no `ref`
204 auto test_inference_1(return shared ref C3 c)
205 {
206 return c;
207 }
208
209 // Fails because no `return` => Escapes
210 auto ref test_inference_2(shared C3 c)
211 {
212 return c;
213 }
214
215 shared(Object) getSharedObject() { assert(0); }
216
217 // Fails because rvalue
218 auto ref test_inference_3()
219 {
220 return getSharedObject();
221 }
222
223 // Fails because `const` conversion
224 auto ref Object test_inference_4(const return shared ref Object a)
225 {
226 return a;
227 }
228
229 // https://issues.dlang.org/show_bug.cgi?id=23226
230 // Allow accessing non-shared `this`
231 struct BitRange
232 {
233 int bits;
234 void f()
235 {
236 this.bits++;
237 }
238 }