d: Merge upstream dmd, druntime 4ca4140e58, phobos 454dff14d.
[official-gcc.git] / gcc / testsuite / gdc.test / fail_compilation / shared.d
blobafdea64c744720dea5e3243b2e70486d594b5ce2
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 ---
13 #line 1000
15 struct S
17 shared(int) si;
18 int i;
21 int test1(shared int j, shared(int)* p, shared(int)[] a, ref S s, ref shared S t)
23 int i;
24 j = i;
25 i = j;
26 i = *p;
27 i = a[0];
28 i = s.si;
29 return t.i;
32 /**************************************/
34 void byref(ref shared int);
35 void byptr(shared(int)*);
37 shared int x;
39 void test2()
41 byref(x); // ok
42 byptr(&x); // ok
45 /**************************************/
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 ---
56 #line 2000
58 void func(int);
60 shared int i;
62 void test3(shared int k)
64 shared int j = void;
65 func(i);
66 func(j);
67 func(k);
70 /**************************************/
72 void test4() // no errors for initialization
74 shared int x;
75 shared int y = 3;
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.value` 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`
103 #line 2100
104 // Derived from https://issues.dlang.org/show_bug.cgi?id=20908
105 ref shared(int) test20908()
107 shared int* pi;
108 // Single indirection, but the pointer is `shared`
109 return *pi;
112 ref shared(int) test20908_2()
114 shared(int*)* pi;
115 // Double indirection, external pointer is not `shared`
116 return **pi;
119 // DotVarExp tests: See matching tests in `compilable/shared.d`
121 struct C1
123 int value;
126 struct C2
128 C1* c1;
131 struct C3
133 C2 c1;
134 C2* c2;
137 // Reading a shared pointer: not okay
138 ref shared(int) test_dotvarexp_1(return shared C1* c)
140 return c.value;
143 // Ditto, but explicitly dereferenced
144 ref shared(int) test_dotvarexp_2(return shared C1* c)
146 return (*c).value;
149 // Even taking the address (which offset the pointers) requires a load
150 shared(int)* test_dotvarexp_3(return shared C1* c)
152 return &c.value;
155 // First level DotVarExp dereferencing
156 ref shared(int) test_dotvarexp_4(return shared ref C2 c)
158 return c.c1.value;
161 // Second level DotVarExp dereferencing
162 ref shared(int) test_dotvarexp_5(return shared ref C3 c)
164 return c.c1.c1.value;
167 class K1
169 int value;
172 class K2
174 shared K1 k1;
177 class K3
179 K2 k2;
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)
185 return k.value;
188 // Using `k.ke.k1` would be okay, but not `value`
189 ref shared(int) test_dotvarexp_7(return ref K3 k)
191 return k.k2.k1.value;
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()
198 return new shared(K2)().k1;
201 // Inference tests
203 // Fails because no `ref`
204 auto test_inference_1(return shared ref C3 c)
206 return c;
209 // Fails because no `return` => Escapes
210 auto ref test_inference_2(shared C3 c)
212 return c;
215 shared(Object) getSharedObject() { assert(0); }
217 // Fails because rvalue
218 auto ref test_inference_3()
220 return getSharedObject();
223 // Fails because `const` conversion
224 auto ref Object test_inference_4(const return shared ref Object a)
226 return a;
229 // https://issues.dlang.org/show_bug.cgi?id=23226
230 // Allow accessing non-shared `this`
231 struct BitRange
233 int bits;
234 void f()
236 this.bits++;
241 TEST_OUTPUT:
243 fail_compilation/shared.d(3004): Error: cast from `void*` to `shared(int*)` not allowed in safe code
244 fail_compilation/shared.d(3005): Error: cast from `void*` to `shared(const(int*))` not allowed in safe code
245 fail_compilation/shared.d(3008): Error: cast from `shared(void*)` to `int*` not allowed in safe code
246 fail_compilation/shared.d(3009): Error: cast from `shared(void*)` to `shared(const(int*))` not allowed in safe code
250 #line 3000
252 void test_casting_safe() @safe
254 void *p;
255 auto t1 = cast(shared(int*))p;
256 auto t2 = cast(const(shared(int*)))p;
258 shared void* s;
259 auto x1 = cast(int*)s;
260 auto x2 = cast(const(shared(int*)))s;