| 1 |
/* REQUIRED_ARGS: -preview=nosharedaccess |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
pure nothrow @nogc ref @safe shared(C1)(return ref shared(C1) c) |
| 5 |
pure nothrow @nogc ref @safe shared(int)(return ref shared(C3) c) |
| 6 |
--- |
| 7 |
*/ |
| 8 |
ref shared(int) f(return shared ref int y) |
| 9 |
{ |
| 10 |
return y; |
| 11 |
} |
| 12 |
|
| 13 |
// https://issues.dlang.org/show_bug.cgi?id=20908 |
| 14 |
struct S |
| 15 |
{ |
| 16 |
int i = 2; |
| 17 |
} |
| 18 |
|
| 19 |
union U |
| 20 |
{ |
| 21 |
int i = 1; |
| 22 |
bool b; |
| 23 |
} |
| 24 |
|
| 25 |
void test20908() |
| 26 |
{ |
| 27 |
// shared locals (or struct members) should be able to be initialised: |
| 28 |
shared int x; |
| 29 |
|
| 30 |
ref shared(int) fun() |
| 31 |
{ |
| 32 |
static shared(int) val; |
| 33 |
|
| 34 |
// return by reference |
| 35 |
return val; |
| 36 |
} |
| 37 |
|
| 38 |
ref shared(int) fun2() |
| 39 |
{ |
| 40 |
static shared(int)* val; |
| 41 |
|
| 42 |
// transfer pointer to reference |
| 43 |
return *val; |
| 44 |
} |
| 45 |
|
| 46 |
ref shared(int) fun3() |
| 47 |
{ |
| 48 |
static shared(int)*** val; |
| 49 |
|
| 50 |
// Multiple indirections |
| 51 |
return ***val; |
| 52 |
} |
| 53 |
|
| 54 |
shared S s; |
| 55 |
shared U u; |
| 56 |
} |
| 57 |
|
| 58 |
// Simple tests for `DotVarExp` |
| 59 |
// A `DotVarExp` is `a.b`. If `a` is a `shared ref`, |
| 60 |
// it is of type `shared(T)*` (as opposed to `shared(T*)`). |
| 61 |
// We should allow arbitrarily nested `DotVarExp` as long |
| 62 |
// as no shared memory is read, as in the case above |
| 63 |
// (we're just offsetting a pointer). |
| 64 |
struct C1 |
| 65 |
{ |
| 66 |
int value; |
| 67 |
} |
| 68 |
|
| 69 |
struct C2 |
| 70 |
{ |
| 71 |
C1 c1; |
| 72 |
} |
| 73 |
|
| 74 |
struct C3 |
| 75 |
{ |
| 76 |
C2 c1; |
| 77 |
C2 c2; |
| 78 |
} |
| 79 |
|
| 80 |
ref shared(int) test_dotvarexp_1(return ref shared C1 c) |
| 81 |
{ |
| 82 |
return c.value; |
| 83 |
} |
| 84 |
|
| 85 |
shared(int)* test_dotvarexp_2(return ref shared C1 c) |
| 86 |
{ |
| 87 |
return &c.value; |
| 88 |
} |
| 89 |
|
| 90 |
shared(C2)* test_dotvarexp_3(return ref shared C3 c) |
| 91 |
{ |
| 92 |
return &c.c1; |
| 93 |
} |
| 94 |
|
| 95 |
shared(C2)* test_dotvarexp_4(return ref shared C3 c) |
| 96 |
{ |
| 97 |
return &c.c2; |
| 98 |
} |
| 99 |
|
| 100 |
ref shared(int) test_dotvarexp_5(return shared ref C3 c) |
| 101 |
{ |
| 102 |
return c.c1.c1.value; |
| 103 |
} |
| 104 |
|
| 105 |
ref shared(int) test_dotvarexp_5(return ref shared(C3)[] c) |
| 106 |
{ |
| 107 |
return c[0].c1.c1.value; |
| 108 |
} |
| 109 |
|
| 110 |
// Test `auto` inference |
| 111 |
auto ref test_inference_1(return ref shared C1 c) |
| 112 |
{ |
| 113 |
return c; |
| 114 |
} |
| 115 |
|
| 116 |
pragma(msg, typeof(test_inference_1)); |
| 117 |
|
| 118 |
auto ref test_inference_2(return ref shared C3 c) |
| 119 |
{ |
| 120 |
return c.c2.c1.value; |
| 121 |
} |
| 122 |
|
| 123 |
pragma(msg, typeof(test_inference_2)); |
| 124 |
|
| 125 |
// https://issues.dlang.org/show_bug.cgi?id=21793 |
| 126 |
|
| 127 |
struct Child |
| 128 |
{ |
| 129 |
this(int) shared {} |
| 130 |
} |
| 131 |
|
| 132 |
struct Parent |
| 133 |
{ |
| 134 |
shared Child ch; |
| 135 |
this(int i) shared |
| 136 |
{ |
| 137 |
ch = shared Child(i); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// https://issues.dlang.org/show_bug.cgi?id=23732 |
| 142 |
class Class {} |
| 143 |
void main() |
| 144 |
{ |
| 145 |
auto b = new shared Class(); |
| 146 |
} |
| 147 |
|
| 148 |
// https://issues.dlang.org/show_bug.cgi?id=23790 |
| 149 |
bool cas(shared bool*, bool, bool) { return true; } |
| 150 |
|
| 151 |
struct Argh |
| 152 |
{ |
| 153 |
bool locked; |
| 154 |
void lock() shared |
| 155 |
{ |
| 156 |
while(!cas(&locked, false, true)) {} |
| 157 |
} |
| 158 |
} |