]> git.ipfire.org Git - people/ms/gcc.git/blame - gcc/testsuite/gdc.test/fail_compilation/shared.d
d: Merge upstream dmd, druntime 4ca4140e58, phobos 454dff14d.
[people/ms/gcc.git] / gcc / testsuite / gdc.test / fail_compilation / shared.d
CommitLineData
5fee5ec3
IB
1/* REQUIRED_ARGS: -preview=nosharedaccess
2 * TEST_OUTPUT:
3---
4fail_compilation/shared.d(1010): Error: direct access to shared `j` is not allowed, see `core.atomic`
5fail_compilation/shared.d(1011): Error: direct access to shared `j` is not allowed, see `core.atomic`
6fail_compilation/shared.d(1012): Error: direct access to shared `*p` is not allowed, see `core.atomic`
7fail_compilation/shared.d(1013): Error: direct access to shared `a[0]` is not allowed, see `core.atomic`
8fail_compilation/shared.d(1014): Error: direct access to shared `s.si` is not allowed, see `core.atomic`
9fail_compilation/shared.d(1015): Error: direct access to shared `t.i` is not allowed, see `core.atomic`
10---
11*/
12
13#line 1000
14
15struct S
16{
17 shared(int) si;
18 int i;
19}
20
21int 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
34void byref(ref shared int);
35void byptr(shared(int)*);
36
37shared int x;
38
39void test2()
40{
41 byref(x); // ok
42 byptr(&x); // ok
43}
44
45/**************************************/
46
47/*
48 * TEST_OUTPUT:
49---
50fail_compilation/shared.d(2008): Error: direct access to shared `i` is not allowed, see `core.atomic`
51fail_compilation/shared.d(2009): Error: direct access to shared `j` is not allowed, see `core.atomic`
52fail_compilation/shared.d(2010): Error: direct access to shared `k` is not allowed, see `core.atomic`
53---
54 */
55
56#line 2000
57
58void func(int);
59
60shared int i;
61
62void test3(shared int k)
63{
64 shared int j = void;
65 func(i);
66 func(j);
67 func(k);
68}
69
70/**************************************/
71
72void test4() // no errors for initialization
73{
74 shared int x;
75 shared int y = 3;
76}
77
78/*
79 * TEST_OUTPUT:
80---
81fail_compilation/shared.d(2105): Error: direct access to shared `*pi` is not allowed, see `core.atomic`
82fail_compilation/shared.d(2112): Error: direct access to shared `**pi` is not allowed, see `core.atomic`
83fail_compilation/shared.d(2136): Error: direct access to shared `*c` is not allowed, see `core.atomic`
84fail_compilation/shared.d(2142): Error: direct access to shared `*c` is not allowed, see `core.atomic`
85fail_compilation/shared.d(2148): Error: direct access to shared `*c` is not allowed, see `core.atomic`
86fail_compilation/shared.d(2154): Error: direct access to shared `*c.c1` is not allowed, see `core.atomic`
87fail_compilation/shared.d(2160): Error: direct access to shared `*c.c1.c1` is not allowed, see `core.atomic`
88fail_compilation/shared.d(2181): Error: direct access to shared `k` is not allowed, see `core.atomic`
8da8c7d3 89fail_compilation/shared.d(2187): Error: direct access to shared `k.k2.k1.value` is not allowed, see `core.atomic`
5fee5ec3
IB
90fail_compilation/shared.d(2194): Error: direct access to shared `(new shared(K2)).k1` is not allowed, see `core.atomic`
91fail_compilation/shared.d(2202): Error: direct access to shared `c` is not allowed, see `core.atomic`
92fail_compilation/shared.d(2206): Error: function `shared.test_inference_2` function returns `shared` but cannot be inferred `ref`
93fail_compilation/shared.d(2208): Error: returning `c` escapes a reference to parameter `c`
5fee5ec3
IB
94fail_compilation/shared.d(2214): Error: function `shared.test_inference_3` function returns `shared` but cannot be inferred `ref`
95fail_compilation/shared.d(2216): return value `getSharedObject()` is not an lvalue
96fail_compilation/shared.d(2222): Error: direct access to shared `a` is not allowed, see `core.atomic`
97fail_compilation/shared.d(2220): Error: function `shared.test_inference_4` function returns `shared` but cannot be inferred `ref`
98fail_compilation/shared.d(2222): cannot implicitly convert `a` of type `shared(const(Object))` to `object.Object`
99fail_compilation/shared.d(2222): Error: cannot implicitly convert expression `a` of type `shared(const(Object))` to `object.Object`
100---
101 */
235d5a96 102
5fee5ec3
IB
103#line 2100
104// Derived from https://issues.dlang.org/show_bug.cgi?id=20908
105ref shared(int) test20908()
106{
107 shared int* pi;
108 // Single indirection, but the pointer is `shared`
109 return *pi;
110}
111
112ref 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
121struct C1
122{
123 int value;
124}
125
126struct C2
127{
128 C1* c1;
129}
130
131struct C3
132{
133 C2 c1;
134 C2* c2;
135}
136
137// Reading a shared pointer: not okay
138ref shared(int) test_dotvarexp_1(return shared C1* c)
139{
140 return c.value;
141}
142
143// Ditto, but explicitly dereferenced
144ref 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
150shared(int)* test_dotvarexp_3(return shared C1* c)
151{
152 return &c.value;
153}
154
155// First level DotVarExp dereferencing
156ref shared(int) test_dotvarexp_4(return shared ref C2 c)
157{
158 return c.c1.value;
159}
160
161// Second level DotVarExp dereferencing
162ref shared(int) test_dotvarexp_5(return shared ref C3 c)
163{
164 return c.c1.c1.value;
165}
166
167class K1
168{
169 int value;
170}
171
172class K2
173{
174 shared K1 k1;
175}
176
177class K3
178{
179 K2 k2;
180}
181
182// A class is a pointer under the hood, and `shared` applies to the pointer
183ref 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`
189ref 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
196ref shared(K1) test_newexp_1()
197{
198 return new shared(K2)().k1;
199}
200
201// Inference tests
202
203// Fails because no `ref`
204auto test_inference_1(return shared ref C3 c)
205{
206 return c;
207}
208
209// Fails because no `return` => Escapes
210auto ref test_inference_2(shared C3 c)
211{
212 return c;
213}
214
215shared(Object) getSharedObject() { assert(0); }
216
217// Fails because rvalue
218auto ref test_inference_3()
219{
220 return getSharedObject();
221}
222
223// Fails because `const` conversion
224auto ref Object test_inference_4(const return shared ref Object a)
225{
226 return a;
227}
b7a586be
IB
228
229// https://issues.dlang.org/show_bug.cgi?id=23226
230// Allow accessing non-shared `this`
231struct BitRange
232{
233 int bits;
234 void f()
235 {
236 this.bits++;
237 }
238}
8da8c7d3
IB
239
240/*
241TEST_OUTPUT:
242---
243fail_compilation/shared.d(3004): Error: cast from `void*` to `shared(int*)` not allowed in safe code
244fail_compilation/shared.d(3005): Error: cast from `void*` to `shared(const(int*))` not allowed in safe code
245fail_compilation/shared.d(3008): Error: cast from `shared(void*)` to `int*` not allowed in safe code
246fail_compilation/shared.d(3009): Error: cast from `shared(void*)` to `shared(const(int*))` not allowed in safe code
247---
248*/
249
250#line 3000
251
252void test_casting_safe() @safe
253{
254 void *p;
255 auto t1 = cast(shared(int*))p;
256 auto t2 = cast(const(shared(int*)))p;
257
258 shared void* s;
259 auto x1 = cast(int*)s;
260 auto x2 = cast(const(shared(int*)))s;
261}