Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testOpApply.d

raw
1 /* PERMUTE_ARGS: -preview=dip1000
2 */
3
4 // https://issues.dlang.org/show_bug.cgi?id=15624
5
6 struct Foo {
7 int x;
8 int opApply(int delegate(int, string, string) @safe dg) @safe {
9 x = 1;
10 return 0;
11 }
12 int opApply(int delegate(int, string, string) @system dg) @system {
13 x = 2;
14 return 0;
15 }
16 }
17
18 void testSafe() @safe {
19 Foo foo;
20 foreach (i, k, v; foo) {
21 }
22 assert(foo.x == 1);
23 }
24
25 void testSystem() @system {
26 Foo foo;
27 foreach (i, k, v; foo) {
28 }
29 assert(foo.x == 2);
30 }
31
32 void test() @system
33 {
34 Foo f;
35
36 int dgsafe (int x, string s, string t) @safe { return 1; }
37 int dgsystem(int x, string s, string t) @system { return 1; }
38
39 f.opApply(&dgsafe);
40 assert(f.x == 1);
41 f.opApply(&dgsystem);
42 assert(f.x == 2);
43 }
44
45 int main()
46 {
47 testSafe();
48 testSystem();
49 test();
50 testDifferentTypes();
51 testSameAttributes();
52 testInverseAttributes();
53 return 0;
54 }
55
56 void testDifferentTypes()
57 {
58 static struct DifferentTypes
59 {
60 int x;
61 int opApply(int delegate(int) dg) @safe {
62 x = 1;
63 return 0;
64 }
65 int opApply(int delegate(long) dg) @safe {
66 x = 2;
67 return 0;
68 }
69 }
70
71 DifferentTypes dt;
72 foreach (int i; dt) {}
73 assert(dt.x == 1);
74
75 foreach (long i; dt) {}
76 assert(dt.x == 2);
77 }
78
79 void testSameAttributes()
80 {
81 static struct SameAttributes
82 {
83 int x;
84 int opApply(int delegate(int) @system dg) @safe {
85 x = 1;
86 return 0;
87 }
88 int opApply(int delegate(int) @safe dg) @safe {
89 x = 2;
90 return 0;
91 }
92 }
93
94 static void safe() @safe
95 {
96 SameAttributes sa;
97 foreach (i; sa) {}
98 assert(sa.x == 2);
99 }
100 safe();
101
102 static void system() @system
103 {
104 SameAttributes sa;
105 foreach (i; sa) {}
106 assert(sa.x == 1);
107 }
108 system();
109 }
110
111 // Not useful but enabled by the associated patch
112 void testInverseAttributes()
113 {
114 static struct InverseAttributes
115 {
116 int x;
117 int opApply(int delegate(int) @system dg) @safe {
118 x = 1;
119 return 0;
120 }
121 int opApply(int delegate(int) @safe dg) @system {
122 x = 2;
123 return 0;
124 }
125 }
126
127 static void system() @system
128 {
129 InverseAttributes sa;
130 foreach (i; sa) {}
131 assert(sa.x == 1);
132 }
133 system();
134
135 static void safe() @safe
136 {
137 InverseAttributes sa;
138 (() @trusted { foreach (i; sa) {} })();
139 assert(sa.x == 2);
140 }
141 safe();
142 }
143
144 // https://issues.dlang.org/show_bug.cgi?id=20907
145 Lockstep!() lockstep()
146 {
147 return Lockstep!()();
148 }
149
150 struct Lockstep()
151 {
152 int opApply(int delegate(int) callback) @system
153 {
154 return 0;
155 }
156
157 int opApply(int delegate(int) pure nothrow @nogc @safe callback) pure nothrow @nogc @safe
158 {
159 return 0;
160 }
161 }
162
163 void foo0()
164 {
165 foreach (x; lockstep()) {}
166 }
167
168 void foo1()
169 {
170 foreach (x; lockstep()) {}
171 }